WP_CLI
1 year ago
abstracts
1 year ago
actions
1 year ago
data-stores
1 year ago
migration
1 year ago
schedules
1 year ago
schema
1 year ago
ActionScheduler_ActionClaim.php
1 year ago
ActionScheduler_ActionFactory.php
1 year ago
ActionScheduler_AdminView.php
1 year ago
ActionScheduler_AsyncRequest_QueueRunner.php
1 year ago
ActionScheduler_Compatibility.php
1 year ago
ActionScheduler_DataController.php
1 year ago
ActionScheduler_DateTime.php
1 year ago
ActionScheduler_Exception.php
4 years ago
ActionScheduler_FatalErrorMonitor.php
1 year ago
ActionScheduler_InvalidActionException.php
1 year ago
ActionScheduler_ListTable.php
1 year ago
ActionScheduler_LogEntry.php
1 year ago
ActionScheduler_NullLogEntry.php
1 year ago
ActionScheduler_OptionLock.php
2 years ago
ActionScheduler_QueueCleaner.php
1 year ago
ActionScheduler_QueueRunner.php
1 year ago
ActionScheduler_SystemInformation.php
1 year ago
ActionScheduler_Versions.php
1 year ago
ActionScheduler_WPCommentCleaner.php
1 year ago
ActionScheduler_wcSystemStatus.php
4 years ago
index.php
3 years ago
ActionScheduler_QueueCleaner.php
120 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class ActionScheduler_QueueCleaner { |
| 4 | protected $batch_size; |
| 5 | private $store = null; |
| 6 | private $month_in_seconds = 2678400; |
| 7 | private $default_statuses_to_purge = array( |
| 8 | ActionScheduler_Store::STATUS_COMPLETE, |
| 9 | ActionScheduler_Store::STATUS_CANCELED, |
| 10 | ); |
| 11 | public function __construct( ?ActionScheduler_Store $store = null, $batch_size = 20 ) { |
| 12 | $this->store = $store ? $store : ActionScheduler_Store::instance(); |
| 13 | $this->batch_size = $batch_size; |
| 14 | } |
| 15 | public function delete_old_actions() { |
| 16 | $lifespan = apply_filters( 'action_scheduler_retention_period', $this->month_in_seconds ); |
| 17 | try { |
| 18 | $cutoff = as_get_datetime_object( $lifespan . ' seconds ago' ); |
| 19 | } catch ( Exception $e ) { |
| 20 | _doing_it_wrong( |
| 21 | __METHOD__, |
| 22 | sprintf( |
| 23 | esc_html__( 'It was not possible to determine a valid cut-off time: %s.', 'action-scheduler' ), |
| 24 | esc_html( $e->getMessage() ) |
| 25 | ), |
| 26 | '3.5.5' |
| 27 | ); |
| 28 | return array(); |
| 29 | } |
| 30 | $statuses_to_purge = (array) apply_filters( 'action_scheduler_default_cleaner_statuses', $this->default_statuses_to_purge ); |
| 31 | return $this->clean_actions( $statuses_to_purge, $cutoff, $this->get_batch_size() ); |
| 32 | } |
| 33 | public function clean_actions( array $statuses_to_purge, DateTime $cutoff_date, $batch_size = null, $context = 'old' ) { |
| 34 | $batch_size = ! is_null( $batch_size ) ? $batch_size : $this->batch_size; |
| 35 | $cutoff = ! is_null( $cutoff_date ) ? $cutoff_date : as_get_datetime_object( $this->month_in_seconds . ' seconds ago' ); |
| 36 | $lifespan = time() - $cutoff->getTimestamp(); |
| 37 | if ( empty( $statuses_to_purge ) ) { |
| 38 | $statuses_to_purge = $this->default_statuses_to_purge; |
| 39 | } |
| 40 | $deleted_actions = array(); |
| 41 | foreach ( $statuses_to_purge as $status ) { |
| 42 | $actions_to_delete = $this->store->query_actions( |
| 43 | array( |
| 44 | 'status' => $status, |
| 45 | 'modified' => $cutoff, |
| 46 | 'modified_compare' => '<=', |
| 47 | 'per_page' => $batch_size, |
| 48 | 'orderby' => 'none', |
| 49 | ) |
| 50 | ); |
| 51 | $deleted_actions = array_merge( $deleted_actions, $this->delete_actions( $actions_to_delete, $lifespan, $context ) ); |
| 52 | } |
| 53 | return $deleted_actions; |
| 54 | } |
| 55 | private function delete_actions( array $actions_to_delete, $lifespan = null, $context = 'old' ) { |
| 56 | $deleted_actions = array(); |
| 57 | if ( is_null( $lifespan ) ) { |
| 58 | $lifespan = $this->month_in_seconds; |
| 59 | } |
| 60 | foreach ( $actions_to_delete as $action_id ) { |
| 61 | try { |
| 62 | $this->store->delete_action( $action_id ); |
| 63 | $deleted_actions[] = $action_id; |
| 64 | } catch ( Exception $e ) { |
| 65 | do_action( "action_scheduler_failed_{$context}_action_deletion", $action_id, $e, $lifespan, count( $actions_to_delete ) ); |
| 66 | } |
| 67 | } |
| 68 | return $deleted_actions; |
| 69 | } |
| 70 | public function reset_timeouts( $time_limit = 300 ) { |
| 71 | $timeout = apply_filters( 'action_scheduler_timeout_period', $time_limit ); |
| 72 | if ( $timeout < 0 ) { |
| 73 | return; |
| 74 | } |
| 75 | $cutoff = as_get_datetime_object( $timeout . ' seconds ago' ); |
| 76 | $actions_to_reset = $this->store->query_actions( |
| 77 | array( |
| 78 | 'status' => ActionScheduler_Store::STATUS_PENDING, |
| 79 | 'modified' => $cutoff, |
| 80 | 'modified_compare' => '<=', |
| 81 | 'claimed' => true, |
| 82 | 'per_page' => $this->get_batch_size(), |
| 83 | 'orderby' => 'none', |
| 84 | ) |
| 85 | ); |
| 86 | foreach ( $actions_to_reset as $action_id ) { |
| 87 | $this->store->unclaim_action( $action_id ); |
| 88 | do_action( 'action_scheduler_reset_action', $action_id ); |
| 89 | } |
| 90 | } |
| 91 | public function mark_failures( $time_limit = 300 ) { |
| 92 | $timeout = apply_filters( 'action_scheduler_failure_period', $time_limit ); |
| 93 | if ( $timeout < 0 ) { |
| 94 | return; |
| 95 | } |
| 96 | $cutoff = as_get_datetime_object( $timeout . ' seconds ago' ); |
| 97 | $actions_to_reset = $this->store->query_actions( |
| 98 | array( |
| 99 | 'status' => ActionScheduler_Store::STATUS_RUNNING, |
| 100 | 'modified' => $cutoff, |
| 101 | 'modified_compare' => '<=', |
| 102 | 'per_page' => $this->get_batch_size(), |
| 103 | 'orderby' => 'none', |
| 104 | ) |
| 105 | ); |
| 106 | foreach ( $actions_to_reset as $action_id ) { |
| 107 | $this->store->mark_failure( $action_id ); |
| 108 | do_action( 'action_scheduler_failed_action', $action_id, $timeout ); |
| 109 | } |
| 110 | } |
| 111 | public function clean( $time_limit = 300 ) { |
| 112 | $this->delete_old_actions(); |
| 113 | $this->reset_timeouts( $time_limit ); |
| 114 | $this->mark_failures( $time_limit ); |
| 115 | } |
| 116 | protected function get_batch_size() { |
| 117 | return absint( apply_filters( 'action_scheduler_cleanup_batch_size', $this->batch_size ) ); |
| 118 | } |
| 119 | } |
| 120 |