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