Commit fde89251 authored by Erick Hitter's avatar Erick Hitter
Browse files

Introduce option to store disabling of runner

parent af244774
......@@ -12,6 +12,8 @@ class Events extends Singleton {
*/
const LOCK = 'run-events';
const DISABLE_RUN_OPTION = 'a8c_cron_control_disable_run';
private $concurrent_action_whitelist = array();
/**
......@@ -21,6 +23,9 @@ class Events extends Singleton {
// Prime lock cache if not present
Lock::prime_lock( self::LOCK );
// Prime options under certain conditions
$this->prime_options();
// Prepare environment as early as possible
$earliest_action = did_action( 'muplugins_loaded' ) ? 'plugins_loaded' : 'muplugins_loaded';
add_action( $earliest_action, array( $this, 'prepare_environment' ) );
......@@ -29,6 +34,15 @@ class Events extends Singleton {
add_action( 'after_setup_theme', array( $this, 'populate_concurrent_action_whitelist' ) );
}
/**
* Set initial options that control event behaviour
*/
private function prime_options() {
if ( is_admin() || ( defined( 'WP_CLI' ) && \WP_CLI ) || is_rest_endpoint_request( REST_API::ENDPOINT_LIST ) || is_rest_endpoint_request( REST_API::ENDPOINT_RUN ) ) {
add_option( self::DISABLE_RUN_OPTION, 0, null, false );
}
}
/**
* Prepare environment to run job
*
......
......@@ -72,7 +72,7 @@ class Orchestrate extends \WP_CLI_Command {
array(
'multisite' => is_multisite() ? 1 : 0,
'siteurl' => site_url(),
'disabled' => 0,
'disabled' => get_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 0 ),
),
);
......@@ -80,6 +80,55 @@ class Orchestrate extends \WP_CLI_Command {
\WP_CLI\Utils\format_items( $format, $info, array_keys( $info[0] ) );
}
/**
*
*
* @subcommand manage-automatic-execution
* @synopsis [--enable] [--disable] [--disable_until=<disable_until>]
*/
public function toggle_event_execution( $args, $assoc_args ) {
// Update execution status
$disable_ts = \WP_CLI\Utils\get_flag_value( $assoc_args, 'disable_until', 0 );
$disable_ts = absint( $disable_ts );
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'enable', false ) ) {
update_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 0 );
\WP_CLI::success( 'Enabled' );
return;
} elseif ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'disable', false ) ) {
update_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 1 );
\WP_CLI::success( 'Disabled' );
return;
} elseif( $disable_ts > 0 ) {
if ( $disable_ts > time() ) {
update_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, $disable_ts );
\WP_CLI::success( sprintf( 'Disabled until %s', date( 'Y-m-d H:i:s T', $disable_ts ) ) );
return;
} else {
\WP_CLI::error( 'Timestamp is in the past.' );
}
}
// Display existing status
$status = get_option( \Automattic\WP\Cron_Control\Events::DISABLE_RUN_OPTION, 0 );
switch ( $status ) {
case 0 :
$status = 'Automatic execution enabled';
break;
case 1 :
$status = 'Automatic execution disabled indefinitely';
break;
default :
$status = sprintf( 'Automatic execution disabled until %s', date( 'Y-m-d H:i:s T', $status ) );
break;
}
\WP_CLI::log( $status );
}
}
\WP_CLI::add_command( 'cron-control orchestrate', 'Automattic\WP\Cron_Control\CLI\Orchestrate' );
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment