+ initialize_kivitendo($client);
+
+ my $job = SL::DB::Manager::BackgroundJob->find_by(id => $run_single_job);
+
+ if ($job) {
+ debug(" Executing the following job: " . $job->package_name);
+ } else {
+ debug(" No jobs to execute found");
+ next;
+ }
+
+ # Provide fresh global variables in case legacy code modifies
+ # them somehow.
+ initialize_kivitendo($client);
+
+ my $history = $job->run;
+
+ debug(" Executed job " . $job->package_name .
+ "; result: " . (!$history ? "no return value" : $history->has_failed ? "failed" : "succeeded") .
+ ($history && $history->has_failed ? "; error: " . $history->error_col : ""));
+
+ notify_on_failure(history => $history) if $history && $history->has_failed;
+
+ 1;
+ };
+
+ if (!$ok) {
+ my $error = $EVAL_ERROR;
+ $::lxdebug->message(LXDebug::WARN(), "Exception during execution: ${error}");
+ notify_on_failure(exception => $error);
+ }
+
+ cleanup_kivitendo();
+ }
+}
+
+sub run_once_for_all_clients {
+ initialize_kivitendo();
+
+ my $clients = enabled_clients();
+
+ foreach my $client (@{ $clients }) {
+ debug("Running for client ID " . $client->id . " (" . $client->name . ")");
+
+ my $ok = eval {
+ initialize_kivitendo($client);