Project

General

Profile

Revision 082b75eb

Added by Assos Assos over 6 years ago

Weekly update of contrib modules

View differences:

drupal7/sites/all/modules/job_scheduler/JobScheduler.inc
16 16
class JobScheduler {
17 17
  /**
18 18
   * The name of this scheduler.
19
   *
20
   * @var string
19 21
   */
20 22
  protected $name;
21 23

  
......
58 60
   *
59 61
   * A job is uniquely identified by $job = array(type, id).
60 62
   *
61
   * @param $job
63
   * @param array $job
62 64
   *   An array that must contain the following keys:
63 65
   *   'type'     - A string identifier of the type of job.
64 66
   *   'id'       - A numeric identifier of the job.
65
   *   'period'   - The time when the task should be executed.
66 67
   *   'periodic' - True if the task should be repeated periodically.
67 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
   *
68 74
   * @code
69 75
   * function worker_callback($job) {
70 76
   *   // Work off job.
......
74 80
   *   $scheduler->set($job);
75 81
   * }
76 82
   * @endcode
83
   *
84
   * @codingStandardsIgnoreStart
77 85
   */
78 86
  public function set($job) {
87
    // @codingStandardsIgnoreEnd
79 88
    $job['name'] = $this->name;
80 89
    $job['last'] = REQUEST_TIME;
81 90
    if (!empty($job['crontab'])) {
......
130 139
   * Executes a worker callback or if schedule declares a queue name, queues a
131 140
   * job for execution.
132 141
   *
133
   * @param $job
142
   * @param array $job
134 143
   *   A $job array as passed into set() or read from job_schedule table.
135 144
   *
136 145
   * @throws Exception
137 146
   *   Exceptions thrown by code called by this method are passed on.
147
   *
148
   * @codingStandardsIgnoreStart
138 149
   */
139 150
  public function dispatch($job) {
151
    // @codingStandardsIgnoreEnd
140 152
    $info = $this->info();
141 153
    if (!$job['periodic']) {
142 154
      $this->remove($job);
......
152 164
  }
153 165

  
154 166
  /**
155
   * Executes a job that
167
   * Executes a job that.
156 168
   *
157
   * @param $job
169
   * @param array $job
158 170
   *   A $job array as passed into set() or read from job_schedule table.
159 171
   *
160 172
   * @throws Exception
161 173
   *   Exceptions thrown by code called by this method are passed on.
174
   *
175
   * @codingStandardsIgnoreStart
162 176
   */
163 177
  public function execute($job) {
178
    // @codingStandardsIgnoreEnd
164 179
    $info = $this->info();
165
    // If the job is periodic, re-schedule it before calling the worker, just in case
180
    // If the job is periodic, re-schedule it before calling the worker, just in
181
    // case.
166 182
    if ($job['periodic']) {
167 183
      $job['last'] = REQUEST_TIME;
168 184
      $this->reschedule($job);
169 185
    }
186
    if (!empty($info['file']) && file_exists($info['file'])) {
187
      include_once $info['file'];
188
    }
170 189
    if (function_exists($info['worker callback'])) {
171 190
      call_user_func($info['worker callback'], $job);
172 191
    }
......
178 197
  }
179 198

  
180 199
  /**
181
   * Re-schedule a job if intended to run again
200
   * Re-schedule a job if intended to run again.
182 201
   *
183 202
   * (If cannot determine the next time, drop the job)
184 203
   */
......
196 215
      drupal_write_record('job_schedule', $job, array('item_id'));
197 216
    }
198 217
    else {
199
      // If no next time, it may mean it wont run again the next year (crontab)
218
      // If no next time, it may mean it won't run again the next year (crontab)
200 219
      $this->remove($job);
201 220
    }
202 221
  }
203 222

  
204 223
  /**
205
   * Check whether a job exists in the queue and update its parameters if so
224
   * Check whether a job exists in the queue and update its parameters if so.
206 225
   */
207 226
  public function check($job) {
208 227
    $job += array('id' => 0, 'period' => 0, 'crontab' => '');
......
213 232
      ->condition('id', $job['id'])
214 233
      ->execute()
215 234
      ->fetchAssoc();
216
    // If existing, and changed period or crontab, we need to reschedule the job
235
    // If existing, and changed period or crontab, we need to reschedule the
236
    // job.
217 237
    if ($existing) {
218 238
      if ($job['period'] != $existing['period'] || $job['crontab'] != $existing['crontab']) {
219 239
        $existing['period'] = $job['period'];
......
223 243
      return $existing;
224 244
    }
225 245
  }
246

  
226 247
}

Also available in: Unified diff