WP_CLI
4 years ago
abstracts
4 years ago
actions
4 years ago
data-stores
4 years ago
migration
4 years ago
schedules
4 years ago
schema
4 years ago
ActionScheduler_ActionClaim.php
4 years ago
ActionScheduler_ActionFactory.php
4 years ago
ActionScheduler_AdminView.php
4 years ago
ActionScheduler_AsyncRequest_QueueRunner.php
4 years ago
ActionScheduler_Compatibility.php
4 years ago
ActionScheduler_DataController.php
4 years ago
ActionScheduler_DateTime.php
4 years ago
ActionScheduler_Exception.php
4 years ago
ActionScheduler_FatalErrorMonitor.php
4 years ago
ActionScheduler_InvalidActionException.php
4 years ago
ActionScheduler_ListTable.php
4 years ago
ActionScheduler_LogEntry.php
4 years ago
ActionScheduler_NullLogEntry.php
4 years ago
ActionScheduler_OptionLock.php
4 years ago
ActionScheduler_QueueCleaner.php
4 years ago
ActionScheduler_QueueRunner.php
4 years ago
ActionScheduler_Versions.php
4 years ago
ActionScheduler_WPCommentCleaner.php
4 years ago
ActionScheduler_wcSystemStatus.php
4 years ago
ActionScheduler_QueueRunner.php
198 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class ActionScheduler_QueueRunner |
| 5 | */ |
| 6 | class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner { |
| 7 | const WP_CRON_HOOK = 'action_scheduler_run_queue'; |
| 8 | |
| 9 | const WP_CRON_SCHEDULE = 'every_minute'; |
| 10 | |
| 11 | /** @var ActionScheduler_AsyncRequest_QueueRunner */ |
| 12 | protected $async_request; |
| 13 | |
| 14 | /** @var ActionScheduler_QueueRunner */ |
| 15 | private static $runner = null; |
| 16 | |
| 17 | /** |
| 18 | * @return ActionScheduler_QueueRunner |
| 19 | * @codeCoverageIgnore |
| 20 | */ |
| 21 | public static function instance() { |
| 22 | if ( empty(self::$runner) ) { |
| 23 | $class = apply_filters('action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner'); |
| 24 | self::$runner = new $class(); |
| 25 | } |
| 26 | return self::$runner; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * ActionScheduler_QueueRunner constructor. |
| 31 | * |
| 32 | * @param ActionScheduler_Store $store |
| 33 | * @param ActionScheduler_FatalErrorMonitor $monitor |
| 34 | * @param ActionScheduler_QueueCleaner $cleaner |
| 35 | */ |
| 36 | public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null, ActionScheduler_AsyncRequest_QueueRunner $async_request = null ) { |
| 37 | parent::__construct( $store, $monitor, $cleaner ); |
| 38 | |
| 39 | if ( is_null( $async_request ) ) { |
| 40 | $async_request = new ActionScheduler_AsyncRequest_QueueRunner( $this->store ); |
| 41 | } |
| 42 | |
| 43 | $this->async_request = $async_request; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @codeCoverageIgnore |
| 48 | */ |
| 49 | public function init() { |
| 50 | |
| 51 | add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) ); |
| 52 | |
| 53 | // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param |
| 54 | $next_timestamp = wp_next_scheduled( self::WP_CRON_HOOK ); |
| 55 | if ( $next_timestamp ) { |
| 56 | wp_unschedule_event( $next_timestamp, self::WP_CRON_HOOK ); |
| 57 | } |
| 58 | |
| 59 | $cron_context = array( 'WP Cron' ); |
| 60 | |
| 61 | if ( ! wp_next_scheduled( self::WP_CRON_HOOK, $cron_context ) ) { |
| 62 | $schedule = apply_filters( 'action_scheduler_run_schedule', self::WP_CRON_SCHEDULE ); |
| 63 | wp_schedule_event( time(), $schedule, self::WP_CRON_HOOK, $cron_context ); |
| 64 | } |
| 65 | |
| 66 | add_action( self::WP_CRON_HOOK, array( self::instance(), 'run' ) ); |
| 67 | $this->hook_dispatch_async_request(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Hook check for dispatching an async request. |
| 72 | */ |
| 73 | public function hook_dispatch_async_request() { |
| 74 | add_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Unhook check for dispatching an async request. |
| 79 | */ |
| 80 | public function unhook_dispatch_async_request() { |
| 81 | remove_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Check if we should dispatch an async request to process actions. |
| 86 | * |
| 87 | * This method is attached to 'shutdown', so is called frequently. To avoid slowing down |
| 88 | * the site, it mitigates the work performed in each request by: |
| 89 | * 1. checking if it's in the admin context and then |
| 90 | * 2. haven't run on the 'shutdown' hook within the lock time (60 seconds by default) |
| 91 | * 3. haven't exceeded the number of allowed batches. |
| 92 | * |
| 93 | * The order of these checks is important, because they run from a check on a value: |
| 94 | * 1. in memory - is_admin() maps to $GLOBALS or the WP_ADMIN constant |
| 95 | * 2. in memory - transients use autoloaded options by default |
| 96 | * 3. from a database query - has_maximum_concurrent_batches() run the query |
| 97 | * $this->store->get_claim_count() to find the current number of claims in the DB. |
| 98 | * |
| 99 | * If all of these conditions are met, then we request an async runner check whether it |
| 100 | * should dispatch a request to process pending actions. |
| 101 | */ |
| 102 | public function maybe_dispatch_async_request() { |
| 103 | if ( is_admin() && ! ActionScheduler::lock()->is_locked( 'async-request-runner' ) ) { |
| 104 | // Only start an async queue at most once every 60 seconds |
| 105 | ActionScheduler::lock()->set( 'async-request-runner' ); |
| 106 | $this->async_request->maybe_dispatch(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Process actions in the queue. Attached to self::WP_CRON_HOOK i.e. 'action_scheduler_run_queue' |
| 112 | * |
| 113 | * The $context param of this method defaults to 'WP Cron', because prior to Action Scheduler 3.0.0 |
| 114 | * that was the only context in which this method was run, and the self::WP_CRON_HOOK hook had no context |
| 115 | * passed along with it. New code calling this method directly, or by triggering the self::WP_CRON_HOOK, |
| 116 | * should set a context as the first parameter. For an example of this, refer to the code seen in |
| 117 | * @see ActionScheduler_AsyncRequest_QueueRunner::handle() |
| 118 | * |
| 119 | * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
| 120 | * Generally, this should be capitalised and not localised as it's a proper noun. |
| 121 | * @return int The number of actions processed. |
| 122 | */ |
| 123 | public function run( $context = 'WP Cron' ) { |
| 124 | ActionScheduler_Compatibility::raise_memory_limit(); |
| 125 | ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() ); |
| 126 | do_action( 'action_scheduler_before_process_queue' ); |
| 127 | $this->run_cleanup(); |
| 128 | $processed_actions = 0; |
| 129 | if ( false === $this->has_maximum_concurrent_batches() ) { |
| 130 | $batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 ); |
| 131 | do { |
| 132 | $processed_actions_in_batch = $this->do_batch( $batch_size, $context ); |
| 133 | $processed_actions += $processed_actions_in_batch; |
| 134 | } while ( $processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded( $processed_actions ) ); // keep going until we run out of actions, time, or memory |
| 135 | } |
| 136 | |
| 137 | do_action( 'action_scheduler_after_process_queue' ); |
| 138 | return $processed_actions; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Process a batch of actions pending in the queue. |
| 143 | * |
| 144 | * Actions are processed by claiming a set of pending actions then processing each one until either the batch |
| 145 | * size is completed, or memory or time limits are reached, defined by @see $this->batch_limits_exceeded(). |
| 146 | * |
| 147 | * @param int $size The maximum number of actions to process in the batch. |
| 148 | * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' |
| 149 | * Generally, this should be capitalised and not localised as it's a proper noun. |
| 150 | * @return int The number of actions processed. |
| 151 | */ |
| 152 | protected function do_batch( $size = 100, $context = '' ) { |
| 153 | $claim = $this->store->stake_claim($size); |
| 154 | $this->monitor->attach($claim); |
| 155 | $processed_actions = 0; |
| 156 | |
| 157 | foreach ( $claim->get_actions() as $action_id ) { |
| 158 | // bail if we lost the claim |
| 159 | if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) { |
| 160 | break; |
| 161 | } |
| 162 | $this->process_action( $action_id, $context ); |
| 163 | $processed_actions++; |
| 164 | |
| 165 | if ( $this->batch_limits_exceeded( $processed_actions ) ) { |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | $this->store->release_claim($claim); |
| 170 | $this->monitor->detach(); |
| 171 | $this->clear_caches(); |
| 172 | return $processed_actions; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Running large batches can eat up memory, as WP adds data to its object cache. |
| 177 | * |
| 178 | * If using a persistent object store, this has the side effect of flushing that |
| 179 | * as well, so this is disabled by default. To enable: |
| 180 | * |
| 181 | * add_filter( 'action_scheduler_queue_runner_flush_cache', '__return_true' ); |
| 182 | */ |
| 183 | protected function clear_caches() { |
| 184 | if ( ! wp_using_ext_object_cache() || apply_filters( 'action_scheduler_queue_runner_flush_cache', false ) ) { |
| 185 | wp_cache_flush(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | public function add_wp_cron_schedule( $schedules ) { |
| 190 | $schedules['every_minute'] = array( |
| 191 | 'interval' => 60, // in seconds |
| 192 | 'display' => __( 'Every minute', 'action-scheduler' ), |
| 193 | ); |
| 194 | |
| 195 | return $schedules; |
| 196 | } |
| 197 | } |
| 198 |