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)
36 $job = self::getJobInstanceById($row[
"job_id"]);
45 foreach(self::getPluginJobs(
true) as $item)
47 self::runJob($item[0], $item[1]);
50 $ilLog->write(
"CRON - batch end");
65 $ilLog->write(
"CRON - manual start (".$a_job_id.
")");
67 $job = self::getJobInstanceById($a_job_id);
70 if($job->isManuallyExecutable())
72 $result = self::runJob($job, null,
true);
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 = null, $a_manual =
false)
103 if($a_job_data === null)
106 $a_job_data = array_pop(self::getCronJobData($a_job->
getId()));
111 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
114 if($a_job_data[
"alive_ts"])
116 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" still running");
121 if(time()-$a_job_data[
"alive_ts"] > $cut)
123 $ilDB->manipulate(
"UPDATE cron_job SET".
124 " running_ts = ".$ilDB->quote(0,
"integer").
125 " , alive_ts = ".$ilDB->quote(0,
"integer").
126 " WHERE job_id = ".$ilDB->quote($a_job_data[
"job_id"],
"text"));
128 self::deactivateJob($a_job);
132 $result->setCode(
"job_auto_deactivation_time_limit");
133 $result->setMessage(
"Cron job deactivated because it has been inactive for 3 hours");
137 self::sendNotification($a_job,
$result);
140 self::updateJobResult($a_job,
$result, $a_manual);
142 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" deactivated (assumed crash)");
146 else if($a_job->
isActive($a_job_data[
"job_result_ts"],
147 $a_job_data[
"schedule_type"], $a_job_data[
"schedule_value"], $a_manual))
149 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" started");
151 $ilDB->manipulate(
"UPDATE cron_job SET".
152 " running_ts = ".$ilDB->quote(time(),
"integer").
153 " , alive_ts = ".$ilDB->quote(time(),
"integer").
154 " WHERE job_id = ".$ilDB->quote($a_job_data[
"job_id"],
"text"));
156 $ts_in = self::getMicrotime();
158 $ts_dur = self::getMicrotime()-$ts_in;
163 $result =
new ilCronJobResult();
165 $result->setCode(
"job_no_result");
166 $result->setMessage(
"Cron job did not return a proper result");
170 self::sendNotification($a_job,
$result);
173 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" no result");
178 self::deactivateJob($a_job);
182 self::sendNotification($a_job,
$result);
185 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" invalid configuration");
195 self::updateJobResult($a_job,
$result, $a_manual);
197 $ilDB->manipulate(
"UPDATE cron_job SET".
198 " running_ts = ".$ilDB->quote(0,
"integer").
199 " , alive_ts = ".$ilDB->quote(0,
"integer").
200 " WHERE job_id = ".$ilDB->quote($a_job_data[
"job_id"],
"text"));
202 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" finished");
206 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" returned status inactive");
221 global
$ilLog, $ilPluginAdmin;
224 if(substr($a_job_id, 0, 4) ==
"pl__")
226 $parts = explode(
"__", $a_job_id);
227 $pl_name = $parts[1];
229 if($ilPluginAdmin->isActive(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name))
232 "Cron",
"crnhk", $pl_name);
233 $job = $plugin_obj->getCronJobInstance($job_id);
251 $job_data = array_pop(self::getCronJobData($a_job_id));
252 if($job_data[
"job_id"] == $a_job_id)
254 return self::getJobInstance($job_data[
"job_id"], $job_data[
"component"],
255 $job_data[
"class"], $job_data[
"path"]);
259 $ilLog->write(
"CRON - job ".$a_job_id.
" seems invalid or is inactive");
270 public static function getJobInstance($a_id, $a_component, $a_class, $a_path = null)
276 $a_path = $a_component.
"/classes/";
278 $class_file = $a_path.
"class.".$a_class.
".php";
279 if(file_exists($class_file))
281 include_once $class_file;
282 if(class_exists($a_class))
284 $job =
new $a_class();
287 if($job->getId() == $a_id)
293 $mess .=
" - job id mismatch";
298 $mess .=
" - does not extend ilCronJob";
303 $mess =
"- class not found in file";
308 $mess =
" - class file not found";
311 $ilLog->write(
"Cron XML - Job ".$a_id.
" in class ".$a_class.
" (".
312 $class_file.
") is invalid.".$mess);
331 $sql =
"SELECT job_id, schedule_type FROM cron_job".
332 " WHERE component = ".$ilDB->quote($a_component,
"text").
333 " AND job_id = ".$ilDB->quote($a_job->
getId(),
"text");
334 $set = $ilDB->query($sql);
335 $row = $ilDB->fetchAssoc($set);
336 $job_exists = (
$row[
"job_id"] == $a_job->
getId());
337 $schedule_type =
$row[
"schedule_type"];
342 $sql =
"INSERT INTO cron_job (job_id, component, class, path)".
343 " VALUES (".$ilDB->quote($a_job->
getId(),
"text").
", ".
344 $ilDB->quote($a_component,
"text").
", ".
345 $ilDB->quote($a_class,
"text").
", ".
346 $ilDB->quote($a_path,
"text").
")";
347 $ilDB->manipulate($sql);
349 $ilLog->write(
"Cron XML - Job ".$a_job->
getId().
" in class ".$a_class.
353 self::updateJobSchedule($a_job,
358 if(!is_object($ilSetting))
360 include_once
"Services/Administration/classes/class.ilSetting.php";
366 self::activateJob($a_job);
377 self::updateJobSchedule($a_job,
384 self::updateJobSchedule($a_job, null, null);
396 public static function updateFromXML($a_component, $a_id, $a_class, $a_path = null)
400 if(!$ilDB->tableExists(
"cron_job"))
406 $job = self::getJobInstance($a_id, $a_component, $a_class, $a_path);
409 self::createDefaultEntry($job, $a_component, $a_class, $a_path);
419 public static function clearFromXML($a_component, array $a_xml_job_ids)
423 if(!$ilDB->tableExists(
"cron_job"))
430 $sql =
"SELECT job_id FROM cron_job".
431 " WHERE component = ".$ilDB->quote($a_component,
"text");
432 $set = $ilDB->query($sql);
433 while(
$row = $ilDB->fetchAssoc($set))
435 $all_jobs[] =
$row[
"job_id"];
438 if(
sizeof($all_jobs))
440 if(
sizeof($a_xml_job_ids))
443 foreach($all_jobs as $job_id)
445 if(!in_array($job_id, $a_xml_job_ids))
447 $ilDB->manipulate(
"DELETE FROM cron_job".
448 " WHERE component = ".$ilDB->quote($a_component,
"text").
449 " AND job_id = ".$ilDB->quote($job_id,
"text"));
451 $ilLog->write(
"Cron XML - Job ".$job_id.
" in class ".$a_component.
458 $ilDB->manipulate(
"DELETE FROM cron_job".
459 " WHERE component = ".$ilDB->quote($a_component,
"text"));
461 $ilLog->write(
"Cron XML - All jobs deleted for ".$a_component.
" as component is inactive.");
468 global $ilPluginAdmin;
472 foreach($ilPluginAdmin->getActivePluginsForSlot(
IL_COMP_SERVICE,
"Cron",
"crnhk") as $pl_name)
474 $plugin_obj = $ilPluginAdmin->getPluginObject(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name);
476 foreach((array)$plugin_obj->getCronJobInstances() as $job)
488 if(!$a_only_active ||
489 $item[
"job_status"] == 1)
491 $res[$job->getId()] = array($job, $item);
512 if($a_id && !is_array($a_id))
514 $a_id = array($a_id);
517 $sql =
"SELECT * FROM cron_job";
522 $where[] = $ilDB->in(
"job_id", $a_id,
"",
"text");
528 if(!$a_include_inactive)
530 $where[] =
"job_status = ".$ilDB->quote(1,
"integer");
534 $sql .=
" WHERE ".implode(
" AND ", $where);
538 $sql .=
" ORDER BY job_id";
540 $set = $ilDB->query($sql);
541 while(
$row = $ilDB->fetchAssoc($set))
558 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
561 $result->setCode(
"job_manual_reset");
562 $result->setMessage(
"Cron job re-activated by admin");
563 self::updateJobResult($a_job,
$result,
true);
565 $ilDB->manipulate(
"UPDATE cron_job".
566 " SET running_ts = ".$ilDB->quote(0,
"integer").
567 " , alive_ts = ".$ilDB->quote(0,
"integer").
568 " , job_result_ts = ".$ilDB->quote(0,
"integer").
569 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text"));
571 self::activateJob($a_job,
true);
584 $user_id = $a_manual ? $ilUser->getId() : 0;
586 $sql =
"UPDATE cron_job SET ".
587 " job_status = ".$ilDB->quote(1,
"integer").
588 " , job_status_user_id = ".$ilDB->quote($user_id,
"integer").
589 " , job_status_type = ".$ilDB->quote($a_manual,
"integer").
590 " , job_status_ts = ".$ilDB->quote(time(),
"integer").
591 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
592 $ilDB->manipulate($sql);
607 $user_id = $a_manual ? $ilUser->getId() : 0;
609 $sql =
"UPDATE cron_job SET ".
610 " job_status = ".$ilDB->quote(0,
"integer").
611 " , job_status_user_id = ".$ilDB->quote($user_id,
"integer").
612 " , job_status_type = ".$ilDB->quote($a_manual,
"integer").
613 " , job_status_ts = ".$ilDB->quote(time(),
"integer").
614 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
615 $ilDB->manipulate($sql);
628 $job = self::getCronJobData($a_job_id);
629 if((
bool)$job[0][
"job_status"])
644 $job = self::getCronJobData($a_job_id);
645 if(!(
bool)$job[0][
"job_status"])
663 $user_id = $a_manual ? $ilUser->getId() : 0;
665 $sql =
"UPDATE cron_job SET ".
666 " job_result_status = ".$ilDB->quote($a_result->
getStatus(),
"integer").
667 " , job_result_user_id = ".$ilDB->quote($user_id,
"integer").
668 " , job_result_code = ".$ilDB->quote($a_result->
getCode(),
"text").
669 " , job_result_message = ".$ilDB->quote($a_result->
getMessage(),
"text").
670 " , job_result_type = ".$ilDB->quote($a_manual,
"integer").
671 " , job_result_ts = ".$ilDB->quote(time(),
"integer").
672 " , job_result_dur = ".$ilDB->quote($a_result->
getDuration()*1000,
"integer").
673 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
674 $ilDB->manipulate($sql);
688 if($a_schedule_type === null ||
692 $sql =
"UPDATE cron_job SET ".
693 " schedule_type = ".$ilDB->quote($a_schedule_type,
"integer").
694 " , schedule_value = ".$ilDB->quote($a_schedule_value,
"integer").
695 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
696 $ilDB->manipulate($sql);
707 list($usec, $sec) = explode(
" ", microtime());
708 return ((
float)$usec + (
float)$sec);
716 public static function ping($a_job_id)
720 $ilDB->manipulate(
"UPDATE cron_job SET ".
721 " alive_ts = ".$ilDB->quote(time(),
"integer").
722 " 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 runActiveJobs()
Run all active jobs.
static updateFromXML($a_component, $a_id, $a_class, $a_path=null)
Process data from module.xml/service.xml.
static sendNotification(ilCronJob $a_job, $a_message)
Send notification to admin about job event(s)
getValidScheduleTypes()
Get all available schedule types.
const STATUS_INVALID_CONFIGURATION
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 runJobManual($a_job_id)
Run single job manually.
activationWasToggled($a_currently_active)
Cron job status was changed.
static resetJob(ilCronJob $a_job)
Reset job.
static getCronJobData($a_id=null, $a_include_inactive=true)
Get cron job configuration/execution data.
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.
static updateJobSchedule(ilCronJob $a_job, $a_schedule_type, $a_schedule_value)
Update job schedule.
Cron job result data container.
static isJobActive($a_job_id)
Check if given job is currently active.
hasAutoActivation()
Is to be activated on "installation".