Problem/Motivation

It is often useful to prevent cron from being run at the Drupal-level (as opposed to the OS-level in the case of a crontab being used, for example).

Use Case 1:
When deploying code from a staging environment to a production environment, it can be desirable to disable cron on the production environment prior to the deployment then re-enable it after the deployment. In some cases, it is undesirable to enable maintenance mode to achieve this.

Use Case 2:
When copying the database from a production environment to a staging environment, it can be desirable to ensure that cron jobs running on the production environment have time to complete before the database is copied (and that those jobs don't start again until the database has finished being copied). This prevents the staging environment from thinking that cron jobs are already running when in fact they started on the production environment, not the staging environment.

Proposed resolution

Introduce a new a variable "ultimate_cron_disable_scheduled", which (when equal to 1 or TRUE) prevents all jobs from running as scheduled (whether cron is executed via Drush, Drupal's external-facing cron URL, or via the UI).

This variable can be set using Drush (e.g. "drush vset ultimate_cron_disable_scheduled 1" / "drush vset ultimate_cron_disable_scheduled 0") and/or overridden in settings.php (e.g. on a per-environment basis).

A named job can still be run via the UI or via "drush cron-run JOB_NAME".

Remaining tasks

Document this variable in the module's documentation.

User interface changes

None required. This variable could optionally be exposed as a setting to allow enabling/disabling scheduled cron via the UI.

API changes

None.

Data model changes

None.

Original report by [sinn]

I know that we can disable cron execution if we use poorman cron.

But how can we disable cron execution if cron is executed from outside? This case is useful when we deploy new code on a site or site is in maintenance mode.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sinn created an issue. See original summary.

arnested’s picture

The jobs will not be launched when the site is in maintenance mode.

sinn’s picture

They will be.

arnested’s picture

Hmm...

Running scheduled jobs should be disabled during maintenance_mode: http://cgit.drupalcode.org/ultimate_cron/tree/ultimate_cron.module#n1581

But runnig named jobs is not disabled.

And you have a point that it might also be an idea to just have a "disable all cron execution" option that can be set no whether you are in maintenance mode or not.

sinn’s picture

What is difference between named jobs and scheduled jobs?

All my cron jobs (that is described in hook_cron or hook_cron_queue_info) are executed in maintenance mode.

RedEight’s picture

I think what he is saying is that Ultimate Cron will actually stop any cron job from running when the site is in maintenance mode... I'm currently fighting this issue as I have a number of cron jobs that DO need to run in maintenance mode but Ultimate Cron is blocking them.

jdleonard’s picture

Title: Disable cron execution at all » Ability to disable execution of scheduled jobs
Issue summary: View changes
Status: Active » Needs review
FileSize
1.55 KB

Status: Needs review » Needs work
jdleonard’s picture

Status: Needs work » Needs review

Ooops; looks like testing is disabled for D7 and I accidentally set that patch to be tested for D8. Please ignore the test failure.

arnested’s picture

Thank you for your patch, jdleonard!

I have add some minor improvements:

* In hook_uninstall() delete the variable before clearing the variable cache.
* Avoid nesting if's in ultimate_cron_run_scheduled() by using early returns instead.
* Add checks to hook_requirements() so we will show on the status page if scheduled jobs will not be run because they have been disabled or we are in maintenance mode.

There is one thing that remains problematic, though.

If cron is launched via wget og curl on /cron.php the new variable will not have any effect.

jdleonard’s picture

Status: Needs work » Needs review

Great additions, thanks arnested!

I tested your patch and everything is working as expected for me, including cron not being run via wget on cron.php (I received the expected message "Cannot launch scheduled jobs while ultimate_cron_disable_scheduled variable is set!"). Can you confirm that wget on cron.php is unexpectedly running cron when ultimate_cron_disable_scheduled is set to a non-zero value?

Assuming things are working for you, I think this is RTBC with the possible exception of some documentation.

  • arnested committed 8d5f2cf on 7.x-2.x authored by jdleonard
    Issue #2632340 by jdleonard, arnested: Ability to disable execution of...
arnested’s picture

Status: Needs review » Fixed

Indeed it works with cron.php as well. Wonder what I was doing when testing it - probably too tired.

I have committed the patch and released a new version 7.x-2.5.

I'm going to close this issue now knowing it does not solve all wishes mentioned in the issue (RedEights need for enabling/disabling individual jobs during maintenance mode). Please open a new issue if those needs still exist.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

delacosta456’s picture

hi
is it still possible to use this functionality with 8 version?