23 $ilLog->write(
"CRON - batch start");
25 $ilSetting->set(
"last_cronjob_start_ts",
time());
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.
")");
103 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
105 if($a_job_data === null)
108 $a_job_data = array_pop(self::getCronJobData($a_job->
getId()));
112 if($a_job_data[
"alive_ts"])
114 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" still running");
119 if(
time()-$a_job_data[
"alive_ts"] > $cut)
121 $ilDB->manipulate(
"UPDATE cron_job SET".
122 " running_ts = ".$ilDB->quote(0,
"integer").
123 " , alive_ts = ".$ilDB->quote(0,
"integer").
124 " WHERE job_id = ".$ilDB->quote($a_job_data[
"job_id"],
"text"));
126 self::deactivateJob($a_job);
131 $result->setMessage(
"Cron job deactivated because it has been inactive for 3 hours");
135 self::sendNotification($a_job,
$result);
138 self::updateJobResult($a_job,
$result, $a_manual);
140 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" deactivated (assumed crash)");
144 else if($a_job->
isActive($a_job_data[
"job_result_ts"],
145 $a_job_data[
"schedule_type"], $a_job_data[
"schedule_value"], $a_manual))
147 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" started");
149 $ilDB->manipulate(
"UPDATE cron_job SET".
150 " running_ts = ".$ilDB->quote(
time(),
"integer").
151 " , alive_ts = ".$ilDB->quote(
time(),
"integer").
152 " WHERE job_id = ".$ilDB->quote($a_job_data[
"job_id"],
"text"));
154 $ts_in = self::getMicrotime();
156 $ts_dur = self::getMicrotime()-$ts_in;
161 $result =
new ilCronJobResult();
164 $result->setMessage(
"Cron job did not return a proper result");
168 self::sendNotification($a_job,
$result);
171 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" no result");
176 self::deactivateJob($a_job);
180 self::sendNotification($a_job,
$result);
183 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" invalid configuration");
193 self::updateJobResult($a_job,
$result, $a_manual);
195 $ilDB->manipulate(
"UPDATE cron_job SET".
196 " running_ts = ".$ilDB->quote(0,
"integer").
197 " , alive_ts = ".$ilDB->quote(0,
"integer").
198 " WHERE job_id = ".$ilDB->quote($a_job_data[
"job_id"],
"text"));
200 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" finished");
204 $ilLog->write(
"CRON - job ".$a_job_data[
"job_id"].
" returned status inactive");
218 global
$ilLog, $ilPluginAdmin;
221 if(substr($a_job_id, 0, 4) ==
"pl__")
223 $parts = explode(
"__", $a_job_id);
224 $pl_name = $parts[1];
226 if($ilPluginAdmin->isActive(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name))
229 "Cron",
"crnhk", $pl_name);
230 $job = $plugin_obj->getCronJobInstance($job_id);
248 $job_data = array_pop(self::getCronJobData($a_job_id));
249 if($job_data[
"job_id"] == $a_job_id)
251 return self::getJobInstance($job_data[
"job_id"], $job_data[
"component"],
252 $job_data[
"class"], $job_data[
"path"]);
256 $ilLog->write(
"CRON - job ".$a_job_id.
" seems invalid or is inactive");
267 public static function getJobInstance($a_id, $a_component, $a_class, $a_path = null)
273 $a_path = $a_component.
"/classes/";
275 $class_file = $a_path.
"class.".$a_class.
".php";
276 if(file_exists($class_file))
278 include_once $class_file;
279 if(class_exists($a_class))
281 $job =
new $a_class();
284 if($job->getId() == $a_id)
290 $mess .=
" - job id mismatch";
295 $mess .=
" - does not extend ilCronJob";
300 $mess =
"- class not found in file";
305 $mess =
" - class file not found";
308 $ilLog->write(
"Cron XML - Job ".$a_id.
" in class ".$a_class.
" (".
309 $class_file.
") is invalid.".$mess);
328 $sql =
"SELECT job_id, schedule_type FROM cron_job".
329 " WHERE component = ".$ilDB->quote($a_component,
"text").
330 " AND job_id = ".$ilDB->quote($a_job->
getId(),
"text");
331 $set = $ilDB->query($sql);
332 $row = $ilDB->fetchAssoc($set);
333 $job_exists = (
$row[
"job_id"] == $a_job->
getId());
334 $schedule_type =
$row[
"schedule_type"];
339 $sql =
"INSERT INTO cron_job (job_id, component, class, path)".
340 " VALUES (".$ilDB->quote($a_job->
getId(),
"text").
", ".
341 $ilDB->quote($a_component,
"text").
", ".
342 $ilDB->quote($a_class,
"text").
", ".
343 $ilDB->quote($a_path,
"text").
")";
344 $ilDB->manipulate($sql);
346 $ilLog->write(
"Cron XML - Job ".$a_job->
getId().
" in class ".$a_class.
350 self::updateJobSchedule($a_job,
355 if(!is_object($ilSetting))
357 include_once
"Services/Administration/classes/class.ilSetting.php";
363 self::activateJob($a_job);
374 self::updateJobSchedule($a_job,
381 self::updateJobSchedule($a_job, null, null);
393 public static function updateFromXML($a_component, $a_id, $a_class, $a_path = null)
397 if(!$ilDB->tableExists(
"cron_job"))
403 $job = self::getJobInstance($a_id, $a_component, $a_class, $a_path);
406 self::createDefaultEntry($job, $a_component, $a_class, $a_path);
420 if(!$ilDB->tableExists(
"cron_job"))
427 $sql =
"SELECT job_id FROM cron_job".
428 " WHERE component = ".$ilDB->quote($a_component,
"text");
429 $set = $ilDB->query($sql);
430 while(
$row = $ilDB->fetchAssoc($set))
432 $all_jobs[] =
$row[
"job_id"];
435 if(
sizeof($all_jobs))
437 if(
sizeof($a_xml_job_ids))
440 foreach($all_jobs as $job_id)
442 if(!in_array($job_id, $a_xml_job_ids))
444 $ilDB->manipulate(
"DELETE FROM cron_job".
445 " WHERE component = ".$ilDB->quote($a_component,
"text").
446 " AND job_id = ".$ilDB->quote($job_id,
"text"));
448 $ilLog->write(
"Cron XML - Job ".$job_id.
" in class ".$a_component.
455 $ilDB->manipulate(
"DELETE FROM cron_job".
456 " WHERE component = ".$ilDB->quote($a_component,
"text"));
458 $ilLog->write(
"Cron XML - All jobs deleted for ".$a_component.
" as component is inactive.");
465 global $ilPluginAdmin;
469 foreach($ilPluginAdmin->getActivePluginsForSlot(
IL_COMP_SERVICE,
"Cron",
"crnhk") as $pl_name)
471 $plugin_obj = $ilPluginAdmin->getPluginObject(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name);
473 foreach((
array)$plugin_obj->getCronJobInstances() as $job)
485 if(!$a_only_active ||
486 $item[
"job_status"] == 1)
488 $res[$job->getId()] =
array($job, $item);
509 if($a_id && !is_array($a_id))
511 $a_id =
array($a_id);
514 $sql =
"SELECT * FROM cron_job";
519 $where[] = $ilDB->in(
"job_id", $a_id,
"",
"text");
525 if(!$a_include_inactive)
527 $where[] =
"job_status = ".$ilDB->quote(1,
"integer");
531 $sql .=
" WHERE ".implode(
" AND ", $where);
535 $sql .=
" ORDER BY job_id";
537 $set = $ilDB->query($sql);
538 while(
$row = $ilDB->fetchAssoc($set))
555 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
559 $result->setMessage(
"Cron job re-activated by admin");
560 self::updateJobResult($a_job,
$result,
true);
562 $ilDB->manipulate(
"UPDATE cron_job".
563 " SET running_ts = ".$ilDB->quote(0,
"integer").
564 " , alive_ts = ".$ilDB->quote(0,
"integer").
565 " , job_result_ts = ".$ilDB->quote(0,
"integer").
566 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text"));
568 self::activateJob($a_job,
true);
581 $user_id = $a_manual ? $ilUser->getId() : 0;
583 $sql =
"UPDATE cron_job SET ".
584 " job_status = ".$ilDB->quote(1,
"integer").
585 " , job_status_user_id = ".$ilDB->quote($user_id,
"integer").
586 " , job_status_type = ".$ilDB->quote($a_manual,
"integer").
587 " , job_status_ts = ".$ilDB->quote(
time(),
"integer").
588 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
589 $ilDB->manipulate($sql);
604 $user_id = $a_manual ? $ilUser->getId() : 0;
606 $sql =
"UPDATE cron_job SET ".
607 " job_status = ".$ilDB->quote(0,
"integer").
608 " , job_status_user_id = ".$ilDB->quote($user_id,
"integer").
609 " , job_status_type = ".$ilDB->quote($a_manual,
"integer").
610 " , job_status_ts = ".$ilDB->quote(
time(),
"integer").
611 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
612 $ilDB->manipulate($sql);
625 $job = self::getCronJobData($a_job_id);
626 if((
bool)$job[0][
"job_status"])
641 $job = self::getCronJobData($a_job_id);
642 if(!(
bool)$job[0][
"job_status"])
660 $user_id = $a_manual ? $ilUser->getId() : 0;
662 $sql =
"UPDATE cron_job SET ".
663 " job_result_status = ".$ilDB->quote($a_result->
getStatus(),
"integer").
664 " , job_result_user_id = ".$ilDB->quote($user_id,
"integer").
665 " , job_result_code = ".$ilDB->quote($a_result->
getCode(),
"text").
666 " , job_result_message = ".$ilDB->quote($a_result->
getMessage(),
"text").
667 " , job_result_type = ".$ilDB->quote($a_manual,
"integer").
668 " , job_result_ts = ".$ilDB->quote(
time(),
"integer").
669 " , job_result_dur = ".$ilDB->quote($a_result->
getDuration()*1000,
"integer").
670 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
671 $ilDB->manipulate($sql);
685 if($a_schedule_type === null ||
689 $sql =
"UPDATE cron_job SET ".
690 " schedule_type = ".$ilDB->quote($a_schedule_type,
"integer").
691 " , schedule_value = ".$ilDB->quote($a_schedule_value,
"integer").
692 " WHERE job_id = ".$ilDB->quote($a_job->
getId(),
"text");
693 $ilDB->manipulate($sql);
704 list($usec, $sec) = explode(
" ", microtime());
705 return ((
float)$usec + (
float)$sec);
713 public static function ping($a_job_id)
717 $ilDB->manipulate(
"UPDATE cron_job SET ".
718 " alive_ts = ".$ilDB->quote(
time(),
"integer").
719 " 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.
const CODE_SUPPOSED_CRASH
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?
Create styles array
The data for the language used.
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.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
Cron job result data container.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
static isJobActive($a_job_id)
Check if given job is currently active.
hasAutoActivation()
Is to be activated on "installation".