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