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