Projet

Général

Profil

Révision 082b75eb

Ajouté par Assos Assos il y a environ 6 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/job_scheduler/job_scheduler.module
2 2

  
3 3
/**
4 4
 * @file
5
 * job scheduler module.
5
 * Main file for the Job Scheduler.
6 6
 */
7 7

  
8
/**
9
 * Implements hook_help().
10
 *
11
 * @codingStandardsIgnoreStart
12
 */
13
function job_scheduler_help($path, $arg) {
14
  switch ($path) {
15
    case 'admin/help#job_scheduler':
16
      $output = '<h3>' . t('About') . '</h3>';
17
      $output .= '<p>' . t('Simple API for scheduling tasks once at a predetermined time or periodically at a fixed interval.') . '</p>';
18
      $output .= '<h3>' . t('Usage') . '</h3>';
19
      $output .= '<p>' . t('Declare scheduler.') . '</p>';
20
      $output .= '<xmp>' .
21
'function example_cron_job_scheduler_info() {
22
  $schedulers - array();
23
  $schedulers[\'example_unpublish\'] - array(
24
    \'worker callback\' -> \'example_unpublish_nodes\',
25
  );
26
  return $schedulers;
27
}'
28
. '</xmp>';
29
      
30
      $output .= '<p>' . t('Add a job.') . '</p>';
31
      $output .= '<xmp>' .
32
'$job - array(
33
  \'type\' -> \'story\',
34
  \'id\' -> 12,
35
  \'period\' -> 3600,
36
  \'periodic\' -> TRUE,
37
);
38
JobScheduler::get(\'example_unpublish\')->set($job);'
39
. '</xmp>';
40
      
41
      //
42
      
43
    $output .= '<p>' . t('Work off a job.') . '</p>';
44
    $output .= '<xmp>' .
45
'function example_unpublish_nodes($job) {
46
  // Do stuff.
47
}'
48
    . '</xmp>';
49
      
50
    $output .= '<p>' . t('Remove a job.') . '</p>';
51
      $output .= '<xmp>' .
52
'$job - array(
53
  \'type\' -> \'story\',
54
  \'id\' -> 12,
55
);
56
JobScheduler::get(\'example_unpublish\')->remove($job);'
57
    . '</xmp>';
58
      
59
      $output .= '<p>' . t('Optionally jobs can declared together with a schedule in a: hook_cron_job_scheduler_info().') . '</p>';
60
      $output .= '<xmp>' .
61
'function example_cron_job_scheduler_info() {
62
  $schedulers - array();
63
  $schedulers[\'example_unpublish\'] - array(
64
    \'worker callback\' -> \'example_unpublish_nodes\',
65
    \'jobs\' -> array(
66
      array(
67
        \'type\' -> \'story\',
68
        \'id\' -> 12,
69
        \'period\' -> 3600,
70
        \'periodic\' -> TRUE,
71
      ),
72
    )
73
  );
74
  return $schedulers;
75
}'
76
      . '</xmp>';
77
      
78
      $output .= '<p>' . t("Jobs can have a 'crontab' instead of a period. Crontab syntax are Unix-like formatted crontab lines.") . '</p>';
79
      $output .= '<p>' . t('Example of job with crontab.') . '</p>';
80
      $output .= '<p>' . t("This will create a job that will be triggered from monday to friday, from january to july, every two hours.") . '</p>';
81
      
82
      
83
      $output .= '<xmp>' .
84
'function example_cron_job_scheduler_info() {
85
  $schedulers - array();
86
  $schedulers[\'example_unpublish\'] - array(
87
    \'worker callback\' -> \'example_unpublish_nodes\',
88
    \'jobs\' -> array(
89
      array(
90
        \'type\' -> \'story\',
91
        \'id\' -> 12,
92
        \'crontab\' -> \'0 */2 * january-july mon-fri\',
93
        \'periodic\' -> TRUE,
94
      ),
95
    )
96
  );
97
  return $schedulers;
98
}'
99
. '</xmp>';
100
      
101
      $output .= '<p>' . t('Read more about crontab syntax: <a href="@url_crontab_sintax" target="blank">@url_crontab_sintax</a>', array('@url_crontab_sintax' => 'http://linux.die.net/man/5/crontab')) . '</p>';
102
      
103
      
104
      return $output;
105
  }
106
}
107
// @codingStandardsIgnoreEnd
108

  
8 109
/**
9 110
 * Collects and returns scheduler info.
10 111
 *
112
 * @param string $name
113
 *   Name of the schedule.
114
 *
11 115
 * @see hook_cron_job_scheduler_info()
12 116
 *
13
 * @param $name
14
 *   Name of the schedule
15 117
 * @return array
16 118
 *   Information for the schedule if $name, all the information if not
17 119
 */
......
54 156
  // Query and dispatch scheduled jobs.
55 157
  // Process a maximum of 200 jobs in a maximum of 30 seconds.
56 158
  $start = time();
57
  $total =
58
  $failed = 0;
159
  $total = $failed = 0;
59 160
  $jobs = db_select('job_schedule', NULL, array('fetch' => PDO::FETCH_ASSOC))
60
            ->fields('job_schedule')
61
            ->condition('scheduled', 0)
62
            ->condition('next', REQUEST_TIME, '<=')
63
            ->orderBy('next', 'ASC')
64
            ->range(0, 200)
65
            ->execute();
161
    ->fields('job_schedule')
162
    ->condition('scheduled', 0)
163
    ->condition('next', REQUEST_TIME, '<=')
164
    ->orderBy('next', 'ASC')
165
    ->range(0, 200)
166
    ->execute();
66 167
  foreach ($jobs as $job) {
168
    $job['data'] = unserialize($job['data']);
67 169
    try {
68 170
      JobScheduler::get($job['name'])->dispatch($job);
69 171
    }
70 172
    catch (Exception $e) {
71 173
      watchdog('job_scheduler', $e->getMessage(), array(), WATCHDOG_ERROR);
72 174
      $failed++;
73
      // Drop jobs that have caused exceptions
175
      // Drop jobs that have caused exceptions.
74 176
      JobScheduler::get($job['name'])->remove($job);
75 177
    }
76 178
    $total++;
......
79 181
    }
80 182
  }
81 183

  
82
  // Leave a note on how much time we spent processing.
83
  watchdog('job_scheduler', 'Finished processing scheduled jobs (!time s, !total total, !failed failed).', array('!time' => format_interval(time() - $start), '!total' => $total, '!failed' => $failed));
184
  // If any jobs were processed, log how much time we spent processing.
185
  if ($total || $failed) {
186
    watchdog('job_scheduler', 'Finished processing scheduled jobs (!time, !total total, !failed failed).', array(
187
      '!time' => format_interval(time() - $start),
188
      '!total' => $total,
189
      '!failed' => $failed,
190
    ));
191
  }
84 192
}
85 193

  
86 194
/**
......
98 206
}
99 207

  
100 208
/**
101
 * Rebuild scheduled information after enable/disable modules
209
 * Rebuild scheduled information after enable/disable modules.
102 210
 *
103 211
 * @todo What should we do about missing ones when disabling their module?
104 212
 */
......
107 215
}
108 216

  
109 217
/**
110
 * Rebuild a single scheduler
218
 * Rebuild a single scheduler.
111 219
 */
112
function job_scheduler_rebuild_scheduler($name, $info = NULL) {
113
  $info = $info ? $info : job_scheduler_info($name);
220
function job_scheduler_rebuild_scheduler($name, $info) {
114 221
  if (!empty($info['jobs'])) {
115 222
    $scheduler = JobScheduler::get($name);
116 223
    foreach ($info['jobs'] as $job) {
......
129 236
 */
130 237
function job_scheduler_cron_queue_info() {
131 238
  $queue = array();
132
  foreach (job_scheduler_info() as $name => $info) {
239
  foreach (job_scheduler_info() as $info) {
133 240
    if (!empty($info['jobs']) && !empty($info['queue name'])) {
134 241
      $queue[$info['queue name']] = array(
135 242
        'worker callback' => 'job_scheduler_cron_queue_worker',
136
        'time' => 60, // Some reasonable default as we don't know
243
        // Some reasonable default as we don't know.
244
        'time' => 60,
137 245
      );
138 246
    }
139 247
  }
......
141 249
}
142 250

  
143 251
/**
144
 * Execute job worker from queue
252
 * Execute job worker from queue.
145 253
 *
146
 * Providing our own worker has the advantage that we can reschedule the job or take care of cleanup
147
 * Note that as we run the execute() action, the job won't be queued again this time.
254
 * Providing our own worker has the advantage that we can reschedule the job or
255
 * take care of cleanup
256
 * Note that as we run the execute() action, the job won't be queued again this
257
 * time.
148 258
 */
149 259
function job_scheduler_cron_queue_worker($job) {
150 260
  try {
......
152 262
  }
153 263
  catch (Exception $e) {
154 264
    watchdog('job_scheduler', $e->getMessage(), array(), WATCHDOG_ERROR);
155
    // Drop jobs that have caused exceptions
265
    // Drop jobs that have caused exceptions.
156 266
    JobScheduler::get($job['name'])->remove($job);
157 267
  }
158 268
}

Formats disponibles : Unified diff