WP_CLI
2 years ago
abstracts
2 years ago
actions
2 years ago
data-stores
2 years ago
migration
2 years ago
schedules
2 years ago
schema
2 years ago
ActionScheduler_ActionClaim.php
2 years ago
ActionScheduler_ActionFactory.php
2 years ago
ActionScheduler_AdminView.php
2 years ago
ActionScheduler_AsyncRequest_QueueRunner.php
2 years ago
ActionScheduler_Compatibility.php
2 years ago
ActionScheduler_DataController.php
2 years ago
ActionScheduler_DateTime.php
2 years ago
ActionScheduler_Exception.php
2 years ago
ActionScheduler_FatalErrorMonitor.php
2 years ago
ActionScheduler_InvalidActionException.php
2 years ago
ActionScheduler_ListTable.php
2 years ago
ActionScheduler_LogEntry.php
2 years ago
ActionScheduler_NullLogEntry.php
2 years ago
ActionScheduler_OptionLock.php
2 years ago
ActionScheduler_QueueCleaner.php
2 years ago
ActionScheduler_QueueRunner.php
2 years ago
ActionScheduler_Versions.php
2 years ago
ActionScheduler_WPCommentCleaner.php
2 years ago
ActionScheduler_wcSystemStatus.php
2 years ago
ActionScheduler_QueueCleaner.php
234 lines
| 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 | /** |
| 71 | * Filter the statuses when cleaning the queue. |
| 72 | * |
| 73 | * @param string[] $default_statuses_to_purge Action statuses to clean. |
| 74 | */ |
| 75 | $statuses_to_purge = (array) apply_filters( 'action_scheduler_default_cleaner_statuses', $this->default_statuses_to_purge ); |
| 76 | |
| 77 | return $this->clean_actions( $statuses_to_purge, $cutoff, $this->get_batch_size() ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Delete selected actions limited by status and date. |
| 82 | * |
| 83 | * @param string[] $statuses_to_purge List of action statuses to purge. Defaults to canceled, complete. |
| 84 | * @param DateTime $cutoff_date Date limit for selecting actions. Defaults to 31 days ago. |
| 85 | * @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`. |
| 87 | * @return array Actions deleted. |
| 88 | */ |
| 89 | 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' ); |
| 92 | $lifespan = time() - $cutoff->getTimestamp(); |
| 93 | if ( empty( $statuses_to_purge ) ) { |
| 94 | $statuses_to_purge = $this->default_statuses_to_purge; |
| 95 | } |
| 96 | |
| 97 | $deleted_actions = []; |
| 98 | 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 | ) ); |
| 106 | |
| 107 | $deleted_actions = array_merge( $deleted_actions, $this->delete_actions( $actions_to_delete, $lifespan, $context ) ); |
| 108 | } |
| 109 | |
| 110 | return $deleted_actions; |
| 111 | } |
| 112 | |
| 113 | /** |
| 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. |
| 116 | * @param string $context Context of the delete request. |
| 117 | * @return array Deleted action IDs. |
| 118 | */ |
| 119 | private function delete_actions( array $actions_to_delete, $lifespan = null, $context = 'old' ) { |
| 120 | $deleted_actions = []; |
| 121 | if ( $lifespan === null ) { |
| 122 | $lifespan = $this->month_in_seconds; |
| 123 | } |
| 124 | |
| 125 | foreach ( $actions_to_delete as $action_id ) { |
| 126 | try { |
| 127 | $this->store->delete_action( $action_id ); |
| 128 | $deleted_actions[] = $action_id; |
| 129 | } catch ( Exception $e ) { |
| 130 | /** |
| 131 | * Notify 3rd party code of exceptions when deleting a completed action older than the retention period |
| 132 | * |
| 133 | * This hook provides a way for 3rd party code to log or otherwise handle exceptions relating to their |
| 134 | * actions. |
| 135 | * |
| 136 | * @param int $action_id The scheduled actions ID in the data store |
| 137 | * @param Exception $e The exception thrown when attempting to delete the action from the data store |
| 138 | * @param int $lifespan The retention period, in seconds, for old actions |
| 139 | * @param int $count_of_actions_to_delete The number of old actions being deleted in this batch |
| 140 | * @since 2.0.0 |
| 141 | * |
| 142 | */ |
| 143 | do_action( "action_scheduler_failed_{$context}_action_deletion", $action_id, $e, $lifespan, count( $actions_to_delete ) ); |
| 144 | } |
| 145 | } |
| 146 | return $deleted_actions; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Unclaim pending actions that have not been run within a given time limit. |
| 151 | * |
| 152 | * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed |
| 153 | * as a parameter is 10x the time limit used for queue processing. |
| 154 | * |
| 155 | * @param int $time_limit The number of seconds to allow a queue to run before unclaiming its pending actions. Default 300 (5 minutes). |
| 156 | */ |
| 157 | public function reset_timeouts( $time_limit = 300 ) { |
| 158 | $timeout = apply_filters( 'action_scheduler_timeout_period', $time_limit ); |
| 159 | if ( $timeout < 0 ) { |
| 160 | return; |
| 161 | } |
| 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 | |
| 172 | foreach ( $actions_to_reset as $action_id ) { |
| 173 | $this->store->unclaim_action( $action_id ); |
| 174 | do_action( 'action_scheduler_reset_action', $action_id ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * 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. |
| 181 | * |
| 182 | * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed |
| 183 | * as a parameter is 10x the time limit used for queue processing. |
| 184 | * |
| 185 | * @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 | */ |
| 187 | public function mark_failures( $time_limit = 300 ) { |
| 188 | $timeout = apply_filters( 'action_scheduler_failure_period', $time_limit ); |
| 189 | if ( $timeout < 0 ) { |
| 190 | return; |
| 191 | } |
| 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 | |
| 201 | foreach ( $actions_to_reset as $action_id ) { |
| 202 | $this->store->mark_failure( $action_id ); |
| 203 | do_action( 'action_scheduler_failed_action', $action_id, $timeout ); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Do all of the cleaning actions. |
| 209 | * |
| 210 | * @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 | */ |
| 213 | public function clean( $time_limit = 300 ) { |
| 214 | $this->delete_old_actions(); |
| 215 | $this->reset_timeouts( $time_limit ); |
| 216 | $this->mark_failures( $time_limit ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Get the batch size for cleaning the queue. |
| 221 | * |
| 222 | * @author Jeremy Pry |
| 223 | * @return int |
| 224 | */ |
| 225 | protected function get_batch_size() { |
| 226 | /** |
| 227 | * Filter the batch size when cleaning the queue. |
| 228 | * |
| 229 | * @param int $batch_size The number of actions to clean in one batch. |
| 230 | */ |
| 231 | return absint( apply_filters( 'action_scheduler_cleanup_batch_size', $this->batch_size ) ); |
| 232 | } |
| 233 | } |
| 234 |