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