function job_scheduler_cron in Job Scheduler 7
Same name and namespace in other branches
- 8.3 job_scheduler.module \job_scheduler_cron()
- 8.2 job_scheduler.module \job_scheduler_cron()
- 6 job_scheduler.module \job_scheduler_cron()
- 7.2 job_scheduler.module \job_scheduler_cron()
Implementation of hook_cron().
File
- ./
job_scheduler.module, line 11 - job scheduler module.
Code
function job_scheduler_cron() {
// Reschedule stuck periodic jobs after one hour.
db_update('job_schedule')
->fields(array(
'scheduled' => 0,
))
->condition('scheduled', REQUEST_TIME - 3600, '<')
->condition('periodic', 1)
->execute();
// Query and dispatch scheduled jobs.
// Process a maximum of 200 jobs in a maximum of 30 seconds.
$start = time();
$total = $failed = 0;
$jobs = db_select('job_schedule', NULL, array(
'fetch' => PDO::FETCH_ASSOC,
))
->fields('job_schedule')
->condition('scheduled', 0)
->condition('next', REQUEST_TIME, '<')
->orderBy('next', 'ASC')
->range(0, 200)
->execute();
foreach ($jobs as $job) {
try {
JobScheduler::get($job['name'])
->dispatch($job);
} catch (Exception $e) {
watchdog('job_scheduler', $e
->getMessage(), array(), WATCHDOG_ERROR);
$failed++;
}
$total++;
if (time() > $start + 30) {
break;
}
}
// Leave a note on how much time we spent processing.
watchdog('job_scheduler', 'Finished processing scheduled jobs (!time s, !total total, !failed failed).', array(
'!time' => format_interval(time() - $start),
'!total' => $total,
'!failed' => $failed,
));
}