← All changes
|
vendor/woocommerce/action-scheduler/classes/ActionScheduler_QueueCleaner.php
+67
-46
1.2.1
→
1.7.7
View file →
| @@ -4,12 +4,20 @@ | ||
| 4 | 4 | * Class ActionScheduler_QueueCleaner |
| 5 | 5 | */ |
| 6 | 6 | class ActionScheduler_QueueCleaner { |
| 7 | 7 | |
| 8 | - /** @var int */ | |
| 8 | + /** | |
| 9 | + * The batch size. | |
| 10 | + * | |
| 11 | + * @var int | |
| 12 | + */ | |
| 9 | 13 | protected $batch_size; |
| 10 | 14 | |
| 11 | - /** @var ActionScheduler_Store */ | |
| 15 | + /** | |
| 16 | + * ActionScheduler_Store instance. | |
| 17 | + * | |
| 18 | + * @var ActionScheduler_Store | |
| 19 | + */ | |
| 12 | 20 | private $store = null; |
| 13 | 21 | |
| 14 | 22 | /** |
| 15 | 23 | * 31 days in seconds. |
| @@ -18,23 +26,25 @@ | ||
| 18 | 26 | */ |
| 19 | 27 | private $month_in_seconds = 2678400; |
| 20 | 28 | |
| 21 | 29 | /** |
| 22 | - * @var string[] Default list of statuses purged by the cleaner process. | |
| 30 | + * Default list of statuses purged by the cleaner process. | |
| 31 | + * | |
| 32 | + * @var string[] | |
| 23 | 33 | */ |
| 24 | - private $default_statuses_to_purge = [ | |
| 34 | + private $default_statuses_to_purge = array( | |
| 25 | 35 | ActionScheduler_Store::STATUS_COMPLETE, |
| 26 | 36 | ActionScheduler_Store::STATUS_CANCELED, |
| 27 | - ]; | |
| 37 | + ); | |
| 28 | 38 | |
| 29 | 39 | /** |
| 30 | 40 | * ActionScheduler_QueueCleaner constructor. |
| 31 | 41 | * |
| 32 | - * @param ActionScheduler_Store $store The store instance. | |
| 33 | - * @param int $batch_size The batch size. | |
| 42 | + * @param ActionScheduler_Store|null $store The store instance. | |
| 43 | + * @param int $batch_size The batch size. | |
| 34 | 44 | */ |
| 35 | - public function __construct( ActionScheduler_Store $store = null, $batch_size = 20 ) { | |
| 36 | - $this->store = $store ? $store : ActionScheduler_Store::instance(); | |
| 45 | + public function __construct( ?ActionScheduler_Store $store = null, $batch_size = 20 ) { | |
| 46 | + $this->store = $store ? $store : ActionScheduler_Store::instance(); | |
| 37 | 47 | $this->batch_size = $batch_size; |
| 38 | 48 | } |
| 39 | 49 | |
| 40 | 50 | /** |
| @@ -65,9 +75,8 @@ | ||
| 65 | 75 | |
| 66 | 76 | return array(); |
| 67 | 77 | } |
| 68 | 78 | |
| 69 | - | |
| 70 | 79 | /** |
| 71 | 80 | * Filter the statuses when cleaning the queue. |
| 72 | 81 | * |
| 73 | 82 | * @param string[] $default_statuses_to_purge Action statuses to clean. |
| @@ -82,28 +91,32 @@ | ||
| 82 | 91 | * |
| 83 | 92 | * @param string[] $statuses_to_purge List of action statuses to purge. Defaults to canceled, complete. |
| 84 | 93 | * @param DateTime $cutoff_date Date limit for selecting actions. Defaults to 31 days ago. |
| 85 | 94 | * @param int|null $batch_size Maximum number of actions per status to delete. Defaults to 20. |
| 86 | - * @param string $context Calling process context. Defaults to `old`. | |
| 95 | + * @param string $context Calling process context. Defaults to `old`. | |
| 87 | 96 | * @return array Actions deleted. |
| 88 | 97 | */ |
| 89 | 98 | public function clean_actions( array $statuses_to_purge, DateTime $cutoff_date, $batch_size = null, $context = 'old' ) { |
| 90 | - $batch_size = $batch_size !== null ? $batch_size : $this->batch_size; | |
| 91 | - $cutoff = $cutoff_date !== null ? $cutoff_date : as_get_datetime_object( $this->month_in_seconds . ' seconds ago' ); | |
| 99 | + $batch_size = ! is_null( $batch_size ) ? $batch_size : $this->batch_size; | |
| 100 | + $cutoff = ! is_null( $cutoff_date ) ? $cutoff_date : as_get_datetime_object( $this->month_in_seconds . ' seconds ago' ); | |
| 92 | 101 | $lifespan = time() - $cutoff->getTimestamp(); |
| 102 | + | |
| 93 | 103 | if ( empty( $statuses_to_purge ) ) { |
| 94 | 104 | $statuses_to_purge = $this->default_statuses_to_purge; |
| 95 | 105 | } |
| 96 | 106 | |
| 97 | - $deleted_actions = []; | |
| 107 | + $deleted_actions = array(); | |
| 108 | + | |
| 98 | 109 | foreach ( $statuses_to_purge as $status ) { |
| 99 | - $actions_to_delete = $this->store->query_actions( array( | |
| 100 | - 'status' => $status, | |
| 101 | - 'modified' => $cutoff, | |
| 102 | - 'modified_compare' => '<=', | |
| 103 | - 'per_page' => $batch_size, | |
| 104 | - 'orderby' => 'none', | |
| 105 | - ) ); | |
| 110 | + $actions_to_delete = $this->store->query_actions( | |
| 111 | + array( | |
| 112 | + 'status' => $status, | |
| 113 | + 'modified' => $cutoff, | |
| 114 | + 'modified_compare' => '<=', | |
| 115 | + 'per_page' => $batch_size, | |
| 116 | + 'orderby' => 'none', | |
| 117 | + ) | |
| 118 | + ); | |
| 106 | 119 | |
| 107 | 120 | $deleted_actions = array_merge( $deleted_actions, $this->delete_actions( $actions_to_delete, $lifespan, $context ) ); |
| 108 | 121 | } |
| 109 | 122 | |
| @@ -110,16 +123,19 @@ | ||
| 110 | 123 | return $deleted_actions; |
| 111 | 124 | } |
| 112 | 125 | |
| 113 | 126 | /** |
| 114 | - * @param int[] $actions_to_delete List of action IDs to delete. | |
| 115 | - * @param int $lifespan Minimum scheduled age in seconds of the actions being deleted. | |
| 127 | + * Delete actions. | |
| 128 | + * | |
| 129 | + * @param int[] $actions_to_delete List of action IDs to delete. | |
| 130 | + * @param int $lifespan Minimum scheduled age in seconds of the actions being deleted. | |
| 116 | 131 | * @param string $context Context of the delete request. |
| 117 | 132 | * @return array Deleted action IDs. |
| 118 | 133 | */ |
| 119 | 134 | private function delete_actions( array $actions_to_delete, $lifespan = null, $context = 'old' ) { |
| 120 | - $deleted_actions = []; | |
| 121 | - if ( $lifespan === null ) { | |
| 135 | + $deleted_actions = array(); | |
| 136 | + | |
| 137 | + if ( is_null( $lifespan ) ) { | |
| 122 | 138 | $lifespan = $this->month_in_seconds; |
| 123 | 139 | } |
| 124 | 140 | |
| 125 | 141 | foreach ( $actions_to_delete as $action_id ) { |
| @@ -137,9 +153,8 @@ | ||
| 137 | 153 | * @param Exception $e The exception thrown when attempting to delete the action from the data store |
| 138 | 154 | * @param int $lifespan The retention period, in seconds, for old actions |
| 139 | 155 | * @param int $count_of_actions_to_delete The number of old actions being deleted in this batch |
| 140 | 156 | * @since 2.0.0 |
| 141 | - * | |
| 142 | 157 | */ |
| 143 | 158 | do_action( "action_scheduler_failed_{$context}_action_deletion", $action_id, $e, $lifespan, count( $actions_to_delete ) ); |
| 144 | 159 | } |
| 145 | 160 | } |
| @@ -155,21 +170,25 @@ | ||
| 155 | 170 | * @param int $time_limit The number of seconds to allow a queue to run before unclaiming its pending actions. Default 300 (5 minutes). |
| 156 | 171 | */ |
| 157 | 172 | public function reset_timeouts( $time_limit = 300 ) { |
| 158 | 173 | $timeout = apply_filters( 'action_scheduler_timeout_period', $time_limit ); |
| 174 | + | |
| 159 | 175 | if ( $timeout < 0 ) { |
| 160 | 176 | return; |
| 161 | 177 | } |
| 162 | - $cutoff = as_get_datetime_object($timeout.' seconds ago'); | |
| 163 | - $actions_to_reset = $this->store->query_actions( array( | |
| 164 | - 'status' => ActionScheduler_Store::STATUS_PENDING, | |
| 165 | - 'modified' => $cutoff, | |
| 166 | - 'modified_compare' => '<=', | |
| 167 | - 'claimed' => true, | |
| 168 | - 'per_page' => $this->get_batch_size(), | |
| 169 | - 'orderby' => 'none', | |
| 170 | - ) ); | |
| 171 | 178 | |
| 179 | + $cutoff = as_get_datetime_object( $timeout . ' seconds ago' ); | |
| 180 | + $actions_to_reset = $this->store->query_actions( | |
| 181 | + array( | |
| 182 | + 'status' => ActionScheduler_Store::STATUS_PENDING, | |
| 183 | + 'modified' => $cutoff, | |
| 184 | + 'modified_compare' => '<=', | |
| 185 | + 'claimed' => true, | |
| 186 | + 'per_page' => $this->get_batch_size(), | |
| 187 | + 'orderby' => 'none', | |
| 188 | + ) | |
| 189 | + ); | |
| 190 | + | |
| 172 | 191 | foreach ( $actions_to_reset as $action_id ) { |
| 173 | 192 | $this->store->unclaim_action( $action_id ); |
| 174 | 193 | do_action( 'action_scheduler_reset_action', $action_id ); |
| 175 | 194 | } |
| @@ -176,9 +195,9 @@ | ||
| 176 | 195 | } |
| 177 | 196 | |
| 178 | 197 | /** |
| 179 | 198 | * Mark actions that have been running for more than a given time limit as failed, based on |
| 180 | - * the assumption some uncatachable and unloggable fatal error occurred during processing. | |
| 199 | + * the assumption some uncatchable and unloggable fatal error occurred during processing. | |
| 181 | 200 | * |
| 182 | 201 | * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed |
| 183 | 202 | * as a parameter is 10x the time limit used for queue processing. |
| 184 | 203 | * |
| @@ -185,20 +204,24 @@ | ||
| 185 | 204 | * @param int $time_limit The number of seconds to allow an action to run before it is considered to have failed. Default 300 (5 minutes). |
| 186 | 205 | */ |
| 187 | 206 | public function mark_failures( $time_limit = 300 ) { |
| 188 | 207 | $timeout = apply_filters( 'action_scheduler_failure_period', $time_limit ); |
| 208 | + | |
| 189 | 209 | if ( $timeout < 0 ) { |
| 190 | 210 | return; |
| 191 | 211 | } |
| 192 | - $cutoff = as_get_datetime_object($timeout.' seconds ago'); | |
| 193 | - $actions_to_reset = $this->store->query_actions( array( | |
| 194 | - 'status' => ActionScheduler_Store::STATUS_RUNNING, | |
| 195 | - 'modified' => $cutoff, | |
| 196 | - 'modified_compare' => '<=', | |
| 197 | - 'per_page' => $this->get_batch_size(), | |
| 198 | - 'orderby' => 'none', | |
| 199 | - ) ); | |
| 200 | 212 | |
| 213 | + $cutoff = as_get_datetime_object( $timeout . ' seconds ago' ); | |
| 214 | + $actions_to_reset = $this->store->query_actions( | |
| 215 | + array( | |
| 216 | + 'status' => ActionScheduler_Store::STATUS_RUNNING, | |
| 217 | + 'modified' => $cutoff, | |
| 218 | + 'modified_compare' => '<=', | |
| 219 | + 'per_page' => $this->get_batch_size(), | |
| 220 | + 'orderby' => 'none', | |
| 221 | + ) | |
| 222 | + ); | |
| 223 | + | |
| 201 | 224 | foreach ( $actions_to_reset as $action_id ) { |
| 202 | 225 | $this->store->mark_failure( $action_id ); |
| 203 | 226 | do_action( 'action_scheduler_failed_action', $action_id, $timeout ); |
| 204 | 227 | } |
| @@ -207,9 +230,8 @@ | ||
| 207 | 230 | /** |
| 208 | 231 | * Do all of the cleaning actions. |
| 209 | 232 | * |
| 210 | 233 | * @param int $time_limit The number of seconds to use as the timeout and failure period. Default 300 (5 minutes). |
| 211 | - * @author Jeremy Pry | |
| 212 | 234 | */ |
| 213 | 235 | public function clean( $time_limit = 300 ) { |
| 214 | 236 | $this->delete_old_actions(); |
| 215 | 237 | $this->reset_timeouts( $time_limit ); |
| @@ -218,9 +240,8 @@ | ||
| 218 | 240 | |
| 219 | 241 | /** |
| 220 | 242 | * Get the batch size for cleaning the queue. |
| 221 | 243 | * |
| 222 | - * @author Jeremy Pry | |
| 223 | 244 | * @return int |
| 224 | 245 | */ |
| 225 | 246 | protected function get_batch_size() { |
| 226 | 247 | /** |