README.txt 3.08 KB
Newer Older
Alex Barth's avatar
Alex Barth committed
1
2
3
4

Job Scheduler
=============

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

Alex Barth's avatar
Alex Barth committed
8
9
10
11

Usage
=====

12
13
14
Declare scheduler.

  function example_cron_job_scheduler_info() {
15
16
    $schedulers = [];
    $schedulers['example_unpublish'] = [
17
      'worker callback' => 'example_unpublish_nodes',
18
    ];
19
20
21
    return $schedulers;
  }

Alex Barth's avatar
Alex Barth committed
22
23
Add a job.

24
25
  $job = [
    'name' => 'example_unpublish',
Alex Barth's avatar
Alex Barth committed
26
27
28
29
    'type' => 'story',
    'id' => 12,
    'period' => 3600,
    'periodic' => TRUE,
30
31
32
  ];
  $service = \Drupal::service('job_scheduler.manager');
  $service->set($job);
Alex Barth's avatar
Alex Barth committed
33

34
Work off a job.
Alex Barth's avatar
Alex Barth committed
35

36
37
  function example_unpublish_nodes(
    \Drupal\job_scheduler\Entity\JobSchedule $job) {
Alex Barth's avatar
Alex Barth committed
38
39
40
41
42
    // Do stuff.
  }

Remove a job.

43
44
  $job = [
    'name' => 'example_unpublish',
Alex Barth's avatar
Alex Barth committed
45
46
    'type' => 'story',
    'id' => 12,
47
48
49
  ];
  $service = \Drupal::service('job_scheduler.manager');
  $service->remove($job);
Alex Barth's avatar
Alex Barth committed
50

51
52
Optionally jobs can declared together with a schedule in a
hook_cron_job_scheduler_info().
Alex Barth's avatar
Alex Barth committed
53
54

  function example_cron_job_scheduler_info() {
55
56
    $schedulers = [];
    $schedulers['example_unpublish'] = [
Alex Barth's avatar
Alex Barth committed
57
      'worker callback' => 'example_unpublish_nodes',
58
      'jobs' => [
59
60
61
62
63
64
         [
           'type' => 'story',
           'id' => 12,
           'period' => 3600,
           'periodic' => TRUE,
         ],
65
66
      ],
    ];
Alex Barth's avatar
Alex Barth committed
67
68
69
    return $schedulers;
  }

70
71
72
Jobs can have a 'crontab' instead of a period. Crontab syntax are Unix-like
formatted crontab lines.

Alex Barth's avatar
Alex Barth committed
73
74
Example of job with crontab.

75
76
  // This will create a job that will be triggered from monday to friday, from
  // january to july, every two hours.
Alex Barth's avatar
Alex Barth committed
77
  function example_cron_job_scheduler_info() {
78
79
    $schedulers = [];
    $schedulers['example_unpublish'] = [
Alex Barth's avatar
Alex Barth committed
80
      'worker callback' => 'example_unpublish_nodes',
81
      'jobs' => [
82
83
84
85
86
87
         [
           'type' => 'story',
           'id' => 12,
           'crontab' => '0 */2 * january-july mon-fri',
           'periodic' => TRUE,
         ],
88
89
      ],
    ];
Alex Barth's avatar
Alex Barth committed
90
91
92
93
    return $schedulers;
  }

Read more about crontab syntax, http://linux.die.net/man/5/crontab
Alex Barth's avatar
Alex Barth committed
94
95
96
97

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

98
99
100
101
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.

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

104
Declare a queue name and a worker callback.
105
106

  function example_cron_job_scheduler_info() {
107
108
    $schedulers = [];
    $schedulers['example_unpublish'] = [
109
110
      'queue name' => 'example_unpublish_queue',
      'worker callback' => 'example_unpublish_nodes',
111
    ];
112
113
114
    return $schedulers;
  }

115
116
  function example_unpublish_nodes(
    \Drupal\job_scheduler\Entity\JobSchedule $job) {
117
118
    // Do stuff.
  }
Alex Barth's avatar
Alex Barth committed
119

120
Optionally, can specify the name and the execution time of the queue.
Alex Barth's avatar
Alex Barth committed
121

122
123
124
125
126
127
128
129
  function example_cron_job_scheduler_queue_info() {
    $schedulers = [];
    $schedulers['example_unpublish_queue'] = [
      'title' => 'Example unpublish nodes',
      'time' => 120,
    ];
    return $schedulers;
  }