Commit 6b321035 authored by Erick Hitter's avatar Erick Hitter
Browse files

Merge branch 'master' into add/custom-table

parents 98849eda 78877f3c
......@@ -10,8 +10,6 @@ class Cron_Options_CPT extends Singleton {
/**
* Class properties
*/
const LOCK = 'create-jobs';
const POST_TYPE = 'a8c_cron_ctrl_event';
const POST_STATUS_PENDING = 'inherit';
const POST_STATUS_COMPLETED = 'trash';
......@@ -22,6 +20,8 @@ class Cron_Options_CPT extends Singleton {
private $option_before_unscheduling = null;
private $job_creation_suspended = false;
/**
* Register hooks
*/
......@@ -33,9 +33,6 @@ class Cron_Options_CPT extends Singleton {
add_filter( 'option_jetpack_sync_settings_post_types_blacklist', array( $this, 'exclude_from_jetpack_sync' ), 999 );
add_filter( 'default_option_jetpack_sync_settings_post_types_blacklist', array( $this, 'exclude_from_jetpack_sync' ), 999 );
// Lock for post insertion, to guard against endless event creation when `wp_next_scheduled()` is misused
Lock::prime_lock( self::LOCK );
// Option interception
add_filter( 'pre_option_cron', array( $this, 'get_option' ) );
add_filter( 'pre_update_option_cron', array( $this, 'update_option' ), 10, 2 );
......@@ -291,9 +288,9 @@ class Cron_Options_CPT extends Singleton {
* Also doesn't call `wp_insert_post()` because this function is needed before post types and capabilities are ready.
*/
public function create_or_update_job( $timestamp, $action, $args, $update_id = null ) {
// Limit how many events to insert at once
if ( ! Lock::check_lock( self::LOCK, JOB_CREATION_CONCURRENCY_LIMIT ) ) {
return false;
// Don't create new jobs when manipulating jobs via the plugin's CLI commands
if ( $this->job_creation_suspended ) {
return;
}
global $wpdb;
......@@ -352,9 +349,6 @@ class Cron_Options_CPT extends Singleton {
// Delete internal cache
wp_cache_delete( self::CACHE_KEY );
// Allow more events to be created
Lock::free_lock( self::LOCK );
}
/**
......@@ -449,6 +443,22 @@ class Cron_Options_CPT extends Singleton {
private function event_title( $timestamp, $action, $instance ) {
return sprintf( '%s | %s | %s', $timestamp, $action, $instance );
}
/**
* Prevent CPT from creating new entries
*
* Should be used sparingly, and followed by a call to resume_event_creation(), during bulk operations
*/
public function suspend_event_creation() {
$this->job_creation_suspended = true;
}
/**
* Stop discarding events, once again storing them in the CPT
*/
public function resume_event_creation() {
$this->job_creation_suspended = false;
}
}
Cron_Options_CPT::instance();
......@@ -11,11 +11,6 @@ const JOB_TIMEOUT_IN_MINUTES = 10;
const JOB_LOCK_EXPIRY_IN_MINUTES = 30;
const JOB_CONCURRENCY_LIMIT = 10;
/**
* Job creation
*/
const JOB_CREATION_CONCURRENCY_LIMIT = 5;
/**
* Locks
*/
......
......@@ -363,7 +363,9 @@ class Events extends \WP_CLI_Command {
\WP_CLI::confirm( sprintf( __( 'Are you sure you want to delete this event?', 'automattic-cron-control' ) ) );
// Try to delete the item and provide some relevant output
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->suspend_event_creation();
$trashed = wp_delete_post( $event_post->ID, true );
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->resume_event_creation();
if ( false === $trashed ) {
\WP_CLI::error( sprintf( __( 'Failed to delete event %d', 'automattic-cron-control' ), $jid ) );
......@@ -474,6 +476,9 @@ class Events extends \WP_CLI_Command {
$events_deleted = array();
$events_deleted_count = $events_failed_delete = 0;
// Don't create new events while deleting events
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->suspend_event_creation();
foreach ( $events_to_delete as $event_to_delete ) {
$deleted = wp_delete_post( $event_to_delete['ID'], true );
......@@ -498,6 +503,9 @@ class Events extends \WP_CLI_Command {
\Automattic\WP\Cron_Control\_flush_internal_caches();
}
// New events can be created now that removal is complete
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->resume_event_creation();
// List the removed items
\WP_CLI::line( "\n" . __( 'RESULTS:', 'automattic-cron-control' ) );
......
......@@ -20,20 +20,6 @@ class Lock extends \WP_CLI_Command {
$this->get_reset_lock( $args, $assoc_args, $lock_name, $lock_limit, $lock_description );
}
/**
* Manage the lock that limits concurrent job creation
*
* @subcommand manage-create-lock
* @synopsis [--reset]
*/
public function manage_create_lock( $args, $assoc_args ) {
$lock_name = \Automattic\WP\Cron_Control\Cron_Options_CPT::LOCK;
$lock_limit = \Automattic\WP\Cron_Control\JOB_CREATION_CONCURRENCY_LIMIT;
$lock_description = __( 'This lock limits the number of events created concurrently.', 'automattic-cron-control' );
$this->get_reset_lock( $args, $assoc_args, $lock_name, $lock_limit, $lock_description );
}
/**
* Manage the lock that limits concurrent execution of jobs with the same action
*
......
......@@ -25,6 +25,9 @@ class One_Time_Fixers extends \WP_CLI_Command {
// Provide some idea of what's going on
\WP_CLI::line( __( 'CRON CONTROL', 'automattic-cron-control' ) . "\n" );
// Don't create new events while deleting events
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->suspend_event_creation();
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type = %s;", 'a8c_cron_ctrl_event' ) );
if ( is_numeric( $count ) ) {
......@@ -102,6 +105,9 @@ class One_Time_Fixers extends \WP_CLI_Command {
\WP_CLI::line( "\n" . sprintf( __( 'Cleared the %s cache', 'automattic-cron-control' ), 'Cron Control' ) );
}
// Let event creation resume
\Automattic\WP\Cron_Control\Cron_Options_CPT::instance()->resume_event_creation();
// Fin
\WP_CLI::success( __( 'All done.', 'automattic-cron-control' ) );
}
......
Supports Markdown
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