23 $ilLog->write(
"CRON - batch start");
26 $ilSetting->set(
"last_cronjob_start_ts", $ts);
32 'Verification of last run datetime (read from database): %s',
40 if (!
defined(
"ILIAS_HTTP_PATH")) {
45 foreach (self::getCronJobData(null,
false) as
$row) {
46 $job = self::getJobInstanceById($row[
"job_id"]);
54 foreach (self::getPluginJobs(
true) as $item) {
55 self::runJob($item[0], $item[1]);
58 $ilLog->write(
"CRON - batch end");
73 $ilLog->write(
"CRON - manual start (" . $a_job_id .
")");
75 $job = self::getJobInstanceById($a_job_id);
77 if ($job->isManuallyExecutable()) {
78 $result = self::runJob($job, null,
true);
80 $ilLog->write(
"CRON - job " . $a_job_id .
" is not intended to be executed manually");
83 $ilLog->write(
"CRON - job " . $a_job_id .
" seems invalid or is inactive");
86 $ilLog->write(
"CRON - manual end (" . $a_job_id .
")");
105 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
107 if ($a_job_data === null) {
109 $a_job_data = array_pop(self::getCronJobData($a_job->
getId()));
113 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) {
120 $ilDB->manipulate(
"UPDATE cron_job SET" .
121 " running_ts = " . $ilDB->quote(0,
"integer") .
122 " , alive_ts = " . $ilDB->quote(0,
"integer") .
123 " WHERE job_id = " . $ilDB->quote($a_job_data[
"job_id"],
"text"));
125 self::deactivateJob($a_job);
130 $result->setMessage(
"Cron job deactivated because it has been inactive for 3 hours");
133 self::sendNotification($a_job,
$result);
136 self::updateJobResult($a_job,
$result, $a_manual);
138 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" deactivated (assumed crash)");
143 $a_job_data[
"job_result_ts"],
144 $a_job_data[
"schedule_type"],
145 $a_job_data[
"schedule_value"],
148 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" started");
150 $ilDB->manipulate(
"UPDATE cron_job SET" .
151 " running_ts = " . $ilDB->quote(
time(),
"integer") .
152 " , alive_ts = " . $ilDB->quote(
time(),
"integer") .
153 " WHERE job_id = " . $ilDB->quote($a_job_data[
"job_id"],
"text"));
155 $ts_in = self::getMicrotime();
159 $result = new \ilCronJobResult();
161 $result->setMessage(sprintf(
"Exception: %s", $e->getMessage()));
163 $ilLog->error($e->getMessage());
164 $ilLog->error($e->getTraceAsString());
166 $result = new \ilCronJobResult();
168 $result->setMessage(sprintf(
"Exception: %s", $e->getMessage()));
170 $ilLog->error($e->getMessage());
171 $ilLog->error($e->getTraceAsString());
173 $ts_dur = self::getMicrotime()-$ts_in;
177 $result =
new ilCronJobResult();
180 $result->setMessage(
"Cron job did not return a proper result");
183 self::sendNotification($a_job,
$result);
186 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" no result");
190 self::deactivateJob($a_job);
193 self::sendNotification($a_job,
$result);
196 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" invalid configuration");
205 self::updateJobResult($a_job,
$result, $a_manual);
207 $ilDB->manipulate(
"UPDATE cron_job SET" .
208 " running_ts = " . $ilDB->quote(0,
"integer") .
209 " , alive_ts = " . $ilDB->quote(0,
"integer") .
210 " WHERE job_id = " . $ilDB->quote($a_job_data[
"job_id"],
"text"));
212 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" finished");
214 $ilLog->write(
"CRON - job " . $a_job_data[
"job_id"] .
" returned status inactive");
228 global
$ilLog, $ilPluginAdmin;
231 if (substr($a_job_id, 0, 4) ==
"pl__") {
232 $parts = explode(
"__", $a_job_id);
233 $pl_name = $parts[1];
235 if ($ilPluginAdmin->isActive(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name)) {
236 $plugin_obj = $ilPluginAdmin->getPluginObject(
242 $job = $plugin_obj->getCronJobInstance($job_id);
257 $job_data = array_pop(self::getCronJobData($a_job_id));
258 if ($job_data[
"job_id"] == $a_job_id) {
259 return self::getJobInstance(
261 $job_data[
"component"],
268 $ilLog->write(
"CRON - job " . $a_job_id .
" seems invalid or is inactive");
279 public static function getJobInstance($a_id, $a_component, $a_class, $a_path = null)
284 $a_path = $a_component .
"/classes/";
286 $class_file = $a_path .
"class." . $a_class .
".php";
287 if (file_exists($class_file)) {
288 include_once $class_file;
289 if (class_exists($a_class)) {
290 $job =
new $a_class();
292 if ($job->getId() == $a_id) {
295 $mess .=
" - job id mismatch";
298 $mess .=
" - does not extend ilCronJob";
301 $mess =
"- class not found in file";
304 $mess =
" - class file not found";
307 $ilLog->write(
"Cron XML - Job " . $a_id .
" in class " . $a_class .
" (" .
308 $class_file .
") is invalid." . $mess);
327 $sql =
"SELECT job_id, schedule_type FROM cron_job" .
328 " WHERE component = " . $ilDB->quote($a_component,
"text") .
329 " AND job_id = " . $ilDB->quote($a_job->
getId(),
"text");
330 $set = $ilDB->query($sql);
331 $row = $ilDB->fetchAssoc($set);
332 $job_exists = (
$row[
"job_id"] == $a_job->
getId());
333 $schedule_type =
$row[
"schedule_type"];
337 $sql =
"INSERT INTO cron_job (job_id, component, class, path)" .
338 " VALUES (" . $ilDB->quote($a_job->
getId(),
"text") .
", " .
339 $ilDB->quote($a_component,
"text") .
", " .
340 $ilDB->quote($a_class,
"text") .
", " .
341 $ilDB->quote($a_path,
"text") .
")";
342 $ilDB->manipulate($sql);
344 $ilLog->write(
"Cron XML - Job " . $a_job->
getId() .
" in class " . $a_class .
348 self::updateJobSchedule(
355 if (!is_object($ilSetting)) {
356 include_once
"Services/Administration/classes/class.ilSetting.php";
361 self::activateJob($a_job);
369 self::updateJobSchedule(
377 self::updateJobSchedule($a_job, null, null);
389 public static function updateFromXML($a_component, $a_id, $a_class, $a_path = null)
393 if (!$ilDB->tableExists(
"cron_job")) {
398 $job = self::getJobInstance($a_id, $a_component, $a_class, $a_path);
400 self::createDefaultEntry($job, $a_component, $a_class, $a_path);
414 if (!$ilDB->tableExists(
"cron_job")) {
420 $sql =
"SELECT job_id FROM cron_job" .
421 " WHERE component = " . $ilDB->quote($a_component,
"text");
422 $set = $ilDB->query($sql);
423 while (
$row = $ilDB->fetchAssoc($set)) {
424 $all_jobs[] =
$row[
"job_id"];
427 if (
sizeof($all_jobs)) {
428 if (
sizeof($a_xml_job_ids)) {
430 foreach ($all_jobs as $job_id) {
431 if (!in_array($job_id, $a_xml_job_ids)) {
432 $ilDB->manipulate(
"DELETE FROM cron_job" .
433 " WHERE component = " . $ilDB->quote($a_component,
"text") .
434 " AND job_id = " . $ilDB->quote($job_id,
"text"));
436 $ilLog->write(
"Cron XML - Job " . $job_id .
" in class " . $a_component .
441 $ilDB->manipulate(
"DELETE FROM cron_job" .
442 " WHERE component = " . $ilDB->quote($a_component,
"text"));
444 $ilLog->write(
"Cron XML - All jobs deleted for " . $a_component .
" as component is inactive.");
451 global $ilPluginAdmin;
455 foreach ($ilPluginAdmin->getActivePluginsForSlot(
IL_COMP_SERVICE,
"Cron",
"crnhk") as $pl_name) {
456 $plugin_obj = $ilPluginAdmin->getPluginObject(
IL_COMP_SERVICE,
"Cron",
"crnhk", $pl_name);
458 foreach ((
array) $plugin_obj->getCronJobInstances() as $job) {
460 if (!is_array($item) || 0 === count($item)) {
468 if (!$a_only_active ||
469 $item[
"job_status"] == 1) {
470 $res[$job->getId()] =
array($job, $item);
491 if ($a_id && !is_array($a_id)) {
492 $a_id =
array($a_id);
495 $sql =
"SELECT * FROM cron_job";
499 $where[] = $ilDB->in(
"job_id", $a_id,
"",
"text");
503 if (!$a_include_inactive) {
504 $where[] =
"job_status = " . $ilDB->quote(1,
"integer");
506 if (
sizeof($where)) {
507 $sql .=
" WHERE " . implode(
" AND ", $where);
511 $sql .=
" ORDER BY job_id";
513 $set = $ilDB->query($sql);
514 while (
$row = $ilDB->fetchAssoc($set)) {
530 include_once
"Services/Cron/classes/class.ilCronJobResult.php";
534 $result->setMessage(
"Cron job re-activated by admin");
535 self::updateJobResult($a_job,
$result,
true);
537 $ilDB->manipulate(
"UPDATE cron_job" .
538 " SET running_ts = " . $ilDB->quote(0,
"integer") .
539 " , alive_ts = " . $ilDB->quote(0,
"integer") .
540 " , job_result_ts = " . $ilDB->quote(0,
"integer") .
541 " WHERE job_id = " . $ilDB->quote($a_job->
getId(),
"text"));
543 self::activateJob($a_job,
true);
556 $user_id = $a_manual ? $ilUser->getId() : 0;
558 $sql =
"UPDATE cron_job SET " .
559 " job_status = " . $ilDB->quote(1,
"integer") .
560 " , job_status_user_id = " . $ilDB->quote($user_id,
"integer") .
561 " , job_status_type = " . $ilDB->quote($a_manual,
"integer") .
562 " , job_status_ts = " . $ilDB->quote(
time(),
"integer") .
563 " WHERE job_id = " . $ilDB->quote($a_job->
getId(),
"text");
564 $ilDB->manipulate($sql);
579 $user_id = $a_manual ? $ilUser->getId() : 0;
581 $sql =
"UPDATE cron_job SET " .
582 " job_status = " . $ilDB->quote(0,
"integer") .
583 " , job_status_user_id = " . $ilDB->quote($user_id,
"integer") .
584 " , job_status_type = " . $ilDB->quote($a_manual,
"integer") .
585 " , job_status_ts = " . $ilDB->quote(
time(),
"integer") .
586 " WHERE job_id = " . $ilDB->quote($a_job->
getId(),
"text");
587 $ilDB->manipulate($sql);
600 $job = self::getCronJobData($a_job_id);
601 if ((
bool) $job[0][
"job_status"]) {
615 $job = self::getCronJobData($a_job_id);
616 if (!(
bool) $job[0][
"job_status"]) {
633 $user_id = $a_manual ? $ilUser->getId() : 0;
635 $sql =
"UPDATE cron_job SET " .
636 " job_result_status = " . $ilDB->quote($a_result->
getStatus(),
"integer") .
637 " , job_result_user_id = " . $ilDB->quote($user_id,
"integer") .
638 " , job_result_code = " . $ilDB->quote($a_result->
getCode(),
"text") .
639 " , job_result_message = " . $ilDB->quote($a_result->
getMessage(),
"text") .
640 " , job_result_type = " . $ilDB->quote($a_manual,
"integer") .
641 " , job_result_ts = " . $ilDB->quote(
time(),
"integer") .
642 " , job_result_dur = " . $ilDB->quote($a_result->
getDuration()*1000,
"integer") .
643 " WHERE job_id = " . $ilDB->quote($a_job->
getId(),
"text");
644 $ilDB->manipulate($sql);
658 if ($a_schedule_type === null ||
661 $sql =
"UPDATE cron_job SET " .
662 " schedule_type = " . $ilDB->quote($a_schedule_type,
"integer") .
663 " , schedule_value = " . $ilDB->quote($a_schedule_value,
"integer") .
664 " WHERE job_id = " . $ilDB->quote($a_job->
getId(),
"text");
665 $ilDB->manipulate($sql);
676 list($usec, $sec) = explode(
" ", microtime());
677 return ((
float) $usec + (
float) $sec);
685 public static function ping($a_job_id)
689 $ilDB->manipulate(
"UPDATE cron_job SET " .
690 " alive_ts = " . $ilDB->quote(
time(),
"integer") .
691 " 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 formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false)
Format a date public.
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 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?
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".