PluginProbe
WANotifier for Forms and Actions / 1.0.3
WANotifier for Forms and Actions v1.0.3
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_QueueRunner.php

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

217 lines 8.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_QueueRunner
5 */
6 class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
7 const WP_CRON_HOOK = 'action_scheduler_run_queue';
8
9 const WP_CRON_SCHEDULE = 'every_minute';
10
11 /** @var ActionScheduler_AsyncRequest_QueueRunner */
12 protected $async_request;
13
14 /** @var ActionScheduler_QueueRunner */
15 private static $runner = null;
16
17 /**
18 * @return ActionScheduler_QueueRunner
19 * @codeCoverageIgnore
20 */
21 public static function instance() {
22 if ( empty(self::$runner) ) {
23 $class = apply_filters('action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner');
24 self::$runner = new $class();
25 }
26 return self::$runner;
27 }
28
29 /**
30 * ActionScheduler_QueueRunner constructor.
31 *
32 * @param ActionScheduler_Store $store
33 * @param ActionScheduler_FatalErrorMonitor $monitor
34 * @param ActionScheduler_QueueCleaner $cleaner
35 */
36 public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null, ActionScheduler_AsyncRequest_QueueRunner $async_request = null ) {
37 parent::__construct( $store, $monitor, $cleaner );
38
39 if ( is_null( $async_request ) ) {
40 $async_request = new ActionScheduler_AsyncRequest_QueueRunner( $this->store );
41 }
42
43 $this->async_request = $async_request;
44 }
45
46 /**
47 * @codeCoverageIgnore
48 */
49 public function init() {
50
51 add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) );
52
53 // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param
54 $next_timestamp = wp_next_scheduled( self::WP_CRON_HOOK );
55 if ( $next_timestamp ) {
56 wp_unschedule_event( $next_timestamp, self::WP_CRON_HOOK );
57 }
58
59 $cron_context = array( 'WP Cron' );
60
61 if ( ! wp_next_scheduled( self::WP_CRON_HOOK, $cron_context ) ) {
62 $schedule = apply_filters( 'action_scheduler_run_schedule', self::WP_CRON_SCHEDULE );
63 wp_schedule_event( time(), $schedule, self::WP_CRON_HOOK, $cron_context );
64 }
65
66 add_action( self::WP_CRON_HOOK, array( self::instance(), 'run' ) );
67 $this->hook_dispatch_async_request();
68 }
69
70 /**
71 * Hook check for dispatching an async request.
72 */
73 public function hook_dispatch_async_request() {
74 add_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) );
75 }
76
77 /**
78 * Unhook check for dispatching an async request.
79 */
80 public function unhook_dispatch_async_request() {
81 remove_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) );
82 }
83
84 /**
85 * Check if we should dispatch an async request to process actions.
86 *
87 * This method is attached to 'shutdown', so is called frequently. To avoid slowing down
88 * the site, it mitigates the work performed in each request by:
89 * 1. checking if it's in the admin context and then
90 * 2. haven't run on the 'shutdown' hook within the lock time (60 seconds by default)
91 * 3. haven't exceeded the number of allowed batches.
92 *
93 * The order of these checks is important, because they run from a check on a value:
94 * 1. in memory - is_admin() maps to $GLOBALS or the WP_ADMIN constant
95 * 2. in memory - transients use autoloaded options by default
96 * 3. from a database query - has_maximum_concurrent_batches() run the query
97 * $this->store->get_claim_count() to find the current number of claims in the DB.
98 *
99 * If all of these conditions are met, then we request an async runner check whether it
100 * should dispatch a request to process pending actions.
101 */
102 public function maybe_dispatch_async_request() {
103 if ( is_admin() && ! ActionScheduler::lock()->is_locked( 'async-request-runner' ) ) {
104 // Only start an async queue at most once every 60 seconds
105 ActionScheduler::lock()->set( 'async-request-runner' );
106 $this->async_request->maybe_dispatch();
107 }
108 }
109
110 /**
111 * Process actions in the queue. Attached to self::WP_CRON_HOOK i.e. 'action_scheduler_run_queue'
112 *
113 * The $context param of this method defaults to 'WP Cron', because prior to Action Scheduler 3.0.0
114 * that was the only context in which this method was run, and the self::WP_CRON_HOOK hook had no context
115 * passed along with it. New code calling this method directly, or by triggering the self::WP_CRON_HOOK,
116 * should set a context as the first parameter. For an example of this, refer to the code seen in
117 * @see ActionScheduler_AsyncRequest_QueueRunner::handle()
118 *
119 * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron'
120 * Generally, this should be capitalised and not localised as it's a proper noun.
121 * @return int The number of actions processed.
122 */
123 public function run( $context = 'WP Cron' ) {
124 ActionScheduler_Compatibility::raise_memory_limit();
125 ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() );
126 do_action( 'action_scheduler_before_process_queue' );
127 $this->run_cleanup();
128 $processed_actions = 0;
129 if ( false === $this->has_maximum_concurrent_batches() ) {
130 $batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 );
131 do {
132 $processed_actions_in_batch = $this->do_batch( $batch_size, $context );
133 $processed_actions += $processed_actions_in_batch;
134 } while ( $processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded( $processed_actions ) ); // keep going until we run out of actions, time, or memory
135 }
136
137 do_action( 'action_scheduler_after_process_queue' );
138 return $processed_actions;
139 }
140
141 /**
142 * Process a batch of actions pending in the queue.
143 *
144 * Actions are processed by claiming a set of pending actions then processing each one until either the batch
145 * size is completed, or memory or time limits are reached, defined by @see $this->batch_limits_exceeded().
146 *
147 * @param int $size The maximum number of actions to process in the batch.
148 * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron'
149 * Generally, this should be capitalised and not localised as it's a proper noun.
150 * @return int The number of actions processed.
151 */
152 protected function do_batch( $size = 100, $context = '' ) {
153 $claim = $this->store->stake_claim($size);
154 $this->monitor->attach($claim);
155 $processed_actions = 0;
156
157 foreach ( $claim->get_actions() as $action_id ) {
158 // bail if we lost the claim
159 if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) {
160 break;
161 }
162 $this->process_action( $action_id, $context );
163 $processed_actions++;
164
165 if ( $this->batch_limits_exceeded( $processed_actions ) ) {
166 break;
167 }
168 }
169 $this->store->release_claim($claim);
170 $this->monitor->detach();
171 $this->clear_caches();
172 return $processed_actions;
173 }
174
175 /**
176 * Flush the cache if possible (intended for use after a batch of actions has been processed).
177 *
178 * This is useful because running large batches can eat up memory and because invalid data can accrue in the
179 * runtime cache, which may lead to unexpected results.
180 */
181 protected function clear_caches() {
182 /*
183 * Calling wp_cache_flush_runtime() lets us clear the runtime cache without invalidating the external object
184 * cache, so we will always prefer this when it is available (but it was only introduced in WordPress 6.0).
185 */
186 if ( function_exists( 'wp_cache_flush_runtime' ) ) {
187 wp_cache_flush_runtime();
188 } elseif (
189 ! wp_using_ext_object_cache()
190 /**
191 * When an external object cache is in use, and when wp_cache_flush_runtime() is not available, then
192 * normally the cache will not be flushed after processing a batch of actions (to avoid a performance
193 * penalty for other processes).
194 *
195 * This filter makes it possible to override this behavior and always flush the cache, even if an external
196 * object cache is in use.
197 *
198 * @since 1.0
199 *
200 * @param bool $flush_cache If the cache should be flushed.
201 */
202 || apply_filters( 'action_scheduler_queue_runner_flush_cache', false )
203 ) {
204 wp_cache_flush();
205 }
206 }
207
208 public function add_wp_cron_schedule( $schedules ) {
209 $schedules['every_minute'] = array(
210 'interval' => 60, // in seconds
211 'display' => __( 'Every minute', 'action-scheduler' ),
212 );
213
214 return $schedules;
215 }
216 }
217