Task-Server und -Control-Modul auf neue Konfigurationsdateinamen angepasst
[kivitendo-erp.git] / scripts / task_server.pl
1 #!/usr/bin/perl
2
3 use strict;
4
5 BEGIN {
6   require Cwd;
7
8   my $dir =  $0;
9   $dir    =  Cwd::getcwd() . '/' . $dir unless $dir =~ m|^/|;
10   $dir    =~ s|[^/]+$|..|;
11
12   chdir($dir) || die "Cannot change directory to ${dir}\n";
13
14   unshift @INC, "modules/override"; # Use our own versions of various modules (e.g. YAML).
15   push    @INC, "modules/fallback"; # Only use our own versions of modules if there's no system version.
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         $job->run;
118       }
119
120       1;
121     };
122
123     if ($lx_office_conf{task_server}->{debug}) {
124       $::lxdebug->message(0, "Exception during execution: ${EVAL_ERROR}") if !$ok;
125       $::lxdebug->message(0, "Sleeping");
126     }
127
128     my $seconds = 60 - (localtime)[0];
129     if (!eval {
130       local $SIG{'ALRM'} = sub {
131         $::lxdebug->message(0, "Got woken up by SIGALRM") if $lx_office_conf{task_server}->{debug};
132         die "Alarm!\n"
133       };
134       sleep($seconds < 30 ? $seconds + 60 : $seconds);
135       1;
136     }) {
137       die $@ unless $@ eq "Alarm!\n";
138     }
139   }
140 }
141
142 my $cwd     = getcwd();
143 my $pidbase = "${cwd}/users/pid";
144
145 mkdir($pidbase) if !-d $pidbase;
146
147 my $file = first { -f } ("${cwd}/config/kivitendo.conf", "${cwd}/config/lx_office.conf", "${cwd}/config/kivitendo.conf.default");
148 newdaemon(configfile => $file,
149           progname   => 'kivitendo-task-server',
150           pidbase    => "${pidbase}/",
151           );
152
153 1;