4 use List::MoreUtils qw(any);
12 use lib "$FindBin::Bin/..";
14 use SL::System::Process;
15 $exe_dir = SL::System::Process::exe_dir;
17 unshift @INC, "${exe_dir}/modules/override"; # Use our own versions of various modules (e.g. YAML).
18 push @INC, "${exe_dir}/modules/fallback"; # Only use our own versions of modules if there's no system version.
19 unshift @INC, $exe_dir;
21 chdir($exe_dir) || die "Cannot change directory to ${exe_dir}\n";
24 use CGI qw( -no_xhtml);
30 use English qw(-no_match_vars);
32 use List::Util qw(first);
33 use POSIX qw(setuid setgid);
35 use SL::DB::BackgroundJob;
36 use SL::BackgroundJob::ALL;
38 use SL::Helper::DateTime;
39 use SL::InstanceConfiguration;
44 use SL::System::TaskServer;
50 return if !$lx_office_conf{task_server}->{debug};
51 $::lxdebug->message(0, @_);
55 my $login = $lx_office_conf{task_server}->{login};
56 my $client = $lx_office_conf{task_server}->{client};
60 $::lxdebug = LXDebug->new;
61 $::locale = Locale->new($::lx_office_conf{system}->{language});
63 $::auth = SL::Auth->new;
64 die "No client configured or no client found with the name/ID '$client'" unless $::auth->set_client($client);
65 $::instance_conf = SL::InstanceConfiguration->new;
66 $::request = { cgi => CGI->new({}) };
68 die 'cannot reach auth db' unless $::auth->session_tables_present;
70 $::auth->restore_session;
72 require "bin/mozilla/common.pl";
74 die "cannot find user $login" unless %::myconfig = $::auth->read_user(login => $login);
75 die "cannot find locale for user $login" unless $::locale = Locale->new('de');
78 sub per_job_initialization {
79 $::locale = Locale->new($::myconfig{countrycode} || $::lx_office_conf{system}->{language});
81 $::instance_conf = SL::InstanceConfiguration->new;
82 $::request = SL::Request->new(
84 layout => SL::Layout::None->new,
87 $::auth->restore_session;
89 $::form->{login} = $lx_office_conf{task_server}->{login};
90 $::instance_conf->init;
94 my $user = $lx_office_conf{task_server}->{run_as};
98 while (my @details = getpwent()) {
99 next unless $details[0] eq $user;
100 ($uid, $gid) = @details[2, 3];
106 print "Error: Cannot drop privileges to ${user}: user does not exist\n";
111 print "Error: Cannot drop group privileges to ${user} (group ID $gid): $!\n";
116 print "Error: Cannot drop user privileges to ${user} (user ID $uid): $!\n";
121 sub notify_on_failure {
124 my $cfg = $lx_office_conf{'task_server/notify_on_failure'} || {};
126 return if any { !$cfg->{$_} } qw(send_email_to email_from email_subject email_template);
130 return debug("Template " . $cfg->{email_template} . " missing!") unless -f $cfg->{email_template};
132 my $email_to = $cfg->{send_email_to};
133 if ($email_to !~ m{\@}) {
134 my %user = $::auth->read_user(login => $email_to);
135 return debug("cannot find user for notification $email_to") unless %user;
137 $email_to = $user{email};
138 return debug("user for notification " . $user{login} . " doesn't have a valid email address") unless $email_to =~ m{\@};
141 my $template = Template->new({
148 return debug("Could not create Template instance") unless $template;
150 $params{client} = $::auth->client;
153 $template->process($cfg->{email_template}, \%params, \$body);
156 from => $cfg->{email_from},
158 subject => $cfg->{email_subject},
159 content_type => 'text/plain',
161 message => Encode::decode('utf-8', $body),
168 SL::LxOfficeConf->read($self->{configfile});
170 die "Missing section [task_server] in config file" unless $lx_office_conf{task_server};
171 die "Missing key 'login' in section [task_server] in config file" unless $lx_office_conf{task_server}->{login};
172 die "Missing key 'client' in section [task_server] in config file" unless $lx_office_conf{task_server}->{client};
183 debug("Retrieving jobs");
185 my $jobs = SL::DB::Manager::BackgroundJob->get_all_need_to_run;
187 debug(" Found: " . join(' ', map { $_->package_name } @{ $jobs })) if @{ $jobs };
189 foreach my $job (@{ $jobs }) {
190 # Provide fresh global variables in case legacy code modifies
192 per_job_initialization();
196 my $history = $job->run;
198 notify_on_failure(history => $history) if $history && $history->has_failed;
205 my $error = $EVAL_ERROR;
206 debug("Exception during execution: ${error}");
207 notify_on_failure(exception => $error);
212 my $seconds = 60 - (localtime)[0];
214 local $SIG{'ALRM'} = sub {
215 debug("Got woken up by SIGALRM");
218 sleep($seconds < 30 ? $seconds + 60 : $seconds);
221 die $@ unless $@ eq "Alarm!\n";
228 mkdir SL::System::TaskServer::PID_BASE() if !-d SL::System::TaskServer::PID_BASE();
230 my $file = first { -f } ("${exe_dir}/config/kivitendo.conf", "${exe_dir}/config/lx_office.conf", "${exe_dir}/config/kivitendo.conf.default");
232 die "No configuration file found." unless $file;
234 $file = File::Spec->abs2rel(Cwd::abs_path($file), Cwd::abs_path($exe_dir));
236 newdaemon(configfile => $file,
237 progname => 'kivitendo-background-jobs',
238 pidbase => SL::System::TaskServer::PID_BASE() . '/',