PluginProbe
WANotifier for Forms and Actions / 1.0.0
WANotifier for Forms and Actions v1.0.0
3.1.1 3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 All 67 releases
notifier / libraries / action-scheduler / classes / ActionScheduler_QueueCleaner.php

ActionScheduler_QueueCleaner.php in WANotifier for Forms and Actions 1.0.0, at libraries/action-scheduler/classes/ActionScheduler_QueueCleaner.php

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