fluentform
/
app
/
Services
/
Libraries
/
action-scheduler
/
classes
/
ActionScheduler_QueueCleaner.php
ActionScheduler_QueueCleaner.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.2, at app/Services/Libraries/action-scheduler/classes/ActionScheduler_QueueCleaner.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class ActionScheduler_QueueCleaner |
| 5 | */ |
| 6 | class ActionScheduler_QueueCleaner { |
| 7 | |
| 8 | /** |
| 9 | * The batch size. |
| 10 | * |
| 11 | * @var int |
| 12 | */ |
| 13 | protected $batch_size; |
| 14 | |
| 15 | /** |
| 16 | * ActionScheduler_Store instance. |
| 17 | * |
| 18 | * @var ActionScheduler_Store |
| 19 | */ |
| 20 | private $store = null; |
| 21 | |
| 22 | /** |
| 23 | * 31 days in seconds. |
| 24 | * |
| 25 | * @var int |
| 26 | */ |
| 27 | private $month_in_seconds = 2678400; |
| 28 | |
| 29 | /** |
| 30 | * Default list of statuses purged by the cleaner process. |
| 31 | * |
| 32 | * @var string[] |
| 33 | */ |
| 34 | private $default_statuses_to_purge = array( |
| 35 | ActionScheduler_Store::STATUS_COMPLETE, |
| 36 | ActionScheduler_Store::STATUS_CANCELED, |
| 37 | ); |
| 38 | |
| 39 | /** |
| 40 | * ActionScheduler_QueueCleaner constructor. |
| 41 | * |
| 42 | * @param ActionScheduler_Store|null $store The store instance. |
| 43 | * @param int $batch_size The batch size. |
| 44 | */ |
| 45 | public function __construct( ?ActionScheduler_Store $store = null, $batch_size = 20 ) { |
| 46 | $this->store = $store ? $store : ActionScheduler_Store::instance(); |
| 47 | $this->batch_size = $batch_size; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Default queue cleaner process used by queue runner. |
| 52 | * |
| 53 | * @return array |
| 54 | */ |
| 55 | public function delete_old_actions() { |
| 56 | /** |
| 57 | * Filter the minimum scheduled date age for action deletion. |
| 58 | * |
| 59 | * @param int $retention_period Minimum scheduled age in seconds of the actions to be deleted. |
| 60 | */ |
| 61 | $lifespan = apply_filters( 'action_scheduler_retention_period', $this->month_in_seconds ); |
| 62 | |
| 63 | try { |
| 64 | $cutoff = as_get_datetime_object( $lifespan . ' seconds ago' ); |
| 65 | } catch ( Exception $e ) { |
| 66 | _doing_it_wrong( |
| 67 | __METHOD__, |
| 68 | sprintf( |
| 69 | /* Translators: %s is the exception message. */ |
| 70 | esc_html__( 'It was not possible to determine a valid cut-off time: %s.', 'action-scheduler' ), |
| 71 | esc_html( $e->getMessage() ) |
| 72 | ), |
| 73 | '3.5.5' |
| 74 | ); |
| 75 | |
| 76 | return array(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Filter the statuses when cleaning the queue. |
| 81 | * |
| 82 | * @param string[] $default_statuses_to_purge Action statuses to clean. |
| 83 | */ |
| 84 | $statuses_to_purge = (array) apply_filters( 'action_scheduler_default_cleaner_statuses', $this->default_statuses_to_purge ); |
| 85 | |
| 86 | return $this->clean_actions( $statuses_to_purge, $cutoff, $this->get_batch_size() ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Delete selected actions limited by status and date. |
| 91 | * |
| 92 | * @param string[] $statuses_to_purge List of action statuses to purge. Defaults to canceled, complete. |
| 93 | * @param DateTime $cutoff_date Date limit for selecting actions. Defaults to 31 days ago. |
| 94 | * @param int|null $batch_size Maximum number of actions per status to delete. Defaults to 20. |
| 95 | * @param string $context Calling process context. Defaults to `old`. |
| 96 | * @return array Actions deleted. |
| 97 | */ |
| 98 | public function clean_actions( array $statuses_to_purge, DateTime $cutoff_date, $batch_size = null, $context = 'old' ) { |
| 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' ); |
| 101 | $lifespan = time() - $cutoff->getTimestamp(); |
| 102 | |
| 103 | if ( empty( $statuses_to_purge ) ) { |
| 104 | $statuses_to_purge = $this->default_statuses_to_purge; |
| 105 | } |
| 106 | |
| 107 | $deleted_actions = array(); |
| 108 | |
| 109 | foreach ( $statuses_to_purge as $status ) { |
| 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 | ); |
| 119 | |
| 120 | $deleted_actions = array_merge( $deleted_actions, $this->delete_actions( $actions_to_delete, $lifespan, $context ) ); |
| 121 | } |
| 122 | |
| 123 | return $deleted_actions; |
| 124 | } |
| 125 | |
| 126 | /** |
| 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. |
| 131 | * @param string $context Context of the delete request. |
| 132 | * @return array Deleted action IDs. |
| 133 | */ |
| 134 | private function delete_actions( array $actions_to_delete, $lifespan = null, $context = 'old' ) { |
| 135 | $deleted_actions = array(); |
| 136 | |
| 137 | if ( is_null( $lifespan ) ) { |
| 138 | $lifespan = $this->month_in_seconds; |
| 139 | } |
| 140 | |
| 141 | foreach ( $actions_to_delete as $action_id ) { |
| 142 | try { |
| 143 | $this->store->delete_action( $action_id ); |
| 144 | $deleted_actions[] = $action_id; |
| 145 | } catch ( Exception $e ) { |
| 146 | /** |
| 147 | * Notify 3rd party code of exceptions when deleting a completed action older than the retention period |
| 148 | * |
| 149 | * This hook provides a way for 3rd party code to log or otherwise handle exceptions relating to their |
| 150 | * actions. |
| 151 | * |
| 152 | * @param int $action_id The scheduled actions ID in the data store |
| 153 | * @param Exception $e The exception thrown when attempting to delete the action from the data store |
| 154 | * @param int $lifespan The retention period, in seconds, for old actions |
| 155 | * @param int $count_of_actions_to_delete The number of old actions being deleted in this batch |
| 156 | * @since 2.0.0 |
| 157 | */ |
| 158 | do_action( "action_scheduler_failed_{$context}_action_deletion", $action_id, $e, $lifespan, count( $actions_to_delete ) ); |
| 159 | } |
| 160 | } |
| 161 | return $deleted_actions; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Unclaim pending actions that have not been run within a given time limit. |
| 166 | * |
| 167 | * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed |
| 168 | * as a parameter is 10x the time limit used for queue processing. |
| 169 | * |
| 170 | * @param int $time_limit The number of seconds to allow a queue to run before unclaiming its pending actions. Default 300 (5 minutes). |
| 171 | */ |
| 172 | public function reset_timeouts( $time_limit = 300 ) { |
| 173 | $timeout = apply_filters( 'action_scheduler_timeout_period', $time_limit ); |
| 174 | |
| 175 | if ( $timeout < 0 ) { |
| 176 | return; |
| 177 | } |
| 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 | |
| 191 | foreach ( $actions_to_reset as $action_id ) { |
| 192 | $this->store->unclaim_action( $action_id ); |
| 193 | do_action( 'action_scheduler_reset_action', $action_id ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Mark actions that have been running for more than a given time limit as failed, based on |
| 199 | * the assumption some uncatchable and unloggable fatal error occurred during processing. |
| 200 | * |
| 201 | * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed |
| 202 | * as a parameter is 10x the time limit used for queue processing. |
| 203 | * |
| 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). |
| 205 | */ |
| 206 | public function mark_failures( $time_limit = 300 ) { |
| 207 | $timeout = apply_filters( 'action_scheduler_failure_period', $time_limit ); |
| 208 | |
| 209 | if ( $timeout < 0 ) { |
| 210 | return; |
| 211 | } |
| 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 | |
| 224 | foreach ( $actions_to_reset as $action_id ) { |
| 225 | $this->store->mark_failure( $action_id ); |
| 226 | do_action( 'action_scheduler_failed_action', $action_id, $timeout ); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Do all of the cleaning actions. |
| 232 | * |
| 233 | * @param int $time_limit The number of seconds to use as the timeout and failure period. Default 300 (5 minutes). |
| 234 | */ |
| 235 | public function clean( $time_limit = 300 ) { |
| 236 | $this->delete_old_actions(); |
| 237 | $this->reset_timeouts( $time_limit ); |
| 238 | $this->mark_failures( $time_limit ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Get the batch size for cleaning the queue. |
| 243 | * |
| 244 | * @return int |
| 245 | */ |
| 246 | protected function get_batch_size() { |
| 247 | /** |
| 248 | * Filter the batch size when cleaning the queue. |
| 249 | * |
| 250 | * @param int $batch_size The number of actions to clean in one batch. |
| 251 | */ |
| 252 | return absint( apply_filters( 'action_scheduler_cleanup_batch_size', $this->batch_size ) ); |
| 253 | } |
| 254 | } |
| 255 |