PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / trunk
Image Optimizer – Compress Images and Convert to WebP or AVIF vtrunk
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
← All changes | vendor/woocommerce/action-scheduler/classes/ActionScheduler_QueueCleaner.php +61 -41 1.5.3 → trunk 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 /**
@@ -85,24 +95,28 @@
85 95 * @param string $context Calling process context. Defaults to `old`.
86 96 * @return array Actions deleted.
87 97 */
88 98 public function clean_actions( array $statuses_to_purge, DateTime $cutoff_date, $batch_size = null, $context = 'old' ) {
89 - $batch_size = $batch_size !== null ? $batch_size : $this->batch_size;
90 - $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' );
91 101 $lifespan = time() - $cutoff->getTimestamp();
102 +
92 103 if ( empty( $statuses_to_purge ) ) {
93 104 $statuses_to_purge = $this->default_statuses_to_purge;
94 105 }
95 106
96 - $deleted_actions = [];
107 + $deleted_actions = array();
108 +
97 109 foreach ( $statuses_to_purge as $status ) {
98 - $actions_to_delete = $this->store->query_actions( array(
99 - 'status' => $status,
100 - 'modified' => $cutoff,
101 - 'modified_compare' => '<=',
102 - 'per_page' => $batch_size,
103 - 'orderby' => 'none',
104 - ) );
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 + );
105 119
106 120 $deleted_actions = array_merge( $deleted_actions, $this->delete_actions( $actions_to_delete, $lifespan, $context ) );
107 121 }
108 122
@@ -117,10 +131,11 @@
117 131 * @param string $context Context of the delete request.
118 132 * @return array Deleted action IDs.
119 133 */
120 134 private function delete_actions( array $actions_to_delete, $lifespan = null, $context = 'old' ) {
121 - $deleted_actions = [];
122 - if ( $lifespan === null ) {
135 + $deleted_actions = array();
136 +
137 + if ( is_null( $lifespan ) ) {
123 138 $lifespan = $this->month_in_seconds;
124 139 }
125 140
126 141 foreach ( $actions_to_delete as $action_id ) {
@@ -138,9 +153,8 @@
138 153 * @param Exception $e The exception thrown when attempting to delete the action from the data store
139 154 * @param int $lifespan The retention period, in seconds, for old actions
140 155 * @param int $count_of_actions_to_delete The number of old actions being deleted in this batch
141 156 * @since 2.0.0
142 - *
143 157 */
144 158 do_action( "action_scheduler_failed_{$context}_action_deletion", $action_id, $e, $lifespan, count( $actions_to_delete ) );
145 159 }
146 160 }
@@ -156,21 +170,25 @@
156 170 * @param int $time_limit The number of seconds to allow a queue to run before unclaiming its pending actions. Default 300 (5 minutes).
157 171 */
158 172 public function reset_timeouts( $time_limit = 300 ) {
159 173 $timeout = apply_filters( 'action_scheduler_timeout_period', $time_limit );
174 +
160 175 if ( $timeout < 0 ) {
161 176 return;
162 177 }
163 - $cutoff = as_get_datetime_object($timeout . ' seconds ago');
164 - $actions_to_reset = $this->store->query_actions( array(
165 - 'status' => ActionScheduler_Store::STATUS_PENDING,
166 - 'modified' => $cutoff,
167 - 'modified_compare' => '<=',
168 - 'claimed' => true,
169 - 'per_page' => $this->get_batch_size(),
170 - 'orderby' => 'none',
171 - ) );
172 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 +
173 191 foreach ( $actions_to_reset as $action_id ) {
174 192 $this->store->unclaim_action( $action_id );
175 193 do_action( 'action_scheduler_reset_action', $action_id );
176 194 }
@@ -186,20 +204,24 @@
186 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).
187 205 */
188 206 public function mark_failures( $time_limit = 300 ) {
189 207 $timeout = apply_filters( 'action_scheduler_failure_period', $time_limit );
208 +
190 209 if ( $timeout < 0 ) {
191 210 return;
192 211 }
193 - $cutoff = as_get_datetime_object($timeout . ' seconds ago');
194 - $actions_to_reset = $this->store->query_actions( array(
195 - 'status' => ActionScheduler_Store::STATUS_RUNNING,
196 - 'modified' => $cutoff,
197 - 'modified_compare' => '<=',
198 - 'per_page' => $this->get_batch_size(),
199 - 'orderby' => 'none',
200 - ) );
201 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 +
202 224 foreach ( $actions_to_reset as $action_id ) {
203 225 $this->store->mark_failure( $action_id );
204 226 do_action( 'action_scheduler_failed_action', $action_id, $timeout );
205 227 }
@@ -208,9 +230,8 @@
208 230 /**
209 231 * Do all of the cleaning actions.
210 232 *
211 233 * @param int $time_limit The number of seconds to use as the timeout and failure period. Default 300 (5 minutes).
212 - * @author Jeremy Pry
213 234 */
214 235 public function clean( $time_limit = 300 ) {
215 236 $this->delete_old_actions();
216 237 $this->reset_timeouts( $time_limit );
@@ -219,9 +240,8 @@
219 240
220 241 /**
221 242 * Get the batch size for cleaning the queue.
222 243 *
223 - * @author Jeremy Pry
224 244 * @return int
225 245 */
226 246 protected function get_batch_size() {
227 247 /**