Commit 633d9b53 authored by Erick Hitter's avatar Erick Hitter
Browse files

Implement run-disabling option added in fde89251

parent 5f72153a
...@@ -13,6 +13,7 @@ class Events extends Singleton { ...@@ -13,6 +13,7 @@ class Events extends Singleton {
const LOCK = 'run-events'; const LOCK = 'run-events';
const DISABLE_RUN_OPTION = 'a8c_cron_control_disable_run'; const DISABLE_RUN_OPTION = 'a8c_cron_control_disable_run';
private $run_disabled = 0;
private $concurrent_action_whitelist = array(); private $concurrent_action_whitelist = array();
...@@ -23,11 +24,9 @@ class Events extends Singleton { ...@@ -23,11 +24,9 @@ class Events extends Singleton {
// Prime lock cache if not present // Prime lock cache if not present
Lock::prime_lock( self::LOCK ); Lock::prime_lock( self::LOCK );
// Prime options under certain conditions // Prime options and prepare environment as early as possible
$this->prime_options();
// Prepare environment as early as possible
$earliest_action = did_action( 'muplugins_loaded' ) ? 'plugins_loaded' : 'muplugins_loaded'; $earliest_action = did_action( 'muplugins_loaded' ) ? 'plugins_loaded' : 'muplugins_loaded';
add_action( $earliest_action, array( $this, 'prime_options' ) );
add_action( $earliest_action, array( $this, 'prepare_environment' ) ); add_action( $earliest_action, array( $this, 'prepare_environment' ) );
// Allow code loaded as late as the theme to modify the whitelist // Allow code loaded as late as the theme to modify the whitelist
...@@ -37,10 +36,12 @@ class Events extends Singleton { ...@@ -37,10 +36,12 @@ class Events extends Singleton {
/** /**
* Set initial options that control event behaviour * Set initial options that control event behaviour
*/ */
private function prime_options() { public 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 ) ) { if ( is_admin() || ( defined( 'WP_CLI' ) && \WP_CLI ) || false !== get_endpoint_type() ) {
add_option( self::DISABLE_RUN_OPTION, 0, null, false ); add_option( self::DISABLE_RUN_OPTION, 0, null, false );
} }
$this->run_disabled = get_option( self::DISABLE_RUN_OPTION, 0 );
} }
/** /**
...@@ -132,10 +133,10 @@ class Events extends Singleton { ...@@ -132,10 +133,10 @@ class Events extends Singleton {
$current_events = $this->reduce_queue( $current_events ); $current_events = $this->reduce_queue( $current_events );
} }
// Combine with Internal Events and return necessary data to process the event queue // Combine with Internal Events
// TODO: un-nest array, which is nested for legacy reasons
return array( return array(
'events' => array_merge( $current_events, $internal_events ), 'events' => array_merge( $current_events, $internal_events ),
'endpoint' => get_rest_url( null, REST_API::API_NAMESPACE . '/' . REST_API::ENDPOINT_RUN ),
); );
} }
...@@ -436,6 +437,15 @@ class Events extends Singleton { ...@@ -436,6 +437,15 @@ class Events extends Singleton {
// Either event doesn't recur, or the interval couldn't be determined // Either event doesn't recur, or the interval couldn't be determined
delete_event( $event->timestamp, $event->action, $event->instance ); delete_event( $event->timestamp, $event->action, $event->instance );
} }
/**
* Return status of automatic event execution
*
* @return int 0 is run is enabled, 1 if run is disabled indefinitely, otherwise timestamp when execution will resume
*/
public function run_disabled() {
return $this->run_disabled;
}
} }
Events::instance(); Events::instance();
...@@ -50,8 +50,17 @@ class REST_API extends Singleton { ...@@ -50,8 +50,17 @@ class REST_API extends Singleton {
* For monitoring and alerting, also provides the total number of pending events * For monitoring and alerting, also provides the total number of pending events
*/ */
public function get_events() { public function get_events() {
// Provides `events` and `endpoint` keys needed to run events $response_array = array(
$response_array = Events::instance()->get_events(); 'events' => array(),
'orchestrate_disabled' => Events::instance()->run_disabled(),
);
// Include events only when automatic execution is enabled
if ( 0 === $response_array['orchestrate_disabled'] ) {
$response_array = array_merge( $response_array, Events::instance()->get_events() );
}
$response_array['endpoint'] = get_rest_url( null, self::API_NAMESPACE . '/' . self::ENDPOINT_RUN );
// Provide pending event count for monitoring etc // Provide pending event count for monitoring etc
$response_array['total_events_pending'] = count_events_by_status( Events_Store::STATUS_PENDING ); $response_array['total_events_pending'] = count_events_by_status( Events_Store::STATUS_PENDING );
...@@ -63,6 +72,18 @@ class REST_API extends Singleton { ...@@ -63,6 +72,18 @@ class REST_API extends Singleton {
* Execute a specific event * Execute a specific event
*/ */
public function run_event( $request ) { public function run_event( $request ) {
// Stop if event execution is blocked
$run_disabled = Events::instance()->run_disabled();
if ( 0 !== $run_disabled ) {
if ( 1 === $run_disabled ) {
$message = __( 'Automatic event execution is disabled indefinitely.', 'automattic-cron-control' );
} else {
$message = sprintf( __( 'Automatic event execution is disabled until %s (%d).', 'automattic-cron-control' ), date( 'Y-m-d H:i:s T', $run_disabled ), $run_disabled );
}
return rest_ensure_response( new \WP_Error( 'automatic-execution-disabled', $message, array( 'status' => 403, ) ) );
}
// Parse request for details needed to identify the event to execute // Parse request for details needed to identify the event to execute
// `$timestamp` is, unsurprisingly, the Unix timestamp the event is scheduled for // `$timestamp` is, unsurprisingly, the Unix timestamp the event is scheduled for
// `$action` is the md5 hash of the action used when the event is registered // `$action` is the md5 hash of the action used when the event is registered
......
...@@ -16,6 +16,10 @@ class Orchestrate extends \WP_CLI_Command { ...@@ -16,6 +16,10 @@ class Orchestrate extends \WP_CLI_Command {
* @subcommand list-due-batch * @subcommand list-due-batch
*/ */
public function list_due_now( $args, $assoc_args ) { public function list_due_now( $args, $assoc_args ) {
if ( 0 !== \Automattic\WP\Cron_Control\Events::instance()->run_disabled() ) {
\WP_CLI::error( __( 'Automatic event execution is disabled', 'automattic-cron-control' ) );
}
$events = \Automattic\WP\Cron_Control\Events::instance()->get_events(); $events = \Automattic\WP\Cron_Control\Events::instance()->get_events();
$format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' ); $format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'table' );
...@@ -36,6 +40,10 @@ class Orchestrate extends \WP_CLI_Command { ...@@ -36,6 +40,10 @@ class Orchestrate extends \WP_CLI_Command {
* @synopsis --timestamp=<timestamp> --action=<action-hashed> --instance=<instance> * @synopsis --timestamp=<timestamp> --action=<action-hashed> --instance=<instance>
*/ */
public function run_event( $args, $assoc_args ) { public function run_event( $args, $assoc_args ) {
if ( 0 !== \Automattic\WP\Cron_Control\Events::instance()->run_disabled() ) {
\WP_CLI::error( __( 'Automatic event execution is disabled', 'automattic-cron-control' ) );
}
$timestamp = \WP_CLI\Utils\get_flag_value( $assoc_args, 'timestamp', null ); $timestamp = \WP_CLI\Utils\get_flag_value( $assoc_args, 'timestamp', null );
$action = \WP_CLI\Utils\get_flag_value( $assoc_args, 'action', null ); $action = \WP_CLI\Utils\get_flag_value( $assoc_args, 'action', null );
$instance = \WP_CLI\Utils\get_flag_value( $assoc_args, 'instance', null ); $instance = \WP_CLI\Utils\get_flag_value( $assoc_args, 'instance', null );
......
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