ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCronManager.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
12 {
16  public static function runActiveJobs()
17  {
18  global $ilLog, $ilSetting;
19 
20  // separate log for cron
21  // $this->log->setFilename($_COOKIE["ilClientId"]."_cron.txt");
22 
23  $ilLog->write("CRON - batch start");
24 
25  $ilSetting->set("last_cronjob_start_ts", time());
26 
27  // ilLink::_getStaticLink() should work in crons
28  if(!defined("ILIAS_HTTP_PATH"))
29  {
30  define("ILIAS_HTTP_PATH", ilUtil::_getHttpPath());
31  }
32 
33  // system
34  foreach(self::getCronJobData(null, false) as $row)
35  {
36  $job = self::getJobInstanceById($row["job_id"]);
37  if($job)
38  {
39  self::runJob($job, $row);
40  }
41  }
42 
43  // plugins
44  foreach(self::getPluginJobs(true) as $item)
45  {
46  self::runJob($item[0], $item[1]);
47  }
48 
49  $ilLog->write("CRON - batch end");
50  }
51 
58  public static function runJobManual($a_job_id)
59  {
60  global $ilLog;
61 
62  $result = false;
63 
64  $ilLog->write("CRON - manual start (".$a_job_id.")");
65 
66  $job = self::getJobInstanceById($a_job_id);
67  if($job)
68  {
69  if($job->isManuallyExecutable())
70  {
71  $job_data = array_pop(self::getCronJobData($job->getId()));
72  $result = self::runJob($job, $job_data, true);
73  }
74  else
75  {
76  $ilLog->write("CRON - job ".$a_job_id." is not intended to be executed manually");
77  }
78  }
79  else
80  {
81  $ilLog->write("CRON - job ".$a_job_id." seems invalid or is inactive");
82  }
83 
84  $ilLog->write("CRON - manual end (".$a_job_id.")");
85 
86  return $result;
87  }
88 
97  protected static function runJob(ilCronJob $a_job, array $a_job_data, $a_manual = false)
98  {
99  global $ilLog, $ilDB;
100 
101  $did_run = false;
102 
103  if($a_job)
104  {
105  include_once "Services/Cron/classes/class.ilCronJobResult.php";
106 
107  // already running?
108  if($a_job_data["alive_ts"])
109  {
110  $ilLog->write("CRON - job ".$a_job_data["job_id"]." still running");
111 
112  $cut = 60*60*3; // 3h
113 
114  // is running (and has not pinged) for 3 hours straight, we assume it crashed
115  if(time()-$a_job_data["alive_ts"] > $cut)
116  {
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"));
121 
122  self::deactivateJob($a_job); // #13082
123 
124  $result = new ilCronJobResult();
126  $result->setCode("job_auto_deactivation_time_limit");
127  $result->setMessage("Cron job deactivated because it has been inactive for 3 hours");
128 
129  if(!$a_manual)
130  {
132  }
133 
134  self::updateJobResult($a_job, $result, $a_manual);
135 
136  $ilLog->write("CRON - job ".$a_job_data["job_id"]." deactivated (assumed crash)");
137  }
138  }
139  // initiate run?
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))
142  {
143  $ilLog->write("CRON - job ".$a_job_data["job_id"]." started");
144 
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"));
149 
150  $ts_in = self::getMicrotime();
151  $result = $a_job->run();
152  $ts_dur = self::getMicrotime()-$ts_in;
153 
154  // no proper result
155  if(!$result instanceof ilCronJobResult)
156  {
157  $result = new ilCronJobResult();
159  $result->setCode("job_no_result");
160  $result->setMessage("Cron job did not return a proper result");
161 
162  if(!$a_manual)
163  {
165  }
166 
167  $ilLog->write("CRON - job ".$a_job_data["job_id"]." no result");
168  }
169  // no valid configuration, job won't work
171  {
172  self::deactivateJob($a_job);
173 
174  if(!$a_manual)
175  {
177  }
178 
179  $ilLog->write("CRON - job ".$a_job_data["job_id"]." invalid configuration");
180  }
181  // success!
182  else
183  {
184  $did_run = true;
185  }
186 
187  $result->setDuration($ts_dur);
188 
189  self::updateJobResult($a_job, $result, $a_manual);
190 
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"));
195 
196  $ilLog->write("CRON - job ".$a_job_data["job_id"]." finished");
197  }
198  else
199  {
200  $ilLog->write("CRON - job ".$a_job_data["job_id"]." returned status inactive");
201  }
202  }
203 
204  return $did_run;
205  }
206 
213  public static function getJobInstanceById($a_job_id)
214  {
215  global $ilLog, $ilPluginAdmin;
216 
217  // plugin
218  if(substr($a_job_id, 0, 4) == "pl__")
219  {
220  $parts = explode("__", $a_job_id);
221  $pl_name = $parts[1];
222  $job_id = $parts[2];
223  if($ilPluginAdmin->isActive(IL_COMP_SERVICE, "Cron", "crnhk", $pl_name))
224  {
225  $plugin_obj = $ilPluginAdmin->getPluginObject(IL_COMP_SERVICE,
226  "Cron", "crnhk", $pl_name);
227  $job = $plugin_obj->getCronJobInstance($job_id);
228  if($job instanceof ilCronJob)
229  {
230  // should never happen but who knows...
231  if(!sizeof(ilCronManager::getCronJobData($job_id)))
232  {
233  // as job is not "imported" from xml
235  }
236  return $job;
237  }
238  }
239 
240  return null;
241  }
242  // system
243  else
244  {
245  $job_data = array_pop(self::getCronJobData($a_job_id));
246  if($job_data["job_id"] == $a_job_id)
247  {
248  return self::getJobInstance($job_data["job_id"], $job_data["component"],
249  $job_data["class"], $job_data["path"]);
250  }
251  }
252 
253  $ilLog->write("CRON - job ".$a_job_id." seems invalid or is inactive");
254  }
255 
264  public static function getJobInstance($a_id, $a_component, $a_class, $a_path = null)
265  {
266  global $ilLog;
267 
268  if(!$a_path)
269  {
270  $a_path = $a_component."/classes/";
271  }
272  $class_file = $a_path."class.".$a_class.".php";
273  if(file_exists($class_file))
274  {
275  include_once $class_file;
276  if(class_exists($a_class))
277  {
278  $job = new $a_class();
279  if($job instanceof ilCronJob)
280  {
281  if($job->getId() == $a_id)
282  {
283  return $job;
284  }
285  else
286  {
287  $mess .= " - job id mismatch";
288  }
289  }
290  else
291  {
292  $mess .= " - does not extend ilCronJob";
293  }
294  }
295  else
296  {
297  $mess = "- class not found in file";
298  }
299  }
300  else
301  {
302  $mess = " - class file not found";
303  }
304 
305  $ilLog->write("Cron XML - Job ".$a_id." in class ".$a_class." (".
306  $class_file.") is invalid.".$mess);
307  }
308 
315  protected static function sendNotification(ilCronJob $a_job, $a_message)
316  {
317  // :TODO:
318  }
319 
320  public static function createDefaultEntry(ilCronJob $a_job, $a_component, $a_class, $a_path)
321  {
322  global $ilDB, $ilLog, $ilSetting;
323 
324  // already exists?
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"];
332 
333  // new job
334  if(!$job_exists)
335  {
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);
342 
343  $ilLog->write("Cron XML - Job ".$a_job->getId()." in class ".$a_class.
344  " added.");
345 
346  // only if flexible
347  self::updateJobSchedule($a_job,
348  $a_job->getDefaultScheduleType(),
349  $a_job->getDefaultScheduleValue());
350 
351  // #12221
352  if(!is_object($ilSetting))
353  {
354  include_once "Services/Administration/classes/class.ilSetting.php";
355  $ilSetting = new ilSetting();
356  }
357 
358  if($a_job->hasAutoActivation())
359  {
360  self::activateJob($a_job);
361  }
362  else
363  {
364  // to overwrite dependent settings
365  $a_job->activationWasToggled(false);
366  }
367  }
368  // existing job - but schedule is flexible now
369  else if($a_job->hasFlexibleSchedule() && !$schedule_type)
370  {
371  self::updateJobSchedule($a_job,
372  $a_job->getDefaultScheduleType(),
373  $a_job->getDefaultScheduleValue());
374  }
375  // existing job - but schedule is static now
376  else if(!$a_job->hasFlexibleSchedule() && $schedule_type)
377  {
378  self::updateJobSchedule($a_job, null, null);
379  }
380  }
381 
390  public static function updateFromXML($a_component, $a_id, $a_class, $a_path = null)
391  {
392  global $ilDB;
393 
394  if(!$ilDB->tableExists("cron_job"))
395  {
396  return;
397  }
398 
399  // only if job seems valid
400  $job = self::getJobInstance($a_id, $a_component, $a_class, $a_path);
401  if($job)
402  {
403  self::createDefaultEntry($job, $a_component, $a_class, $a_path);
404  }
405  }
406 
413  public static function clearFromXML($a_component, array $a_xml_job_ids)
414  {
415  global $ilDB, $ilLog;
416 
417  if(!$ilDB->tableExists("cron_job"))
418  {
419  return;
420  }
421 
422  // gather existing jobs
423  $all_jobs = array();
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))
428  {
429  $all_jobs[] = $row["job_id"];
430  }
431 
432  if(sizeof($all_jobs))
433  {
434  if(sizeof($a_xml_job_ids))
435  {
436  // delete obsolete job data
437  foreach($all_jobs as $job_id)
438  {
439  if(!in_array($job_id, $a_xml_job_ids))
440  {
441  $ilDB->manipulate("DELETE FROM cron_job".
442  " WHERE component = ".$ilDB->quote($a_component, "text").
443  " AND job_id = ".$ilDB->quote($job_id, "text"));
444 
445  $ilLog->write("Cron XML - Job ".$job_id." in class ".$a_component.
446  " deleted.");
447  }
448  }
449  }
450  else
451  {
452  $ilDB->manipulate("DELETE FROM cron_job".
453  " WHERE component = ".$ilDB->quote($a_component, "text"));
454 
455  $ilLog->write("Cron XML - All jobs deleted for ".$a_component." as component is inactive.");
456  }
457  }
458  }
459 
460  public static function getPluginJobs($a_only_active = false)
461  {
462  global $ilPluginAdmin;
463 
464  $res = array();
465 
466  foreach($ilPluginAdmin->getActivePluginsForSlot(IL_COMP_SERVICE, "Cron", "crnhk") as $pl_name)
467  {
468  $plugin_obj = $ilPluginAdmin->getPluginObject(IL_COMP_SERVICE, "Cron", "crnhk", $pl_name);
469 
470  foreach((array)$plugin_obj->getCronJobInstances() as $job)
471  {
472  $item = array_pop(ilCronManager::getCronJobData($job->getId()));
473  if(!sizeof($item))
474  {
475  // as job is not "imported" from xml
477  }
478 
479  $item = array_pop(ilCronManager::getCronJobData($job->getId()));
480  }
481 
482  if(!$a_only_active ||
483  $item["job_status"] == 1)
484  {
485  $res[$job->getId()] = array($job, $item);
486  }
487  }
488 
489  return $res;
490  }
491 
499  public static function getCronJobData($a_id = null, $a_include_inactive = true)
500  {
501  global $ilDB;
502 
503  $res = array();
504 
505  if($a_id && !is_array($a_id))
506  {
507  $a_id = array($a_id);
508  }
509 
510  $sql = "SELECT * FROM cron_job";
511 
512  $where = array();
513  if($a_id)
514  {
515  $where[] = $ilDB->in("job_id", $a_id, "", "text");
516  }
517  else
518  {
519  $where[] = "class <> ".$ilDB->quote(IL_COMP_PLUGIN, "text");
520  }
521  if(!$a_include_inactive)
522  {
523  $where[] = "job_status = ".$ilDB->quote(1, "integer");
524  }
525  if(sizeof($where))
526  {
527  $sql .= " WHERE ".implode(" AND ", $where);
528  }
529 
530  // :TODO: discuss job execution order
531  $sql .= " ORDER BY job_id";
532 
533  $set = $ilDB->query($sql);
534  while($row = $ilDB->fetchAssoc($set))
535  {
536  $res[] = $row;
537  }
538 
539  return $res;
540  }
541 
547  public static function resetJob(ilCronJob $a_job)
548  {
549  global $ilDB;
550 
551  include_once "Services/Cron/classes/class.ilCronJobResult.php";
552  $result = new ilCronJobResult();
554  $result->setCode("job_manual_reset");
555  $result->setMessage("Cron job re-activated by admin");
556  self::updateJobResult($a_job, $result, true);
557 
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"));
563 
564  self::activateJob($a_job, true);
565  }
566 
573  public static function activateJob(ilCronJob $a_job, $a_manual = false)
574  {
575  global $ilDB, $ilUser;
576 
577  $user_id = $a_manual ? $ilUser->getId() : 0;
578 
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);
586 
587  $a_job->activationWasToggled(true);
588  }
589 
596  public static function deactivateJob(ilCronJob $a_job, $a_manual = false)
597  {
598  global $ilDB, $ilUser;
599 
600  $user_id = $a_manual ? $ilUser->getId() : 0;
601 
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);
609 
610  $a_job->activationWasToggled(false);
611  }
612 
619  public static function isJobActive($a_job_id)
620  {
621  $job = self::getCronJobData($a_job_id);
622  if((bool)$job[0]["job_status"])
623  {
624  return true;
625  }
626  return false;
627  }
628 
635  public static function isJobInactive($a_job_id)
636  {
637  $job = self::getCronJobData($a_job_id);
638  if(!(bool)$job[0]["job_status"])
639  {
640  return true;
641  }
642  return false;
643  }
644 
652  protected static function updateJobResult(ilCronJob $a_job, ilCronJobResult $a_result, $a_manual = false)
653  {
654  global $ilDB, $ilUser;
655 
656  $user_id = $a_manual ? $ilUser->getId() : 0;
657 
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);
668  }
669 
677  public static function updateJobSchedule(ilCronJob $a_job, $a_schedule_type, $a_schedule_value)
678  {
679  global $ilDB;
680 
681  if($a_schedule_type === null ||
682  ($a_job->hasFlexibleSchedule() &&
683  in_array($a_schedule_type, $a_job->getValidScheduleTypes())))
684  {
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);
690  }
691  }
692 
698  protected static function getMicrotime()
699  {
700  list($usec, $sec) = explode(" ", microtime());
701  return ((float)$usec + (float)$sec);
702  }
703 
709  public static function ping($a_job_id)
710  {
711  global $ilDB;
712 
713  $ilDB->manipulate("UPDATE cron_job SET ".
714  " alive_ts = ".$ilDB->quote(time(), "integer").
715  " WHERE job_id = ".$ilDB->quote($a_job_id, "text"));
716  }
717 }
718 
719 ?>