23 $ilLog->write(
"CRON - batch start");
25 $ilSetting->set(
"last_cronjob_start_ts", time());
28 if(!defined(
"ILIAS_HTTP_PATH"))
34 foreach(self::getCronJobData(null,
false) as
$row)
44 foreach(self::getPluginJobs(
true) as $item)
49 $ilLog->write(
"CRON - batch end");
64 $ilLog->write(
"CRON - manual start (".$a_job_id.
")");
69 if($job->isManuallyExecutable())
71 $job_data = array_pop(self::getCronJobData($job->getId()));
76 $ilLog->write(
"CRON - job ".$a_job_id.
" is not intended to be executed manually");
81 $ilLog->write(
"CRON - job ".$a_job_id.
" seems invalid or is inactive");
84 $ilLog->write(
"CRON - manual end (".$a_job_id.
")");
97 protected static function runJob(
ilCronJob $a_job, array $a_job_data, $a_manual =
false)
105 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
108 if($a_job_data[
"alive_ts"])
110 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" still running");
115 if(time()-$a_job_data[
"alive_ts"] > $cut)
117 $ilDB->manipulate(
"UPDATE cron_job SET".
118 " running_ts = ".$ilDB->quote(0,
"integer").
119 " , alive_ts = ".$ilDB->quote(0,
"integer").
120 " WHERE job_id = ".$ilDB->quote($a_job_data[
"job_id"],
"text"));
126 $result->setCode(
"job_auto_deactivation_time_limit");
127 $result->setMessage(
"Cron job deactivated because it has been inactive for 3 hours");
136 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" deactivated (assumed crash)");
140 else if($a_job->
isActive($a_job_data[
"job_result_ts"],
141 $a_job_data[
"schedule_type"], $a_job_data[
"schedule_value"], $a_manual))
143 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" started");
145 $ilDB->manipulate(
"UPDATE cron_job SET".
146 " running_ts = ".$ilDB->quote(time(),
"integer").
147 " , alive_ts = ".$ilDB->quote(time(),
"integer").
148 " WHERE job_id = ".$ilDB->quote($a_job_data[
"job_id"],
"text"));
157 $result =
new ilCronJobResult();
159 $result->setCode(
"job_no_result");
160 $result->setMessage(
"Cron job did not return a proper result");
167 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" no result");
179 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" invalid configuration");
191 $ilDB->manipulate(
"UPDATE cron_job SET".
192 " running_ts = ".$ilDB->quote(0,
"integer").
193 " , alive_ts = ".$ilDB->quote(0,
"integer").
194 " WHERE job_id = ".$ilDB->quote($a_job_data[
"job_id"],
"text"));
196 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" finished");
200 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" returned status inactive");
215 global
$ilLog, $ilPluginAdmin;
218 if(substr($a_job_id, 0, 4) ==
"pl__")
220 $parts = explode(
"__", $a_job_id);
221 $pl_name = $parts[1];
223 if($ilPluginAdmin->isActive(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name))
226 "Cron",
"crnhk", $pl_name);
227 $job = $plugin_obj->getCronJobInstance($job_id);
245 $job_data = array_pop(self::getCronJobData($a_job_id));
246 if($job_data[
"job_id"] == $a_job_id)
249 $job_data[
"class"], $job_data[
"path"]);
253 $ilLog->write(
"CRON - job ".$a_job_id.
" seems invalid or is inactive");
264 public static function getJobInstance($a_id, $a_component, $a_class, $a_path = null)
270 $a_path = $a_component.
"/classes/";
272 $class_file = $a_path.
"class.".$a_class.
".php";
273 if(file_exists($class_file))
275 include_once $class_file;
276 if(class_exists($a_class))
278 $job =
new $a_class();
281 if($job->getId() == $a_id)
287 $mess .=
" - job id mismatch";
292 $mess .=
" - does not extend ilCronJob";
297 $mess =
"- class not found in file";
302 $mess =
" - class file not found";
305 $ilLog->write(
"Cron XML - Job ".$a_id.
" in class ".$a_class.
" (".
306 $class_file.
") is invalid.".$mess);
325 $sql =
"SELECT job_id, schedule_type FROM cron_job".
326 " WHERE component = ".$ilDB->quote($a_component,
"text").
327 " AND job_id = ".$ilDB->quote($a_job->
getId(),
"text");
328 $set = $ilDB->query($sql);
329 $row = $ilDB->fetchAssoc($set);
330 $job_exists = (
$row[
"job_id"] == $a_job->
getId());
331 $schedule_type =
$row[
"schedule_type"];
336 $sql =
"INSERT INTO cron_job (job_id, component, class, path)".
337 " VALUES (".$ilDB->quote($a_job->
getId(),
"text").
", ".
338 $ilDB->quote($a_component,
"text").
", ".
339 $ilDB->quote($a_class,
"text").
", ".
340 $ilDB->quote($a_path,
"text").
")";
341 $ilDB->manipulate($sql);
343 $ilLog->write(
"Cron XML - Job ".$a_job->
getId().
" in class ".$a_class.
352 if(!is_object($ilSetting))
354 include_once
"Services/Administration/classes/class.ilSetting.php";
390 public static function updateFromXML($a_component, $a_id, $a_class, $a_path = null)
394 if(!$ilDB->tableExists(
"cron_job"))
413 public static function clearFromXML($a_component, array $a_xml_job_ids)
417 if(!$ilDB->tableExists(
"cron_job"))
424 $sql =
"SELECT job_id FROM cron_job".
425 " WHERE component = ".$ilDB->quote($a_component,
"text");
426 $set = $ilDB->query($sql);
427 while(
$row = $ilDB->fetchAssoc($set))
429 $all_jobs[] =
$row[
"job_id"];
432 if(
sizeof($all_jobs))
434 if(
sizeof($a_xml_job_ids))
437 foreach($all_jobs as $job_id)
439 if(!in_array($job_id, $a_xml_job_ids))
441 $ilDB->manipulate(
"DELETE FROM cron_job".
442 " WHERE component = ".$ilDB->quote($a_component,
"text").
443 " AND job_id = ".$ilDB->quote($job_id,
"text"));
445 $ilLog->write(
"Cron XML - Job ".$job_id.
" in class ".$a_component.
452 $ilDB->manipulate(
"DELETE FROM cron_job".
453 " WHERE component = ".$ilDB->quote($a_component,
"text"));
455 $ilLog->write(
"Cron XML - All jobs deleted for ".$a_component.
" as component is inactive.");
462 global $ilPluginAdmin;
466 foreach($ilPluginAdmin->getActivePluginsForSlot(
IL_COMP_SERVICE,
"Cron",
"crnhk") as $pl_name)
468 $plugin_obj = $ilPluginAdmin->getPluginObject(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name);
470 foreach((array)$plugin_obj->getCronJobInstances() as $job)
482 if(!$a_only_active ||
483 $item[
"job_status"] == 1)
485 $res[$job->getId()] = array($job, $item);
505 if($a_id && !is_array($a_id))
507 $a_id = array($a_id);
510 $sql =
"SELECT * FROM cron_job";
515 $where[] = $ilDB->in(
"job_id", $a_id,
"",
"text");
521 if(!$a_include_inactive)
523 $where[] =
"job_status = ".$ilDB->quote(1,
"integer");
527 $sql .=
" WHERE ".implode(
" AND ", $where);
531 $sql .=
" ORDER BY job_id";
533 $set = $ilDB->query($sql);
534 while(
$row = $ilDB->fetchAssoc($set))
551 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
554 $result->setCode(
"job_manual_reset");
555 $result->setMessage(
"Cron job re-activated by admin");
558 $ilDB->manipulate(
"UPDATE cron_job".
559 " SET running_ts = ".$ilDB->quote(0,
"integer").
560 " , alive_ts = ".$ilDB->quote(0,
"integer").
561 " , job_result_ts = ".$ilDB->quote(0,
"integer").
562 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text"));
577 $user_id = $a_manual ? $ilUser->getId() : 0;
579 $sql =
"UPDATE cron_job SET ".
580 " job_status = ".$ilDB->quote(1,
"integer").
581 " , job_status_user_id = ".$ilDB->quote($user_id,
"integer").
582 " , job_status_type = ".$ilDB->quote($a_manual,
"integer").
583 " , job_status_ts = ".$ilDB->quote(time(),
"integer").
584 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
585 $ilDB->manipulate($sql);
600 $user_id = $a_manual ? $ilUser->getId() : 0;
602 $sql =
"UPDATE cron_job SET ".
603 " job_status = ".$ilDB->quote(0,
"integer").
604 " , job_status_user_id = ".$ilDB->quote($user_id,
"integer").
605 " , job_status_type = ".$ilDB->quote($a_manual,
"integer").
606 " , job_status_ts = ".$ilDB->quote(time(),
"integer").
607 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
608 $ilDB->manipulate($sql);
622 if((
bool)$job[0][
"job_status"])
638 if(!(
bool)$job[0][
"job_status"])
656 $user_id = $a_manual ? $ilUser->getId() : 0;
658 $sql =
"UPDATE cron_job SET ".
659 " job_result_status = ".$ilDB->quote($a_result->
getStatus(),
"integer").
660 " , job_result_user_id = ".$ilDB->quote($user_id,
"integer").
661 " , job_result_code = ".$ilDB->quote($a_result->
getCode(),
"text").
662 " , job_result_message = ".$ilDB->quote($a_result->
getMessage(),
"text").
663 " , job_result_type = ".$ilDB->quote($a_manual,
"integer").
664 " , job_result_ts = ".$ilDB->quote(time(),
"integer").
665 " , job_result_dur = ".$ilDB->quote($a_result->
getDuration()*1000,
"integer").
666 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
667 $ilDB->manipulate($sql);
681 if($a_schedule_type === null ||
685 $sql =
"UPDATE cron_job SET ".
686 " schedule_type = ".$ilDB->quote($a_schedule_type,
"integer").
687 " , schedule_value = ".$ilDB->quote($a_schedule_value,
"integer").
688 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
689 $ilDB->manipulate($sql);
700 list($usec, $sec) = explode(
" ", microtime());
701 return ((
float)$usec + (
float)$sec);
709 public static function ping($a_job_id)
713 $ilDB->manipulate(
"UPDATE cron_job SET ".
714 " alive_ts = ".$ilDB->quote(time(),
"integer").
715 " WHERE job_id = ".$ilDB->quote($a_job_id,
"text"));