38 $this->logger->info(
"CRON - batch start");
41 $this->
settings->set(
"last_cronjob_start_ts", $ts);
46 $this->logger->info(sprintf(
47 'Verification of last run datetime (read from database): %s',
55 if (!defined(
"ILIAS_HTTP_PATH")) {
60 foreach (self::getCronJobData(null,
false) as
$row) {
61 $job = self::getJobInstanceById($row[
"job_id"]);
69 foreach (self::getPluginJobs(
true) as $item) {
70 self::runJob($item[0], $item[1]);
73 $this->logger->info(
"CRON - batch end");
86 $ilLog = $DIC->logger()->root();
90 $ilLog->write(
"CRON - manual start (" . $a_job_id .
")");
92 $job = self::getJobInstanceById($a_job_id);
94 if ($job->isManuallyExecutable()) {
95 $result = self::runJob($job, null,
true);
97 $ilLog->write(
"CRON - job " . $a_job_id .
" is not intended to be executed manually");
100 $ilLog->write(
"CRON - job " . $a_job_id .
" seems invalid or is inactive");
103 $ilLog->write(
"CRON - manual end (" . $a_job_id .
")");
116 protected static function runJob(
ilCronJob $a_job, array $a_job_data = null, $a_manual =
false)
120 $ilLog = $DIC->logger()->root();
121 $ilDB = $DIC->database();
125 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
127 if ($a_job_data === null) {
129 $jobData = self::getCronJobData($a_job->
getId());
130 $a_job_data = array_pop($jobData);
134 if ($a_job_data[
"alive_ts"]) {
135 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" still running");
140 if (time() - $a_job_data[
"alive_ts"] > $cut) {
141 $ilDB->manipulate(
"UPDATE cron_job SET" .
142 " running_ts = " .
$ilDB->quote(0,
"integer") .
143 " , alive_ts = " .
$ilDB->quote(0,
"integer") .
144 " WHERE job_id = " .
$ilDB->quote($a_job_data[
"job_id"],
"text"));
146 self::deactivateJob($a_job);
151 $result->setMessage(
"Cron job deactivated because it has been inactive for 3 hours");
154 self::sendNotification($a_job,
$result);
157 self::updateJobResult($a_job,
$result, $a_manual);
159 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" deactivated (assumed crash)");
164 $a_job_data[
"job_result_ts"],
165 $a_job_data[
"schedule_type"],
166 $a_job_data[
"schedule_value"],
169 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" started");
171 $ilDB->manipulate(
"UPDATE cron_job SET" .
172 " running_ts = " .
$ilDB->quote(time(),
"integer") .
173 " , alive_ts = " .
$ilDB->quote(time(),
"integer") .
174 " WHERE job_id = " .
$ilDB->quote($a_job_data[
"job_id"],
"text"));
176 $ts_in = self::getMicrotime();
180 $result = new \ilCronJobResult();
182 $result->setMessage(sprintf(
"Exception: %s", $e->getMessage()));
184 $ilLog->error($e->getMessage());
185 $ilLog->error($e->getTraceAsString());
187 $result = new \ilCronJobResult();
189 $result->setMessage(sprintf(
"Exception: %s", $e->getMessage()));
191 $ilLog->error($e->getMessage());
192 $ilLog->error($e->getTraceAsString());
194 $ts_dur = self::getMicrotime() - $ts_in;
198 $result =
new ilCronJobResult();
201 $result->setMessage(
"Cron job did not return a proper result");
204 self::sendNotification($a_job,
$result);
207 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" no result");
211 self::deactivateJob($a_job);
214 self::sendNotification($a_job,
$result);
217 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" invalid configuration");
226 self::updateJobResult($a_job,
$result, $a_manual);
228 $ilDB->manipulate(
"UPDATE cron_job SET" .
229 " running_ts = " .
$ilDB->quote(0,
"integer") .
230 " , alive_ts = " .
$ilDB->quote(0,
"integer") .
231 " WHERE job_id = " .
$ilDB->quote($a_job_data[
"job_id"],
"text"));
233 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" finished");
235 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" returned status inactive");
251 $ilLog = $DIC->logger()->root();
252 $ilPluginAdmin = $DIC[
'ilPluginAdmin'];
255 if (substr($a_job_id, 0, 4) ==
"pl__") {
256 $parts = explode(
"__", $a_job_id);
257 $pl_name = $parts[1];
259 if ($ilPluginAdmin->isActive(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name)) {
260 $plugin_obj = $ilPluginAdmin->getPluginObject(
266 $job = $plugin_obj->getCronJobInstance($job_id);
281 $job_data = self::getCronJobData($a_job_id);
282 $job_data = array_pop($job_data);
283 if ($job_data[
"job_id"] == $a_job_id) {
284 return self::getJobInstance(
286 $job_data[
"component"],
293 $ilLog->write(
"CRON - job " . $a_job_id .
" seems invalid or is inactive");
304 public static function getJobInstance($a_id, $a_component, $a_class, $a_path = null)
308 $ilLog = $DIC->logger()->root();
311 $a_path = $a_component .
"/classes/";
313 $class_file = $a_path .
"class." . $a_class .
".php";
314 if (file_exists($class_file)) {
315 include_once $class_file;
316 if (class_exists($a_class)) {
317 $refl = new \ReflectionClass($a_class);
318 $job = $refl->newInstanceWithoutConstructor();
319 if ($refl->isSubclassOf(\ilCronJob::class)) {
320 if (0 === strlen($job->getId()) || !isset(
$_SERVER[
'PHP_SELF']) || basename(
$_SERVER[
'PHP_SELF']) !==
'setup.php') {
324 if ($job->getId() === $a_id) {
327 $mess .=
" - job id mismatch";
330 $mess .=
" - does not extend ilCronJob";
333 $mess =
"- class not found in file";
336 $mess =
" - class file not found";
339 $ilLog->write(
"Cron XML - Job " . $a_id .
" in class " . $a_class .
" (" .
340 $class_file .
") is invalid." . $mess);
358 $ilLog = $DIC->logger()->root();
359 $ilDB = $DIC->database();
361 if (!isset($DIC[
"ilSetting"])) {
362 $DIC[
"ilSetting"] =
function (
$c) {
370 $sql =
"SELECT job_id, schedule_type, component, class, path FROM cron_job" .
371 " WHERE job_id = " .
$ilDB->quote($a_job->
getId(),
"text");
372 $set =
$ilDB->query($sql);
374 $job_exists = (
$row[
"job_id"] == $a_job->
getId());
375 $schedule_type =
$row[
"schedule_type"];
378 $row[
'component'] != $a_component ||
379 $row[
'class'] != $a_class ||
380 $row[
'path'] != $a_path
383 'UPDATE cron_job SET component = %s, class = %s, path = %s WHERE job_id = %s',
384 [
'text',
'text',
'text',
'text'],
385 [$a_component, $a_class, $a_path, $a_job->
getId()]
391 $sql =
"INSERT INTO cron_job (job_id, component, class, path)" .
392 " VALUES (" .
$ilDB->quote($a_job->
getId(),
"text") .
", " .
393 $ilDB->quote($a_component,
"text") .
", " .
394 $ilDB->quote($a_class,
"text") .
", " .
395 $ilDB->quote($a_path,
"text") .
")";
396 $ilDB->manipulate($sql);
398 $ilLog->write(
"Cron XML - Job " . $a_job->
getId() .
" in class " . $a_class .
402 self::updateJobSchedule(
410 include_once
"Services/Administration/classes/class.ilSetting.php";
415 self::activateJob($a_job);
423 self::updateJobSchedule(
431 self::updateJobSchedule($a_job, null, null);
443 public static function updateFromXML($a_component, $a_id, $a_class, $a_path = null)
447 $ilDB = $DIC->database();
449 if (!
$ilDB->tableExists(
"cron_job")) {
454 $job = self::getJobInstance($a_id, $a_component, $a_class, $a_path);
456 self::createDefaultEntry($job, $a_component, $a_class, $a_path);
466 public static function clearFromXML($a_component, array $a_xml_job_ids)
470 $ilDB = $DIC->database();
471 $ilLog = $DIC->logger()->root();
473 if (!
$ilDB->tableExists(
"cron_job")) {
479 $sql =
"SELECT job_id FROM cron_job" .
480 " WHERE component = " .
$ilDB->quote($a_component,
"text");
481 $set =
$ilDB->query($sql);
483 $all_jobs[] =
$row[
"job_id"];
486 if (
sizeof($all_jobs)) {
487 if (
sizeof($a_xml_job_ids)) {
489 foreach ($all_jobs as $job_id) {
490 if (!in_array($job_id, $a_xml_job_ids)) {
491 $ilDB->manipulate(
"DELETE FROM cron_job" .
492 " WHERE component = " .
$ilDB->quote($a_component,
"text") .
493 " AND job_id = " .
$ilDB->quote($job_id,
"text"));
495 $ilLog->write(
"Cron XML - Job " . $job_id .
" in class " . $a_component .
500 $ilDB->manipulate(
"DELETE FROM cron_job" .
501 " WHERE component = " .
$ilDB->quote($a_component,
"text"));
503 $ilLog->write(
"Cron XML - All jobs deleted for " . $a_component .
" as component is inactive.");
512 $ilPluginAdmin = $DIC[
'ilPluginAdmin'];
516 foreach ($ilPluginAdmin->getActivePluginsForSlot(
IL_COMP_SERVICE,
"Cron",
"crnhk") as $pl_name) {
517 $plugin_obj = $ilPluginAdmin->getPluginObject(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name);
519 foreach ((array) $plugin_obj->getCronJobInstances() as $job) {
521 $item = array_pop($jobData);
522 if (!is_array($item) || 0 === count($item)) {
528 $item = array_pop($jobData);
531 if (!$a_only_active ||
532 $item[
"job_status"] == 1) {
533 $res[$job->getId()] = array($job, $item);
551 $ilDB = $DIC->database();
555 if ($a_id && !is_array($a_id)) {
556 $a_id = array($a_id);
559 $sql =
"SELECT * FROM cron_job";
563 $where[] =
$ilDB->in(
"job_id", $a_id,
"",
"text");
567 if (!$a_include_inactive) {
568 $where[] =
"job_status = " .
$ilDB->quote(1,
"integer");
570 if (
sizeof($where)) {
571 $sql .=
" WHERE " . implode(
" AND ", $where);
575 $sql .=
" ORDER BY job_id";
577 $set =
$ilDB->query($sql);
593 $ilDB = $DIC->database();
595 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
599 $result->setMessage(
"Cron job re-activated by admin");
600 self::updateJobResult($a_job,
$result,
true);
602 $ilDB->manipulate(
"UPDATE cron_job" .
603 " SET running_ts = " .
$ilDB->quote(0,
"integer") .
604 " , alive_ts = " .
$ilDB->quote(0,
"integer") .
605 " , job_result_ts = " .
$ilDB->quote(0,
"integer") .
606 " WHERE job_id = " .
$ilDB->quote($a_job->
getId(),
"text"));
608 self::activateJob($a_job,
true);
620 $ilDB = $DIC->database();
623 if ($DIC->isDependencyAvailable(
'user')) {
624 $user = $DIC->user();
625 $user_id = $a_manual ?
$user->getId() : 0;
628 $sql =
"UPDATE cron_job SET " .
629 " job_status = " .
$ilDB->quote(1,
"integer") .
630 " , job_status_user_id = " .
$ilDB->quote($user_id,
"integer") .
631 " , job_status_type = " .
$ilDB->quote($a_manual,
"integer") .
632 " , job_status_ts = " .
$ilDB->quote(time(),
"integer") .
633 " WHERE job_id = " .
$ilDB->quote($a_job->
getId(),
"text");
634 $ilDB->manipulate($sql);
648 $ilDB = $DIC->database();
651 $user_id = $a_manual ?
$ilUser->getId() : 0;
653 $sql =
"UPDATE cron_job SET " .
654 " job_status = " .
$ilDB->quote(0,
"integer") .
655 " , job_status_user_id = " .
$ilDB->quote($user_id,
"integer") .
656 " , job_status_type = " .
$ilDB->quote($a_manual,
"integer") .
657 " , job_status_ts = " .
$ilDB->quote(time(),
"integer") .
658 " WHERE job_id = " .
$ilDB->quote($a_job->
getId(),
"text");
659 $ilDB->manipulate($sql);
672 $job = self::getCronJobData($a_job_id);
673 if ((
bool) $job[0][
"job_status"]) {
687 $job = self::getCronJobData($a_job_id);
688 if (!(
bool) $job[0][
"job_status"]) {
704 $ilDB = $DIC->database();
707 $user_id = $a_manual ?
$ilUser->getId() : 0;
709 $sql =
"UPDATE cron_job SET " .
710 " job_result_status = " .
$ilDB->quote($a_result->
getStatus(),
"integer") .
711 " , job_result_user_id = " .
$ilDB->quote($user_id,
"integer") .
712 " , job_result_code = " .
$ilDB->quote($a_result->
getCode(),
"text") .
713 " , job_result_message = " .
$ilDB->quote($a_result->
getMessage(),
"text") .
714 " , job_result_type = " .
$ilDB->quote($a_manual,
"integer") .
715 " , job_result_ts = " .
$ilDB->quote(time(),
"integer") .
716 " , job_result_dur = " .
$ilDB->quote($a_result->
getDuration() * 1000,
"integer") .
717 " WHERE job_id = " .
$ilDB->quote($a_job->
getId(),
"text");
718 $ilDB->manipulate($sql);
731 $ilDB = $DIC->database();
733 if ($a_schedule_type === null ||
736 $sql =
"UPDATE cron_job SET " .
737 " schedule_type = " .
$ilDB->quote($a_schedule_type,
"integer") .
738 " , schedule_value = " .
$ilDB->quote($a_schedule_value,
"integer") .
739 " WHERE job_id = " .
$ilDB->quote($a_job->
getId(),
"text");
740 $ilDB->manipulate($sql);
751 list($usec, $sec) = explode(
" ", microtime());
752 return ((
float) $usec + (
float) $sec);
760 public static function ping($a_job_id)
763 $ilDB = $DIC->database();
765 $ilDB->manipulate(
"UPDATE cron_job SET " .
766 " alive_ts = " .
$ilDB->quote(time(),
"integer") .
767 " WHERE job_id = " .
$ilDB->quote($a_job_id,
"text"));
static getJobInstance($a_id, $a_component, $a_class, $a_path=null)
Get job instance (by job data)
static updateFromXML($a_component, $a_id, $a_class, $a_path=null)
Process data from module.xml/service.xml.
const CODE_SUPPOSED_CRASH
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
static sendNotification(ilCronJob $a_job, $a_message)
Send notification to admin about job event(s)
getValidScheduleTypes()
Returns a collection of all valid schedule types for a specific job.
const STATUS_INVALID_CONFIGURATION
runActiveJobs()
Run all active jobs.
Cron job application base class.
static deactivateJob(ilCronJob $a_job, $a_manual=false)
Deactivate cron job.
static runJob(ilCronJob $a_job, array $a_job_data=null, $a_manual=false)
Run single cron job (internal)
static activateJob(ilCronJob $a_job, $a_manual=false)
Activate cron job.
static isJobInactive($a_job_id)
Check if given job is currently inactive.
static setUseRelativeDates($a_status)
set use relative dates
static runJobManual($a_job_id)
Run single job manually.
activationWasToggled($a_currently_active)
Cron job status was changed.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
static useRelativeDates()
check if relative dates are used
static resetJob(ilCronJob $a_job)
Reset job.
static _lookupValue($a_module, $a_keyword)
static getCronJobData($a_id=null, $a_include_inactive=true)
Get cron job configuration/execution data.
foreach($_POST as $key=> $value) $res
static getJobInstanceById($a_job_id)
Get job instance (by job id)
hasFlexibleSchedule()
Can the schedule be configured?
static createDefaultEntry(ilCronJob $a_job, $a_component, $a_class, $a_path)
isActive($a_ts_last_run, $a_schedule_type, $a_schedule_value, $a_manual=false)
Is job currently active?
static updateJobResult(ilCronJob $a_job, ilCronJobResult $a_result, $a_manual=false)
Save job result.
getDefaultScheduleType()
Get schedule type.
static ping($a_job_id)
Keep cron job alive.
static getPluginJobs($a_only_active=false)
static getMicrotime()
Get current microtime.
static clearFromXML($a_component, array $a_xml_job_ids)
Clear job data.
getDefaultScheduleValue()
Get schedule value.
__construct(\ilSetting $settings, \ilLogger $logger)
ilCronManager constructor.
Class ilStrictCliCronManager.
static updateJobSchedule(ilCronJob $a_job, $a_schedule_type, $a_schedule_value)
Update job schedule.
Cron job result data container.
Component logger with individual log levels by component id.
static isJobActive($a_job_id)
Check if given job is currently active.
hasAutoActivation()
Is to be activated on "installation".