Task-Server: Absolute Pfade für @INC verwenden
[kivitendo-erp.git] / scripts / task_server.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 BEGIN {
6   use SL::System::Process;
7   my $exe_dir = SL::System::Process::exe_dir;
8
9   unshift @INC, "${exe_dir}/modules/override"; # Use our own versions of various modules (e.g. YAML).
10   push    @INC, "${exe_dir}/modules/fallback"; # Only use our own versions of modules if there's no system version.
11   unshift @INC, $exe_dir;
12
13   chdir($exe_dir) || die "Cannot change directory to ${exe_dir}\n";
14 }
15
16 use CGI qw( -no_xhtml);
17 use Cwd;
18 use Daemon::Generic;
19 use Data::Dumper;
20 use DateTime;
21 use English qw(-no_match_vars);
22 use List::Util qw(first);
23 use POSIX qw(setuid setgid);
24 use SL::Auth;
25 use SL::DB::BackgroundJob;
26 use SL::BackgroundJob::ALL;
27 use SL::Form;
28 use SL::Helper::DateTime;
29 use SL::InstanceConfiguration;
30 use SL::LXDebug;
31 use SL::LxOfficeConf;
32 use SL::Locale;
33
34 our %lx_office_conf;
35
36 sub lxinit {
37   my $login = $lx_office_conf{task_server}->{login};
38
39   package main;
40
41   $::lxdebug       = LXDebug->new;
42   $::locale        = Locale->new($::lx_office_conf{system}->{language});
43   $::form          = Form->new;
44   $::auth          = SL::Auth->new;
45   $::instance_conf = SL::InstanceConfiguration->new;
46   $::request       = { cgi => CGI->new({}) };
47
48   die 'cannot reach auth db'               unless $::auth->session_tables_present;
49
50   $::auth->restore_session;
51
52   require "bin/mozilla/common.pl";
53
54   die "cannot find user $login"            unless %::myconfig = $::auth->read_user(login => $login);
55   die "cannot find locale for user $login" unless $::locale   = Locale->new('de');
56 }
57
58 sub drop_privileges {
59   my $user = $lx_office_conf{task_server}->{run_as};
60   return unless $user;
61
62   my ($uid, $gid);
63   while (my @details = getpwent()) {
64     next unless $details[0] eq $user;
65     ($uid, $gid) = @details[2, 3];
66     last;
67   }
68   endpwent();
69
70   if (!$uid) {
71     print "Error: Cannot drop privileges to ${user}: user does not exist\n";
72     exit 1;
73   }
74
75   if (!setgid($gid)) {
76     print "Error: Cannot drop group privileges to ${user} (group ID $gid): $!\n";
77     exit 1;
78   }
79
80   if (!setuid($uid)) {
81     print "Error: Cannot drop user privileges to ${user} (user ID $uid): $!\n";
82     exit 1;
83   }
84 }
85
86 sub gd_preconfig {
87   my $self = shift;
88
89   SL::LxOfficeConf->read($self->{configfile});
90
91   die "Missing section [task_server] in config file"                unless $lx_office_conf{task_server};
92   die "Missing key 'login' in section [task_server] in config file" unless $lx_office_conf{task_server}->{login};
93
94   drop_privileges();
95   lxinit();
96
97   return ();
98 }
99
100 sub gd_run {
101   while (1) {
102     my $ok = eval {
103       $::lxdebug->message(0, "Retrieving jobs") if $lx_office_conf{task_server}->{debug};
104
105       my $jobs = SL::DB::Manager::BackgroundJob->get_all_need_to_run;
106
107       $::lxdebug->message(0, "  Found: " . join(' ', map { $_->package_name } @{ $jobs })) if $lx_office_conf{task_server}->{debug} && @{ $jobs };
108
109       foreach my $job (@{ $jobs }) {
110         # Provide fresh global variables in case legacy code modifies
111         # them somehow.
112         $::locale = Locale->new($::lx_office_conf{system}->{language});
113         $::form   = Form->new;
114
115         $job->run;
116       }
117
118       1;
119     };
120
121     if ($lx_office_conf{task_server}->{debug}) {
122       $::lxdebug->message(0, "Exception during execution: ${EVAL_ERROR}") if !$ok;
123       $::lxdebug->message(0, "Sleeping");
124     }
125
126     my $seconds = 60 - (localtime)[0];
127     if (!eval {
128       local $SIG{'ALRM'} = sub {
129         $::lxdebug->message(0, "Got woken up by SIGALRM") if $lx_office_conf{task_server}->{debug};
130         die "Alarm!\n"
131       };
132       sleep($seconds < 30 ? $seconds + 60 : $seconds);
133       1;
134     }) {
135       die $@ unless $@ eq "Alarm!\n";
136     }
137   }
138 }
139
140 my $cwd     = getcwd();
141 my $pidbase = "${cwd}/users/pid";
142
143 mkdir($pidbase) if !-d $pidbase;
144
145 my $file = first { -f } ("${cwd}/config/kivitendo.conf", "${cwd}/config/lx_office.conf", "${cwd}/config/kivitendo.conf.default");
146 newdaemon(configfile => $file,
147           progname   => 'kivitendo-task-server',
148           pidbase    => "${pidbase}/",
149           );
150
151 1;