PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.4 3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 All 262 releases
wpfunnels / packages / action-scheduler / classes / ActionScheduler_QueueCleaner.php

ActionScheduler_QueueCleaner.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at packages/action-scheduler/classes/ActionScheduler_QueueCleaner.php

163 lines 5.3 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 /**
9 * @var int
10 */
11 protected $batch_size;
12
13 /**
14 * @var ActionScheduler_Store
15 */
16 private $store = null;
17
18 /**
19 * 31 days in seconds.
20 *
21 * @var int
22 */
23 private $month_in_seconds = 2678400;
24
25 /**
26 * ActionScheduler_QueueCleaner constructor.
27 *
28 * @param ActionScheduler_Store $store The store instance.
29 * @param int $batch_size The batch size.
30 */
31 public function __construct( ActionScheduler_Store $store = null, $batch_size = 20 ) {
32 $this->store = $store ? $store : ActionScheduler_Store::instance();
33 $this->batch_size = $batch_size;
34 }
35
36 public function delete_old_actions() {
37 $lifespan = apply_filters( 'action_scheduler_retention_period', $this->month_in_seconds );
38 $cutoff = as_get_datetime_object($lifespan.' seconds ago');
39
40 $statuses_to_purge = array(
41 ActionScheduler_Store::STATUS_COMPLETE,
42 ActionScheduler_Store::STATUS_CANCELED,
43 );
44
45 foreach ( $statuses_to_purge as $status ) {
46 $actions_to_delete = $this->store->query_actions( array(
47 'status' => $status,
48 'modified' => $cutoff,
49 'modified_compare' => '<=',
50 'per_page' => $this->get_batch_size(),
51 'orderby' => 'none',
52 ) );
53
54 foreach ( $actions_to_delete as $action_id ) {
55 try {
56 $this->store->delete_action( $action_id );
57 } catch ( Exception $e ) {
58
59 /**
60 * Notify 3rd party code of exceptions when deleting a completed action older than the retention period
61 *
62 * This hook provides a way for 3rd party code to log or otherwise handle exceptions relating to their
63 * actions.
64 *
65 * @since 2.0.0
66 *
67 * @param int $action_id The scheduled actions ID in the data store
68 * @param Exception $e The exception thrown when attempting to delete the action from the data store
69 * @param int $lifespan The retention period, in seconds, for old actions
70 * @param int $count_of_actions_to_delete The number of old actions being deleted in this batch
71 */
72 do_action( 'action_scheduler_failed_old_action_deletion', $action_id, $e, $lifespan, count( $actions_to_delete ) );
73 }
74 }
75 }
76 }
77
78 /**
79 * Unclaim pending actions that have not been run within a given time limit.
80 *
81 * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed
82 * as a parameter is 10x the time limit used for queue processing.
83 *
84 * @param int $time_limit The number of seconds to allow a queue to run before unclaiming its pending actions. Default 300 (5 minutes).
85 */
86 public function reset_timeouts( $time_limit = 300 ) {
87 $timeout = apply_filters( 'action_scheduler_timeout_period', $time_limit );
88 if ( $timeout < 0 ) {
89 return;
90 }
91 $cutoff = as_get_datetime_object($timeout.' seconds ago');
92 $actions_to_reset = $this->store->query_actions( array(
93 'status' => ActionScheduler_Store::STATUS_PENDING,
94 'modified' => $cutoff,
95 'modified_compare' => '<=',
96 'claimed' => true,
97 'per_page' => $this->get_batch_size(),
98 'orderby' => 'none',
99 ) );
100
101 foreach ( $actions_to_reset as $action_id ) {
102 $this->store->unclaim_action( $action_id );
103 do_action( 'action_scheduler_reset_action', $action_id );
104 }
105 }
106
107 /**
108 * Mark actions that have been running for more than a given time limit as failed, based on
109 * the assumption some uncatachable and unloggable fatal error occurred during processing.
110 *
111 * When called by ActionScheduler_Abstract_QueueRunner::run_cleanup(), the time limit passed
112 * as a parameter is 10x the time limit used for queue processing.
113 *
114 * @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).
115 */
116 public function mark_failures( $time_limit = 300 ) {
117 $timeout = apply_filters( 'action_scheduler_failure_period', $time_limit );
118 if ( $timeout < 0 ) {
119 return;
120 }
121 $cutoff = as_get_datetime_object($timeout.' seconds ago');
122 $actions_to_reset = $this->store->query_actions( array(
123 'status' => ActionScheduler_Store::STATUS_RUNNING,
124 'modified' => $cutoff,
125 'modified_compare' => '<=',
126 'per_page' => $this->get_batch_size(),
127 'orderby' => 'none',
128 ) );
129
130 foreach ( $actions_to_reset as $action_id ) {
131 $this->store->mark_failure( $action_id );
132 do_action( 'action_scheduler_failed_action', $action_id, $timeout );
133 }
134 }
135
136 /**
137 * Do all of the cleaning actions.
138 *
139 * @param int $time_limit The number of seconds to use as the timeout and failure period. Default 300 (5 minutes).
140 * @author Jeremy Pry
141 */
142 public function clean( $time_limit = 300 ) {
143 $this->delete_old_actions();
144 $this->reset_timeouts( $time_limit );
145 $this->mark_failures( $time_limit );
146 }
147
148 /**
149 * Get the batch size for cleaning the queue.
150 *
151 * @author Jeremy Pry
152 * @return int
153 */
154 protected function get_batch_size() {
155 /**
156 * Filter the batch size when cleaning the queue.
157 *
158 * @param int $batch_size The number of actions to clean in one batch.
159 */
160 return absint( apply_filters( 'action_scheduler_cleanup_batch_size', $this->batch_size ) );
161 }
162 }
163