' . t('About') . ''; $output .= '
' . t('Simple API for scheduling tasks once at a predetermined time or periodically at a fixed interval.') . '
'; $output .= '' . t('Declare scheduler.') . '
'; $output .= '' . t('Add a job.') . '
'; $output .= '' . t('Work off a job.') . '
'; $output .= '' . t('Remove a job.') . '
'; $output .= '' . t('Optionally jobs can declared together with a schedule in a: hook_cron_job_scheduler_info().') . '
'; $output .= '' . t("Jobs can have a 'crontab' instead of a period. Crontab syntax are Unix-like formatted crontab lines.") . '
'; $output .= '' . t('Example of job with crontab.') . '
'; $output .= '' . t("This will create a job that will be triggered from monday to friday, from january to july, every two hours.") . '
'; $output .= '' . t('Read more about crontab syntax: @url_crontab_sintax', array('@url_crontab_sintax' => 'http://linux.die.net/man/5/crontab')) . '
'; return $output; } } // @codingStandardsIgnoreEnd /** * Collects and returns scheduler info. * * @param string $name * Name of the schedule. * * @see hook_cron_job_scheduler_info() * * @return array * Information for the schedule if $name, all the information if not */ function job_scheduler_info($name = NULL) { $info = &drupal_static(__FUNCTION__); if (!$info) { $info = module_invoke_all('cron_job_scheduler_info'); drupal_alter('cron_job_scheduler_info', $info); } if ($name) { return isset($info[$name]) ? $info[$name] : NULL; } else { return $info; } } /** * Implements hook_cron(). */ function job_scheduler_cron() { // Reschedule all jobs if requested. if (variable_get('job_scheduler_rebuild_all', FALSE)) { foreach (job_scheduler_info() as $name => $info) { job_scheduler_rebuild_scheduler($name, $info); } variable_set('job_scheduler_rebuild_all', FALSE); return; } // 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) { $job['data'] = unserialize($job['data']); try { JobScheduler::get($job['name'])->dispatch($job); } catch (Exception $e) { watchdog('job_scheduler', $e->getMessage(), array(), WATCHDOG_ERROR); $failed++; // Drop jobs that have caused exceptions. JobScheduler::get($job['name'])->remove($job); } $total++; if (time() > ($start + 30)) { break; } } // If any jobs were processed, log how much time we spent processing. if ($total || $failed) { watchdog('job_scheduler', 'Finished processing scheduled jobs (!time, !total total, !failed failed).', array( '!time' => format_interval(time() - $start), '!total' => $total, '!failed' => $failed, )); } } /** * Implements hook_modules_enabled(). */ function job_scheduler_modules_enabled($modules) { job_scheduler_rebuild_all(); } /** * Implements hook_modules_disabled(). */ function job_scheduler_modules_disabled($modules) { job_scheduler_rebuild_all(); } /** * Rebuild scheduled information after enable/disable modules. * * @todo What should we do about missing ones when disabling their module? */ function job_scheduler_rebuild_all() { variable_set('job_scheduler_rebuild_all', TRUE); } /** * Rebuild a single scheduler. */ function job_scheduler_rebuild_scheduler($name, $info) { if (!empty($info['jobs'])) { $scheduler = JobScheduler::get($name); foreach ($info['jobs'] as $job) { if (!$scheduler->check($job)) { $scheduler->set($job); } } } } /** * Implements hook_cron_queue_info(). * * Provide queue worker information for jobs declared in * hook_cron_job_scheduler_info(). */ function job_scheduler_cron_queue_info() { $queue = array(); foreach (job_scheduler_info() as $info) { if (!empty($info['jobs']) && !empty($info['queue name'])) { $queue[$info['queue name']] = array( 'worker callback' => 'job_scheduler_cron_queue_worker', // Some reasonable default as we don't know. 'time' => 60, ); } } return $queue; } /** * Execute job worker from queue. * * Providing our own worker has the advantage that we can reschedule the job or * take care of cleanup * Note that as we run the execute() action, the job won't be queued again this * time. */ function job_scheduler_cron_queue_worker($job) { try { JobScheduler::get($job['name'])->execute($job); } catch (Exception $e) { watchdog('job_scheduler', $e->getMessage(), array(), WATCHDOG_ERROR); // Drop jobs that have caused exceptions. JobScheduler::get($job['name'])->remove($job); } }