当Drupal运行cron任务时,它将自动处理从模块中定义的任何cron队列drupal_cron_run()
;首先hook_cron()
调用实现,然后清空cron队列。
添加hook_cronapi()
,您可以为处理模块的cron队列的另一个函数添加一个条目。
function mymodule_cronapi($op, $job = NULL) {
$items = array();
$items['queue_users_for_synch'] = array(
'description' => 'Queue all user accounts for synching.',
'rule' => '0 3 * * *', // Run this job every day at 3am.
'callback' => 'mymodule_queue_all_users_for_synching',
);
$items['clean_queue'] = array(
'description' => 'Clean the queue for the user synching.',
'rule' => '0 4 * * *', // Run this job every day at 4 AM.
'callback' => 'mymodule_clean_queue',
);
return $items;
}
function mymodule_clean_queue() {
$queues = module_invoke('mymodule', 'cron_queue_info');
drupal_alter('cron_queue_info', $queues);
// Make sure every queue exists. There is no harm in trying to recreate an
// existing queue.
foreach ($queues as $queue_name => $info) {
DrupalQueue::get($queue_name)->createQueue();
}
foreach ($queues as $queue_name => $info) {
$function = $info['worker callback'];
$end = time() + (isset($info['time']) ? $info['time'] : 15);
$queue = DrupalQueue::get($queue_name);
while (time() < $end && ($item = $queue->claimItem())) {
$function($item->data);
$queue->deleteItem($item);
}
}
}
另一种选择是让Drupal为您处理cron队列,但这会在执行Drupal cron任务时发生。如果要更频繁地清空模块的cron队列,则只能添加由Elysia Cron模块处理的新cron任务。
Elysia Cron模块处理in中的cron队列elysia_cron_run()
;此函数是从elysia_cron_cron()
(的实现hook_cron()
),drush_elysia_cron_run_wrapper()
(Drush命令回调)和自己的cron.php调用的。如果您按照INSTALL.txt文件中的说明进行操作(尤其是在“ STEP B:更改系统目录(可选)”中),并且用http:// example替换了对http://example.com/cron.php的任何调用.com / sites / all / modules / elysia_cron / cron.php,Elysia Cron模块应该已经在处理cron队列。如果确实需要,我建议的代码可用于加快处理模块中使用的cron队列的速度。
// This code is part of the code executed from modules/elysia_cron/cron.php.
define('DRUPAL_ROOT', getcwd());
include_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_override_server_variables(array(
'SCRIPT_NAME' => '/cron.php',
));
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
if (!isset($_GET['cron_key']) || variable_get('cron_key', 'drupal') != $_GET['cron_key']) {
watchdog('cron', 'Cron could not run because an invalid key was used.', array(), WATCHDOG_NOTICE);
drupal_access_denied();
}
elseif (variable_get('maintenance_mode', 0)) {
watchdog('cron', 'Cron could not run because the site is in maintenance mode.', array(), WATCHDOG_NOTICE);
drupal_access_denied();
}
else {
if (function_exists('elysia_cron_run')) {
elysia_cron_run();
}
else {
drupal_cron_run();
}
}