/** * Format event data into something human-readable * * @param $events * * @return array */ private function format_events($events) { $formatted_events = array(); foreach ($events as $event) { $event_data = \Automattic\WP\Cron_Control\Events::instance()->get_event($event['timestamp'], $event['action'], $event['instance']); $formatted_events[] = array('timestamp' => $event_data['timestamp'], 'action' => $event_data['action'], 'instance' => $event_data['instance'], 'scheduled_for' => date(TIME_FORMAT, $event_data['timestamp']), 'internal_event' => \Automattic\WP\Cron_Control\is_internal_event($event_data['action']) ? __('true', 'automattic-cron-control') : '', 'schedule_name' => false === $event_data['schedule'] ? __('n/a', 'automattic-cron-control') : $event_data['schedule'], 'event_args' => maybe_serialize($event_data['args'])); } return $formatted_events; }
/** * Manage the lock that limits concurrent execution of jobs with the same action * * @subcommand manage-event-lock * @synopsis <action> [--reset] */ public function manage_event_lock($args, $assoc_args) { if (empty($args[0])) { \WP_CLI::error(sprintf(__('Specify an action', 'automattic-cron-control'))); } $lock_name = \Automattic\WP\Cron_Control\Events::instance()->get_lock_key_for_event_action(array('action' => $args[0])); $lock_limit = 1; $lock_description = __("This lock prevents concurrent executions of events with the same action, regardless of the action's arguments.", 'automattic-cron-control'); $this->get_reset_lock($args, $assoc_args, $lock_name, $lock_limit, $lock_description); }
/** * Run an event given an ID * * @subcommand run * @synopsis <event_id> */ public function run_event($args, $assoc_args) { // Validate ID if (!is_numeric($args[0])) { \WP_CLI::error(__('Specify the ID of an event to run', 'automattic-cron-control')); } // Validate event ID and get the information needed to execute it global $wpdb; $event = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE ID = %d AND post_type = %s AND post_status = %s LIMIT 1", $args[0], \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE, \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_STATUS_PENDING)); if (empty($event)) { \WP_CLI::error(sprintf(__('Failed to locate event %d. Please confirm that the entry exists and that the ID is that of an event.', 'automattic-cron-control'), $args[0])); } // Event data $event_data = $this->get_event_details_from_post_title($event); \WP_CLI::line(sprintf(__('Found event %1$d with action `%2$s` and instance identifier `%3$s`', 'automattic-cron-control'), $args[0], $event_data['action'], $event_data['instance'])); // Proceed? $now = time(); if ($event_data['timestamp'] > $now) { \WP_CLI::warning(sprintf(__('This event is not scheduled to run until %1$s GMT (%2$s)', 'automattic-cron-control'), date(TIME_FORMAT, $event_data['timestamp']), $this->calculate_interval($event_data['timestamp'] - $now))); } \WP_CLI::confirm(sprintf(__('Run this event?', 'automattic-cron-control'))); // Environment preparation if (!defined('DOING_CRON')) { define('DOING_CRON', true); } // Run the event $run = \Automattic\WP\Cron_Control\Events::instance()->run_event($event_data['timestamp'], md5($event_data['action']), $event_data['instance'], true); // Output based on run attempt if (is_array($run)) { \WP_CLI::success($run['message']); } elseif (is_wp_error($run)) { \WP_CLI::error($run->get_error_message()); } else { \WP_CLI::error(__('Failed to run event', 'automattic-cron-control')); } }