You are here

README.txt in Job Scheduler 8.3

Same filename and directory in other branches
  1. 8.2 README.txt
  2. 6 README.txt
  3. 7.2 README.txt
  4. 7 README.txt
Job Scheduler
=============

Simple API for scheduling tasks once at a predetermined time or periodically at
a fixed interval.


Usage
=====

Declare scheduler.

  function example_cron_job_scheduler_info() {
    $schedulers = [];
    $schedulers['example_unpublish'] = [
      'worker callback' => 'example_unpublish_nodes',
    ];
    return $schedulers;
  }

Add a job.

  $job = [
    'name' => 'example_unpublish',
    'type' => 'story',
    'id' => 12,
    'period' => 3600,
    'periodic' => TRUE,
  ];
  $service = \Drupal::service('job_scheduler.manager');
  $service->set($job);

Work off a job.

  function example_unpublish_nodes(\Drupal\job_scheduler\Entity\JobSchedule $job) {
    // Do stuff.
  }

Remove a job.

  $job = [
    'name' => 'example_unpublish',
    'type' => 'story',
    'id' => 12,
  ];
  $service = \Drupal::service('job_scheduler.manager');
  $service->remove($job);

Optionally jobs can declared together with a schedule in a hook_cron_job_scheduler_info().

  function example_cron_job_scheduler_info() {
    $schedulers = [];
    $schedulers['example_unpublish'] = [
      'worker callback' => 'example_unpublish_nodes',
      'jobs' => [
         ['type' => 'story', 'id' => 12, 'period' => 3600, 'periodic' => TRUE],
      ],
    ];
    return $schedulers;
  }

Jobs can have a 'crontab' instead of a period. Crontab syntax are Unix-like formatted crontab lines.
Example of job with crontab.

  // This will create a job that will be triggered from monday to friday, from january to july, every two hours
  function example_cron_job_scheduler_info() {
    $schedulers = [];
    $schedulers['example_unpublish'] = [
      'worker callback' => 'example_unpublish_nodes',
      'jobs' => [
         ['type' => 'story', 'id' => 12, 'crontab' => '0 */2 * january-july mon-fri', 'periodic' => TRUE],
      ],
    ];
    return $schedulers;
  }

Read more about crontab syntax, http://linux.die.net/man/5/crontab

Drupal Queue integration
========================

Optionally, at the scheduled time Job Scheduler can queue a job for execution,
rather than executing the job directly. This is useful when many jobs need to
be executed or when the job's expected execution time is very long.

More information on Drupal Queue: https://api.drupal.org/api/drupal/core%21core.api.php/group/queue/8.0.x

Declare a queue name and a worker callback.

  function example_cron_job_scheduler_info() {
    $schedulers = [];
    $schedulers['example_unpublish'] = [
      'queue name' => 'example_unpublish_queue',
      'worker callback' => 'example_unpublish_nodes',
    ];
    return $schedulers;
  }

  function example_unpublish_nodes(\Drupal\job_scheduler\Entity\JobSchedule $job) {
    // Do stuff.
  }

Optionally, can specify the name and the execution time of the queue.

  function example_cron_job_scheduler_queue_info() {
    $schedulers = [];
    $schedulers['example_unpublish_queue'] = [
      'title' => 'Example unpublish nodes',
      'time' => 120,
    ];
    return $schedulers;
  }

File

README.txt
View source
  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 = [];
  10. $schedulers['example_unpublish'] = [
  11. 'worker callback' => 'example_unpublish_nodes',
  12. ];
  13. return $schedulers;
  14. }
  15. Add a job.
  16. $job = [
  17. 'name' => 'example_unpublish',
  18. 'type' => 'story',
  19. 'id' => 12,
  20. 'period' => 3600,
  21. 'periodic' => TRUE,
  22. ];
  23. $service = \Drupal::service('job_scheduler.manager');
  24. $service->set($job);
  25. Work off a job.
  26. function example_unpublish_nodes(\Drupal\job_scheduler\Entity\JobSchedule $job) {
  27. // Do stuff.
  28. }
  29. Remove a job.
  30. $job = [
  31. 'name' => 'example_unpublish',
  32. 'type' => 'story',
  33. 'id' => 12,
  34. ];
  35. $service = \Drupal::service('job_scheduler.manager');
  36. $service->remove($job);
  37. Optionally jobs can declared together with a schedule in a hook_cron_job_scheduler_info().
  38. function example_cron_job_scheduler_info() {
  39. $schedulers = [];
  40. $schedulers['example_unpublish'] = [
  41. 'worker callback' => 'example_unpublish_nodes',
  42. 'jobs' => [
  43. ['type' => 'story', 'id' => 12, 'period' => 3600, 'periodic' => TRUE],
  44. ],
  45. ];
  46. return $schedulers;
  47. }
  48. Jobs can have a 'crontab' instead of a period. Crontab syntax are Unix-like formatted crontab lines.
  49. Example of job with crontab.
  50. // This will create a job that will be triggered from monday to friday, from january to july, every two hours
  51. function example_cron_job_scheduler_info() {
  52. $schedulers = [];
  53. $schedulers['example_unpublish'] = [
  54. 'worker callback' => 'example_unpublish_nodes',
  55. 'jobs' => [
  56. ['type' => 'story', 'id' => 12, 'crontab' => '0 */2 * january-july mon-fri', 'periodic' => TRUE],
  57. ],
  58. ];
  59. return $schedulers;
  60. }
  61. Read more about crontab syntax, http://linux.die.net/man/5/crontab
  62. Drupal Queue integration
  63. ========================
  64. Optionally, at the scheduled time Job Scheduler can queue a job for execution,
  65. rather than executing the job directly. This is useful when many jobs need to
  66. be executed or when the job's expected execution time is very long.
  67. More information on Drupal Queue: https://api.drupal.org/api/drupal/core%21core.api.php/group/queue/8.0.x
  68. Declare a queue name and a worker callback.
  69. function example_cron_job_scheduler_info() {
  70. $schedulers = [];
  71. $schedulers['example_unpublish'] = [
  72. 'queue name' => 'example_unpublish_queue',
  73. 'worker callback' => 'example_unpublish_nodes',
  74. ];
  75. return $schedulers;
  76. }
  77. function example_unpublish_nodes(\Drupal\job_scheduler\Entity\JobSchedule $job) {
  78. // Do stuff.
  79. }
  80. Optionally, can specify the name and the execution time of the queue.
  81. function example_cron_job_scheduler_queue_info() {
  82. $schedulers = [];
  83. $schedulers['example_unpublish_queue'] = [
  84. 'title' => 'Example unpublish nodes',
  85. 'time' => 120,
  86. ];
  87. return $schedulers;
  88. }