Projet

Général

Profil

Paste
Télécharger (6,62 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / job_scheduler / JobScheduler.inc @ 082b75eb

1
<?php
2

    
3
/**
4
 * @file
5
 * JobScheduler class.
6
 */
7

    
8
/**
9
 * Use to make Job Scheduler exceptions identifiable by type.
10
 */
11
class JobSchedulerException extends Exception {}
12

    
13
/**
14
 * Manage scheduled jobs.
15
 */
16
class JobScheduler {
17
  /**
18
   * The name of this scheduler.
19
   *
20
   * @var string
21
   */
22
  protected $name;
23

    
24
  /**
25
   * Produces a single instance of JobScheduler for a schedule name.
26
   */
27
  public static function get($name) {
28
    static $schedulers;
29
    // Instantiante a new scheduler for $name if we haven't done so yet.
30
    if (!isset($schedulers[$name])) {
31
      $class = variable_get('job_scheduler_class_' . $name, 'JobScheduler');
32
      $schedulers[$name] = new $class($name);
33
    }
34
    return $schedulers[$name];
35
  }
36

    
37
  /**
38
   * Creates a JobScheduler object.
39
   */
40
  protected function __construct($name) {
41
    $this->name = $name;
42
  }
43

    
44
  /**
45
   * Returns scheduler info.
46
   *
47
   * @see hook_cron_job_scheduler_info()
48
   *
49
   * @throws JobSchedulerException.
50
   */
51
  public function info() {
52
    if ($info = job_scheduler_info($this->name)) {
53
      return $info;
54
    }
55
    throw new JobSchedulerException('Could not find Job Scheduler cron information for ' . check_plain($this->name));
56
  }
57

    
58
  /**
59
   * Add a job to the schedule, replace any existing job.
60
   *
61
   * A job is uniquely identified by $job = array(type, id).
62
   *
63
   * @param array $job
64
   *   An array that must contain the following keys:
65
   *   'type'     - A string identifier of the type of job.
66
   *   'id'       - A numeric identifier of the job.
67
   *   'periodic' - True if the task should be repeated periodically.
68
   *
69
   *   The array must also contain one of the following keys:
70
   *   'period'  - The time when the task should be executed.
71
   *   'crontab' - A crontab entry which describes the times a task should be
72
   *               executed.
73
   *
74
   * @code
75
   * function worker_callback($job) {
76
   *   // Work off job.
77
   *   // Set next time to be called. If this portion of the code is not
78
   *   // reached for some reason, the scheduler will keep periodically invoking
79
   *   // the callback() with the period value initially specified.
80
   *   $scheduler->set($job);
81
   * }
82
   * @endcode
83
   *
84
   * @codingStandardsIgnoreStart
85
   */
86
  public function set($job) {
87
    // @codingStandardsIgnoreEnd
88
    $job['name'] = $this->name;
89
    $job['last'] = REQUEST_TIME;
90
    if (!empty($job['crontab'])) {
91
      $crontab = new JobSchedulerCronTab($job['crontab']);
92
      $job['next'] = $crontab->nextTime(REQUEST_TIME);
93
    }
94
    else {
95
      $job['next'] = REQUEST_TIME + $job['period'];
96
    }
97
    $job['scheduled'] = 0;
98
    $this->remove($job);
99
    drupal_write_record('job_schedule', $job);
100
  }
101

    
102
  /**
103
   * Reserve a job.
104
   */
105
  protected function reserve($job) {
106
    $job['name'] = $this->name;
107
    $job['scheduled'] =
108
    $job['last'] = REQUEST_TIME;
109
    $job['next'] = $job['period'] + REQUEST_TIME;
110
    drupal_write_record('job_schedule', $job, array('name', 'type', 'id'));
111
  }
112

    
113
  /**
114
   * Remove a job from the schedule, replace any existing job.
115
   *
116
   * A job is uniquely identified by $job = array(type, id).
117
   */
118
  public function remove($job) {
119
    db_delete('job_schedule')
120
      ->condition('name', $this->name)
121
      ->condition('type', $job['type'])
122
      ->condition('id', isset($job['id']) ? $job['id'] : 0)
123
      ->execute();
124
  }
125

    
126
  /**
127
   * Remove all jobs for a given type.
128
   */
129
  public function removeAll($type) {
130
    db_delete('job_schedule')
131
      ->condition('name', $this->name)
132
      ->condition('type', $type)
133
      ->execute();
134
  }
135

    
136
  /**
137
   * Dispatches a job.
138
   *
139
   * Executes a worker callback or if schedule declares a queue name, queues a
140
   * job for execution.
141
   *
142
   * @param array $job
143
   *   A $job array as passed into set() or read from job_schedule table.
144
   *
145
   * @throws Exception
146
   *   Exceptions thrown by code called by this method are passed on.
147
   *
148
   * @codingStandardsIgnoreStart
149
   */
150
  public function dispatch($job) {
151
    // @codingStandardsIgnoreEnd
152
    $info = $this->info();
153
    if (!$job['periodic']) {
154
      $this->remove($job);
155
    }
156
    if (!empty($info['queue name'])) {
157
      if (DrupalQueue::get($info['queue name'])->createItem($job)) {
158
        $this->reserve($job);
159
      }
160
    }
161
    else {
162
      $this->execute($job);
163
    }
164
  }
165

    
166
  /**
167
   * Executes a job that.
168
   *
169
   * @param array $job
170
   *   A $job array as passed into set() or read from job_schedule table.
171
   *
172
   * @throws Exception
173
   *   Exceptions thrown by code called by this method are passed on.
174
   *
175
   * @codingStandardsIgnoreStart
176
   */
177
  public function execute($job) {
178
    // @codingStandardsIgnoreEnd
179
    $info = $this->info();
180
    // If the job is periodic, re-schedule it before calling the worker, just in
181
    // case.
182
    if ($job['periodic']) {
183
      $job['last'] = REQUEST_TIME;
184
      $this->reschedule($job);
185
    }
186
    if (!empty($info['file']) && file_exists($info['file'])) {
187
      include_once $info['file'];
188
    }
189
    if (function_exists($info['worker callback'])) {
190
      call_user_func($info['worker callback'], $job);
191
    }
192
    else {
193
      // @todo If worker doesn't exist anymore we should do something about it, remove and throw exception?
194
      $this->remove($job);
195
      throw new JobSchedulerException('Could not find worker callback function: ' . $info['worker callback']);
196
    }
197
  }
198

    
199
  /**
200
   * Re-schedule a job if intended to run again.
201
   *
202
   * (If cannot determine the next time, drop the job)
203
   */
204
  public function reschedule($job) {
205
    $job['scheduled'] = 0;
206
    if (!empty($job['crontab'])) {
207
      $crontab = new JobSchedulerCronTab($job['crontab']);
208
      $job['next'] = $crontab->nextTime($job['last']);
209
    }
210
    else {
211
      $job['next'] = $job['last'] + $job['period'];
212
    }
213

    
214
    if ($job['next']) {
215
      drupal_write_record('job_schedule', $job, array('item_id'));
216
    }
217
    else {
218
      // If no next time, it may mean it won't run again the next year (crontab)
219
      $this->remove($job);
220
    }
221
  }
222

    
223
  /**
224
   * Check whether a job exists in the queue and update its parameters if so.
225
   */
226
  public function check($job) {
227
    $job += array('id' => 0, 'period' => 0, 'crontab' => '');
228
    $existing = db_select('job_schedule')
229
      ->fields('job_schedule')
230
      ->condition('name', $this->name)
231
      ->condition('type', $job['type'])
232
      ->condition('id', $job['id'])
233
      ->execute()
234
      ->fetchAssoc();
235
    // If existing, and changed period or crontab, we need to reschedule the
236
    // job.
237
    if ($existing) {
238
      if ($job['period'] != $existing['period'] || $job['crontab'] != $existing['crontab']) {
239
        $existing['period'] = $job['period'];
240
        $existing['crontab'] = $job['crontab'];
241
        $this->reschedule($existing);
242
      }
243
      return $existing;
244
    }
245
  }
246

    
247
}