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, @_);
54 sub initialize_kivitendo {
57 my $login = $lx_office_conf{task_server}->{login};
58 my $client = $lx_office_conf{task_server}->{client};
62 Form::disconnect_standard_dbh;
63 $::lxdebug = LXDebug->new;
64 $::locale = Locale->new($::lx_office_conf{system}->{language});
66 $::auth = SL::Auth->new;
67 die "No client configured or no client found with the name/ID '$client'" unless $::auth->set_client($client);
68 $::instance_conf = SL::InstanceConfiguration->new;
69 $::request = SL::Request->new(
71 layout => SL::Layout::None->new,
74 die 'cannot reach auth db' unless $::auth->session_tables_present;
76 $::auth->restore_session;
77 $::auth->create_or_refresh_session;
79 die "cannot find user $login" unless %::myconfig = $::auth->read_user(login => $login);
80 die "cannot find locale for user $login" unless $::locale = Locale->new($::myconfig{countrycode} || $::lx_office_conf{system}->{language});
82 $::form->{__ERROR_HANDLER} = sub { die @_ };
86 my $user = $lx_office_conf{task_server}->{run_as};
90 while (my @details = getpwent()) {
91 next unless $details[0] eq $user;
92 ($uid, $gid) = @details[2, 3];
98 print "Error: Cannot drop privileges to ${user}: user does not exist\n";
103 print "Error: Cannot drop group privileges to ${user} (group ID $gid): $!\n";
108 print "Error: Cannot drop user privileges to ${user} (user ID $uid): $!\n";
113 sub notify_on_failure {
116 my $cfg = $lx_office_conf{'task_server/notify_on_failure'} || {};
118 return if any { !$cfg->{$_} } qw(send_email_to email_from email_subject email_template);
122 return debug("Template " . $cfg->{email_template} . " missing!") unless -f $cfg->{email_template};
124 my $email_to = $cfg->{send_email_to};
125 if ($email_to !~ m{\@}) {
126 my %user = $::auth->read_user(login => $email_to);
127 return debug("cannot find user for notification $email_to") unless %user;
129 $email_to = $user{email};
130 return debug("user for notification " . $user{login} . " doesn't have a valid email address") unless $email_to =~ m{\@};
133 my $template = Template->new({
140 return debug("Could not create Template instance") unless $template;
142 $params{client} = $::auth->client;
145 $template->process($cfg->{email_template}, \%params, \$body);
148 from => $cfg->{email_from},
150 subject => $cfg->{email_subject},
151 content_type => 'text/plain',
153 message => Encode::decode('utf-8', $body),
160 SL::LxOfficeConf->read($self->{configfile});
162 die "Missing section [task_server] in config file" unless $lx_office_conf{task_server};
163 die "Missing key 'login' in section [task_server] in config file" unless $lx_office_conf{task_server}->{login};
164 die "Missing key 'client' in section [task_server] in config file" unless $lx_office_conf{task_server}->{client};
167 initialize_kivitendo();
175 initialize_kivitendo();
177 debug("Retrieving jobs");
179 my $jobs = SL::DB::Manager::BackgroundJob->get_all_need_to_run;
181 debug(" Found: " . join(' ', map { $_->package_name } @{ $jobs })) if @{ $jobs };
183 foreach my $job (@{ $jobs }) {
184 # Provide fresh global variables in case legacy code modifies
186 initialize_kivitendo();
188 my $history = $job->run;
190 notify_on_failure(history => $history) if $history && $history->has_failed;
197 my $error = $EVAL_ERROR;
198 debug("Exception during execution: ${error}");
199 notify_on_failure(exception => $error);
204 my $seconds = 60 - (localtime)[0];
206 local $SIG{'ALRM'} = sub {
207 debug("Got woken up by SIGALRM");
210 sleep($seconds < 30 ? $seconds + 60 : $seconds);
213 die $@ unless $@ eq "Alarm!\n";
219 $main::lxdebug->show_backtrace();
221 Job called ::end_of_request()!
223 This usually indicates success but should not be used by background jobs. A
224 backtrace has been logged. Please tell the job author to have a look at it.
231 mkdir SL::System::TaskServer::PID_BASE() if !-d SL::System::TaskServer::PID_BASE();
233 my $file = first { -f } ("${exe_dir}/config/kivitendo.conf", "${exe_dir}/config/lx_office.conf", "${exe_dir}/config/kivitendo.conf.default");
235 die "No configuration file found." unless $file;
237 $file = File::Spec->abs2rel(Cwd::abs_path($file), Cwd::abs_path($exe_dir));
239 newdaemon(configfile => $file,
240 progname => 'kivitendo-background-jobs',
241 pidbase => SL::System::TaskServer::PID_BASE() . '/',