Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By asghar on
Hi Guys
Generally drupal cron runs all drupal cron jobs & queu process when we run drupal cron but some time we need to run specific drupal cron & cron queu process individually at specific time without touching others drupal cron process. Here is snippet code which help you for running specific cron jon & queu
/***
* Implements of hook_menu
*/
function ikonami_menu() {
$items = array();
$items['ikonami-individual/cron-run'] = array(
'page callback' => 'ikonami_menu_callback_handler',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
return $items;
}
function ikonami_menu_callback_handler() {
$module_name = 'ikonami_custom';
return ikonami_run_cron_specific_process($module_name);
}
function ikonami_run_cron_specific_process($module_name) {
if(function_exists('bounce_cron')) {
// bounce_cron();
// drupal_cron_run();
// Run bounce module cron queu function without invoking other cron hooks.
// Allow execution to continue even if the request gets canceled.
@ignore_user_abort(TRUE);
// Prevent session information from being saved while cron is running.
$original_session_saving = drupal_save_session();
drupal_save_session(FALSE);
// Force the current user to anonymous to ensure consistent permissions on
// cron runs.
$original_user = $GLOBALS['user'];
$GLOBALS['user'] = drupal_anonymous_user();
// Try to allocate enough time to run all the hook_cron implementations.
drupal_set_time_limit(240);
$return = FALSE;
// Grab the defined cron queues.
$queue_function =$module_name.'_cron_queue_info';
$queues = $queue_function();
// Try to acquire cron lock.
if (!lock_acquire('cron', 240.0)) {
// Cron is still running normally.
watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
}
else {
// 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();
}
// Register shutdown callback.
drupal_register_shutdown_function('drupal_cron_cleanup');
// Iterate through the modules calling their cron handlers (if any):
//foreach (module_implements('cron') as $module) {
// Do not let an exception thrown by one module disturb another.
try {
module_invoke($module_name, 'cron');
}
catch (Exception $e) {
watchdog_exception('cron', $e);
}
// }
// Record cron time.
variable_set('cron_last', REQUEST_TIME);
watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE);
// Release cron lock.
lock_release('cron');
// Return TRUE so other functions can check if it did run successfully
$return = TRUE;
}
foreach ($queues as $queue_name => $info) {
if (!empty($info['skip on cron'])) {
// Do not run if queue wants to skip.
continue;
}
$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);
}
}
// Restore the user.
$GLOBALS['user'] = $original_user;
drupal_save_session($original_session_saving);
// echo "<pre>".print_r($queues, true)."</pre>";
exit('Success fully run');
}
exit('Cron run Failed');
}
Comments
Question ?
Is this a question or siggestion ?
You don't need to write code.
Elysia Cron is the soution.
It is a solution.
It is a solution.
I have already used this but
I have already used this but some time suddenly stop and does not work again.