README.txt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. Job Scheduler
  2. =============
  3. Simple API for scheduling tasks once at a predetermined time or periodically at
  4. a fixed interval.
  5. Usage
  6. =====
  7. Declare scheduler.
  8. function example_cron_job_scheduler_info() {
  9. $schedulers = array();
  10. $schedulers['example_unpublish'] = array(
  11. 'worker callback' => 'example_unpublish_nodes',
  12. );
  13. return $schedulers;
  14. }
  15. Add a job.
  16. $job = array(
  17. 'type' => 'story',
  18. 'id' => 12,
  19. 'period' => 3600,
  20. 'periodic' => TRUE,
  21. );
  22. JobScheduler::get('example_unpublish')->set($job);
  23. Work off a job.
  24. function example_unpublish_nodes($job) {
  25. // Do stuff.
  26. }
  27. Remove a job.
  28. $job = array(
  29. 'type' => 'story',
  30. 'id' => 12,
  31. );
  32. JobScheduler::get('example_unpublish')->remove($job);
  33. Optionally jobs can declared together with a schedule in a hook_cron_job_scheduler_info().
  34. function example_cron_job_scheduler_info() {
  35. $schedulers = array();
  36. $schedulers['example_unpublish'] = array(
  37. 'worker callback' => 'example_unpublish_nodes',
  38. 'jobs' => array(
  39. array('type' => 'story', 'id' => 12, 'period' => 3600, 'periodic' => TRUE),
  40. )
  41. );
  42. return $schedulers;
  43. }
  44. Jobs can have a 'crontab' instead of a period. Crontab syntax are Unix-like formatted crontab lines.
  45. Example of job with crontab.
  46. // This will create a job that will be triggered from monday to friday, from january to july, every two hours
  47. function example_cron_job_scheduler_info() {
  48. $schedulers = array();
  49. $schedulers['example_unpublish'] = array(
  50. 'worker callback' => 'example_unpublish_nodes',
  51. 'jobs' => array(
  52. array('type' => 'story', 'id' => 12, 'crontab' => '0 */2 * january-july mon-fri', 'periodic' => TRUE),
  53. )
  54. );
  55. return $schedulers;
  56. }
  57. Read more about crontab syntax, http://linux.die.net/man/5/crontab
  58. Drupal Queue integration
  59. ========================
  60. Optionally, at the scheduled time Job Scheduler can queue a job for execution,
  61. rather than executing the job directly. This is useful when many jobs need to
  62. be executed or when the job's expected execution time is very long.
  63. More information on Drupal Queue: http://api.drupal.org/api/group/queue/7
  64. Instead of declaring a worker callback, declare a queue name.
  65. function example_cron_job_scheduler_info() {
  66. $schedulers = array();
  67. $schedulers['example_unpublish'] = array(
  68. 'queue name' => 'example_unpublish_queue',
  69. );
  70. return $schedulers;
  71. }
  72. This of course assumes that you have declared a queue. Notice how in this
  73. pattern the queue callback contains the actual worker callback.
  74. function example_cron_queue_info() {
  75. $schedulers = array();
  76. $schedulers['example_unpublish_queue'] = array(
  77. 'worker callback' => 'example_unpublish_nodes',
  78. );
  79. return $schedulers;
  80. }
  81. Work off a job: when using a queue, Job Scheduler reserves a job for one hour
  82. giving the queue time to work off a job before it reschedules it. This means
  83. that the worker callback needs to reset the job's schedule flag in order to
  84. allow renewed scheduling.
  85. function example_unpublish_nodes($job) {
  86. // Do stuff.
  87. // Set the job again so that its reserved flag is reset.
  88. JobScheduler::get('example_unpublish')->set($job);
  89. }
  90. Example
  91. =======
  92. See Feeds module.
  93. Hidden settings
  94. ===============
  95. Hidden settings are variables that you can define by adding them to the $conf
  96. array in your settings.php file.
  97. Name: 'job_scheduler_class_' . $name
  98. Default: 'JobScheduler'
  99. Description: The class to use for managing a particular schedule.