← All changes
|
vendor/woocommerce/action-scheduler/classes/ActionScheduler_QueueRunner.php
+48
-23
1.2.0
→
trunk
View file →
| @@ -7,26 +7,41 @@ | ||
| 7 | 7 | const WP_CRON_HOOK = 'action_scheduler_run_queue'; |
| 8 | 8 | |
| 9 | 9 | const WP_CRON_SCHEDULE = 'every_minute'; |
| 10 | 10 | |
| 11 | - /** @var ActionScheduler_AsyncRequest_QueueRunner */ | |
| 11 | + /** | |
| 12 | + * ActionScheduler_AsyncRequest_QueueRunner instance. | |
| 13 | + * | |
| 14 | + * @var ActionScheduler_AsyncRequest_QueueRunner | |
| 15 | + */ | |
| 12 | 16 | protected $async_request; |
| 13 | 17 | |
| 14 | - /** @var ActionScheduler_QueueRunner */ | |
| 18 | + /** | |
| 19 | + * ActionScheduler_QueueRunner instance. | |
| 20 | + * | |
| 21 | + * @var ActionScheduler_QueueRunner | |
| 22 | + */ | |
| 15 | 23 | private static $runner = null; |
| 16 | 24 | |
| 17 | - /** @var int */ | |
| 25 | + /** | |
| 26 | + * Number of processed actions. | |
| 27 | + * | |
| 28 | + * @var int | |
| 29 | + */ | |
| 18 | 30 | private $processed_actions_count = 0; |
| 19 | 31 | |
| 20 | 32 | /** |
| 33 | + * Get instance. | |
| 34 | + * | |
| 21 | 35 | * @return ActionScheduler_QueueRunner |
| 22 | 36 | * @codeCoverageIgnore |
| 23 | 37 | */ |
| 24 | 38 | public static function instance() { |
| 25 | - if ( empty(self::$runner) ) { | |
| 26 | - $class = apply_filters('action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner'); | |
| 39 | + if ( empty( self::$runner ) ) { | |
| 40 | + $class = apply_filters( 'action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner' ); | |
| 27 | 41 | self::$runner = new $class(); |
| 28 | 42 | } |
| 43 | + | |
| 29 | 44 | return self::$runner; |
| 30 | 45 | } |
| 31 | 46 | |
| 32 | 47 | /** |
| @@ -31,13 +46,14 @@ | ||
| 31 | 46 | |
| 32 | 47 | /** |
| 33 | 48 | * ActionScheduler_QueueRunner constructor. |
| 34 | 49 | * |
| 35 | - * @param ActionScheduler_Store $store | |
| 36 | - * @param ActionScheduler_FatalErrorMonitor $monitor | |
| 37 | - * @param ActionScheduler_QueueCleaner $cleaner | |
| 50 | + * @param ActionScheduler_Store|null $store Store object. | |
| 51 | + * @param ActionScheduler_FatalErrorMonitor|null $monitor Monitor object. | |
| 52 | + * @param ActionScheduler_QueueCleaner|null $cleaner Cleaner object. | |
| 53 | + * @param ActionScheduler_AsyncRequest_QueueRunner|null $async_request Async request runner object. | |
| 38 | 54 | */ |
| 39 | - public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null, ActionScheduler_AsyncRequest_QueueRunner $async_request = null ) { | |
| 55 | + public function __construct( ?ActionScheduler_Store $store = null, ?ActionScheduler_FatalErrorMonitor $monitor = null, ?ActionScheduler_QueueCleaner $cleaner = null, ?ActionScheduler_AsyncRequest_QueueRunner $async_request = null ) { | |
| 40 | 56 | parent::__construct( $store, $monitor, $cleaner ); |
| 41 | 57 | |
| 42 | 58 | if ( is_null( $async_request ) ) { |
| 43 | 59 | $async_request = new ActionScheduler_AsyncRequest_QueueRunner( $this->store ); |
| @@ -46,15 +62,17 @@ | ||
| 46 | 62 | $this->async_request = $async_request; |
| 47 | 63 | } |
| 48 | 64 | |
| 49 | 65 | /** |
| 66 | + * Initialize. | |
| 67 | + * | |
| 50 | 68 | * @codeCoverageIgnore |
| 51 | 69 | */ |
| 52 | 70 | public function init() { |
| 53 | 71 | |
| 54 | - add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) ); | |
| 72 | + add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) ); // phpcs:ignore WordPress.WP.CronInterval.CronSchedulesInterval | |
| 55 | 73 | |
| 56 | - // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param | |
| 74 | + // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param. | |
| 57 | 75 | $next_timestamp = wp_next_scheduled( self::WP_CRON_HOOK ); |
| 58 | 76 | if ( $next_timestamp ) { |
| 59 | 77 | wp_unschedule_event( $next_timestamp, self::WP_CRON_HOOK ); |
| 60 | 78 | } |
| @@ -119,11 +137,12 @@ | ||
| 119 | 137 | * The $context param of this method defaults to 'WP Cron', because prior to Action Scheduler 3.0.0 |
| 120 | 138 | * that was the only context in which this method was run, and the self::WP_CRON_HOOK hook had no context |
| 121 | 139 | * passed along with it. New code calling this method directly, or by triggering the self::WP_CRON_HOOK, |
| 122 | 140 | * should set a context as the first parameter. For an example of this, refer to the code seen in |
| 141 | + * | |
| 123 | 142 | * @see ActionScheduler_AsyncRequest_QueueRunner::handle() |
| 124 | 143 | * |
| 125 | - * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' | |
| 144 | + * @param string $context Optional identifier for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' | |
| 126 | 145 | * Generally, this should be capitalised and not localised as it's a proper noun. |
| 127 | 146 | * @return int The number of actions processed. |
| 128 | 147 | */ |
| 129 | 148 | public function run( $context = 'WP Cron' ) { |
| @@ -133,13 +152,13 @@ | ||
| 133 | 152 | $this->run_cleanup(); |
| 134 | 153 | |
| 135 | 154 | $this->processed_actions_count = 0; |
| 136 | 155 | if ( false === $this->has_maximum_concurrent_batches() ) { |
| 137 | - $batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 ); | |
| 138 | 156 | do { |
| 157 | + $batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 ); | |
| 139 | 158 | $processed_actions_in_batch = $this->do_batch( $batch_size, $context ); |
| 140 | 159 | $this->processed_actions_count += $processed_actions_in_batch; |
| 141 | - } while ( $processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded( $this->processed_actions_count ) ); // keep going until we run out of actions, time, or memory | |
| 160 | + } while ( $processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded( $this->processed_actions_count ) ); // keep going until we run out of actions, time, or memory. | |
| 142 | 161 | } |
| 143 | 162 | |
| 144 | 163 | do_action( 'action_scheduler_after_process_queue' ); |
| 145 | 164 | return $this->processed_actions_count; |
| @@ -150,21 +169,21 @@ | ||
| 150 | 169 | * |
| 151 | 170 | * Actions are processed by claiming a set of pending actions then processing each one until either the batch |
| 152 | 171 | * size is completed, or memory or time limits are reached, defined by @see $this->batch_limits_exceeded(). |
| 153 | 172 | * |
| 154 | - * @param int $size The maximum number of actions to process in the batch. | |
| 155 | - * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' | |
| 156 | - * Generally, this should be capitalised and not localised as it's a proper noun. | |
| 173 | + * @param int $size The maximum number of actions to process in the batch. | |
| 174 | + * @param string $context Optional identifier for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron' | |
| 175 | + * Generally, this should be capitalised and not localised as it's a proper noun. | |
| 157 | 176 | * @return int The number of actions processed. |
| 158 | 177 | */ |
| 159 | 178 | protected function do_batch( $size = 100, $context = '' ) { |
| 160 | - $claim = $this->store->stake_claim($size); | |
| 161 | - $this->monitor->attach($claim); | |
| 179 | + $claim = $this->store->stake_claim( $size ); | |
| 180 | + $this->monitor->attach( $claim ); | |
| 162 | 181 | $processed_actions = 0; |
| 163 | 182 | |
| 164 | 183 | foreach ( $claim->get_actions() as $action_id ) { |
| 165 | - // bail if we lost the claim | |
| 166 | - if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) { | |
| 184 | + // bail if we lost the claim. | |
| 185 | + if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ), true ) ) { | |
| 167 | 186 | break; |
| 168 | 187 | } |
| 169 | 188 | $this->process_action( $action_id, $context ); |
| 170 | 189 | $processed_actions++; |
| @@ -172,9 +191,9 @@ | ||
| 172 | 191 | if ( $this->batch_limits_exceeded( $processed_actions + $this->processed_actions_count ) ) { |
| 173 | 192 | break; |
| 174 | 193 | } |
| 175 | 194 | } |
| 176 | - $this->store->release_claim($claim); | |
| 195 | + $this->store->release_claim( $claim ); | |
| 177 | 196 | $this->monitor->detach(); |
| 178 | 197 | $this->clear_caches(); |
| 179 | 198 | return $processed_actions; |
| 180 | 199 | } |
| @@ -217,11 +236,17 @@ | ||
| 217 | 236 | wp_cache_flush(); |
| 218 | 237 | } |
| 219 | 238 | } |
| 220 | 239 | |
| 240 | + /** | |
| 241 | + * Add schedule to WP cron. | |
| 242 | + * | |
| 243 | + * @param array<string, array<string, int|string>> $schedules Schedules. | |
| 244 | + * @return array<string, array<string, int|string>> | |
| 245 | + */ | |
| 221 | 246 | public function add_wp_cron_schedule( $schedules ) { |
| 222 | 247 | $schedules['every_minute'] = array( |
| 223 | - 'interval' => 60, // in seconds | |
| 248 | + 'interval' => 60, // in seconds. | |
| 224 | 249 | 'display' => __( 'Every minute', 'action-scheduler' ), |
| 225 | 250 | ); |
| 226 | 251 | |
| 227 | 252 | return $schedules; |