PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.4
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
image-optimization / vendor / woocommerce / action-scheduler / classes / ActionScheduler_QueueCleaner.php

ActionScheduler_QueueCleaner.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.4, at vendor/woocommerce/action-scheduler/classes/ActionScheduler_QueueCleaner.php

235 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class ActionScheduler_QueueCleaner
5 */
6 class ActionScheduler_QueueCleaner {
7
8 /** @var int */
9 protected $batch_size;
10
11 /** @var ActionScheduler_Store */
12 private $store = null;
13
14 /**
15 * 31 days in seconds.
16 *
17 * @var int
18 */
19 private $month_in_seconds = 2678400;
20
21 /**
22 * @var string[] Default list of statuses purged by the cleaner process.
23 */
24 private $default_statuses_to_purge = [
25 ActionScheduler_Store::STATUS_COMPLETE,
26 ActionScheduler_Store::STATUS_CANCELED,
27 ];
28
29 /**
30 * ActionScheduler_QueueCleaner constructor.
31 *
32 * @param ActionScheduler_Store $store The store instance.
33 * @param int $batch_size The batch size.
34 */
35 public function __construct( ActionScheduler_Store $store = null, $batch_size = 20 ) {
36 $this->store = $store ? $store : ActionScheduler_Store::instance();
37 $this->batch_size = $batch_size;
38 }
39
40 /**
41 * Default queue cleaner process used by queue runner.
42 *
43 * @return array
44 */
45 public function delete_old_actions() {
46 /**
47 * Filter the minimum scheduled date age for action deletion.
48 *
49 * @param int $retention_period Minimum scheduled age in seconds of the actions to be deleted.
50 */
51 $lifespan = apply_filters( 'action_scheduler_retention_period', $this->month_in_seconds );
52
53 try {
54 $cutoff = as_get_datetime_object( $lifespan . ' seconds ago' );
55 } catch ( Exception $e ) {
56 _doing_it_wrong(
57 __METHOD__,
58 sprintf(
59 /* Translators: %s is the exception message. */
60 esc_html__( 'It was not possible to determine a valid cut-off time: %s.', 'action-scheduler' ),
61 esc_html( $e->getMessage() )
62 ),
63 '3.5.5'
64 );
65
66 return array();
67 }
68
69 /**
70 * Filter the statuses when cleaning the queue.
71 *
72 * @param string[] $default_statuses_to_purge Action statuses to clean.
73 */
74 $statuses_to_purge = (array) apply_filters( 'action_scheduler_default_cleaner_statuses', $this->default_statuses_to_purge );
75
76 return $this->clean_actions( $statuses_to_purge, $cutoff, $this->get_batch_size() );
77 }
78
79 /**
80 * Delete selected actions limited by status and date.
81 *
82 * @param string[] $statuses_to_purge List of action statuses to purge. Defaults to canceled, complete.
83 * @param DateTime $cutoff_date Date limit for selecting actions. Defaults to 31 days ago.
84 * @param int|null $batch_size Maximum number of actions per status to delete. Defaults to 20.
85 * @param string $context Calling process context. Defaults to `old`.
86 * @return array Actions deleted.
87 */
88 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' );
91 $lifespan = time() - $cutoff->getTimestamp();
92 if ( empty( $statuses_to_purge ) ) {
93 $statuses_to_purge = $this->default_statuses_to_purge;
94 }
95
96 $deleted_actions = [];
97 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 ) );
105
106 $deleted_actions = array_merge( $deleted_actions, $this->delete_actions( $actions_to_delete, $lifespan, $context ) );
107 }
108
109 return $deleted_actions;
110 }
111
112 /**
113 * Delete actions.
114 *
115 * @param int[] $actions_to_delete List of action IDs to delete.
116 * @param int $lifespan Minimum scheduled age in seconds of the actions being deleted.
117 * @param string $context Context of the delete request.
118 * @return array Deleted action IDs.
119 */
120 private function delete_actions( array $actions_to_delete, $lifespan = null, $context = 'old' ) {
121 $deleted_actions = [];
122 if ( $lifespan === null ) {
123 $lifespan = $this->month_in_seconds;
124 }
125
126 foreach ( $actions_to_delete as $action_id ) {
127 try {
128 $this->store->delete_action( $action_id );
129 $deleted_actions[] = $action_id;
130 } catch ( Exception $e ) {
131 /**
132 * Notify 3rd party code of exceptions when deleting a completed action older than the retention period
133 *
134 * This hook provides a way for 3rd party code to log or otherwise handle exceptions relating to their
135 * actions.
136 *
137 * @param int $action_id The scheduled actions ID in the data store
138 * @param Exception $e The exception thrown when attempting to delete the action from the data store
139 * @param int $lifespan The retention period, in seconds, for old actions
140 * @param int $count_of_actions_to_delete The number of old actions being deleted in this batch
141 * @since 2.0.0
142 *
143 */
144 do_action( "action_scheduler_failed_{$context}_action_deletion", $action_id, $e, $lifespan, count( $actions_to_delete ) );
145 }
146 }
147 return $deleted_actions;
148 }
149
150 /**
151 * Unclaim pending actions that have not been run within a given time limit.
152 *
153 * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed
154 * as a parameter is 10x the time limit used for queue processing.
155 *
156 * @param int $time_limit The number of seconds to allow a queue to run before unclaiming its pending actions. Default 300 (5 minutes).
157 */
158 public function reset_timeouts( $time_limit = 300 ) {
159 $timeout = apply_filters( 'action_scheduler_timeout_period', $time_limit );
160 if ( $timeout < 0 ) {
161 return;
162 }
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
173 foreach ( $actions_to_reset as $action_id ) {
174 $this->store->unclaim_action( $action_id );
175 do_action( 'action_scheduler_reset_action', $action_id );
176 }
177 }
178
179 /**
180 * Mark actions that have been running for more than a given time limit as failed, based on
181 * the assumption some uncatchable and unloggable fatal error occurred during processing.
182 *
183 * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed
184 * as a parameter is 10x the time limit used for queue processing.
185 *
186 * @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 */
188 public function mark_failures( $time_limit = 300 ) {
189 $timeout = apply_filters( 'action_scheduler_failure_period', $time_limit );
190 if ( $timeout < 0 ) {
191 return;
192 }
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
202 foreach ( $actions_to_reset as $action_id ) {
203 $this->store->mark_failure( $action_id );
204 do_action( 'action_scheduler_failed_action', $action_id, $timeout );
205 }
206 }
207
208 /**
209 * Do all of the cleaning actions.
210 *
211 * @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 */
214 public function clean( $time_limit = 300 ) {
215 $this->delete_old_actions();
216 $this->reset_timeouts( $time_limit );
217 $this->mark_failures( $time_limit );
218 }
219
220 /**
221 * Get the batch size for cleaning the queue.
222 *
223 * @author Jeremy Pry
224 * @return int
225 */
226 protected function get_batch_size() {
227 /**
228 * Filter the batch size when cleaning the queue.
229 *
230 * @param int $batch_size The number of actions to clean in one batch.
231 */
232 return absint( apply_filters( 'action_scheduler_cleanup_batch_size', $this->batch_size ) );
233 }
234 }
235