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