1

I would like to have the ability to prevent cron from running based on a variable set in the database. I have created a hook_cron implementation in a module with an extremely low weight so it will presumably run before all other module's cron hooks. Within that function I then check the variable and call exit() to abort PHP's execution. This seems to work but is there a more elegant way to prevent cron from continuing? Calling exit() seems a bit harsh and I am concerned about unintended consequences. Example code below.

function ex1_cron() {
  if (variable_get('ex1_var', 1) === 1) {
    watchdog('ex1', 'Cron is being aborted.', array(), WATCHDOG_WARNING, NULL);
    exit();
  }
}

3 Answers 3

3

Calling exit() yes is a little bit too harsh and it does bring some consequences with it:

  1. Typically when drupal_cron_run() finishes running all the cron jobs created with hook_cron() it logs into the watchdog the message "Cron run completed". Aborting your cron cycle mid-execution leaves you with no traces of how or when your cron finished.
  2. When the cron cycle begins, Drupal creates a cron lock by using variable_set('cron_semaphore', time());, which prevents further cron cycles from running until you release the sempahore (manually) or until 3600 seconds pass since the last time you aborted the cron.

When your abort condition is met, you should at least do the following:

// Call cleanup function.
drupal_cron_cleanup();

// Release cron semaphore
variable_del('cron_semaphore');

watchdog('name_of_yourmodule', 'Aborted because XYZ.', array(), WATCHDOG_ERROR);

Also like @wiifm mentioned in his answer, a lot of critical functions defined by core and contrib depend on cron running to keep your site working. Abort cron and your site suddenly stops running smoothly. I would really avoid doing this.

0

I would propose that not running cron is a bad thing on most drupal sites. Cron is used for a large number of critical functions e.g. sending emails, updating the search index, making new sitemaps etc.

Instead of stopping cron from running, why not address the issue that is happening on cron? BTW - what is happening on cron that you do not want?

The code you wrote above will technically work, but if you were a developer on my team, and I saw this code in the repo, there would be words had ;)

3
  • This would only be in rare instances. For example, I would prefer cron not to run when I am doing a deployment. So I would set this variable only while the deployment is active. Commented Dec 14, 2011 at 21:21
  • 3
    If this is the case...then you can lock the cron by issuing a drush command, e.g.: drush vset cron_semaphore 9999999. Then when your deployment is done you can do drush vdel cron_semaphore. That will prevent cron from running. You can also get fancier by creating your own drush command for locking cron that includes a watchdog message. This would be a far more elegant and reliable solution than preventing cron from running. I agree with @wiifm 100%. Commented Dec 14, 2011 at 21:36
  • Agree with @amateurbarista, drush is amazing and should be used more often. Another point to mention is you probably want to check to see if cron is already running before doing a deployment (i.e. check to see if it is in progress already)
    – wiifm
    Commented Dec 14, 2011 at 21:43
0

I don't know if its relevant to you, but you might wanna check Elysa cron module

It basically let you to determine which cron jobs will run and when. Maybe there is a way to do so within the code and not only by GUI(but i never checked it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.