PluginProbe
WANotifier for Forms and Actions / 2.7.13
WANotifier for Forms and Actions v2.7.13
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_AsyncRequest_QueueRunner.php

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

98 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ActionScheduler_AsyncRequest_QueueRunner
4 */
5
6 defined( 'ABSPATH' ) || exit;
7
8 /**
9 * ActionScheduler_AsyncRequest_QueueRunner class.
10 */
11 class ActionScheduler_AsyncRequest_QueueRunner extends WP_Async_Request {
12
13 /**
14 * Data store for querying actions
15 *
16 * @var ActionScheduler_Store
17 * @access protected
18 */
19 protected $store;
20
21 /**
22 * Prefix for ajax hooks
23 *
24 * @var string
25 * @access protected
26 */
27 protected $prefix = 'as';
28
29 /**
30 * Action for ajax hooks
31 *
32 * @var string
33 * @access protected
34 */
35 protected $action = 'async_request_queue_runner';
36
37 /**
38 * Initiate new async request
39 */
40 public function __construct( ActionScheduler_Store $store ) {
41 parent::__construct();
42 $this->store = $store;
43 }
44
45 /**
46 * Handle async requests
47 *
48 * Run a queue, and maybe dispatch another async request to run another queue
49 * if there are still pending actions after completing a queue in this request.
50 */
51 protected function handle() {
52 do_action( 'action_scheduler_run_queue', 'Async Request' ); // run a queue in the same way as WP Cron, but declare the Async Request context
53
54 $sleep_seconds = $this->get_sleep_seconds();
55
56 if ( $sleep_seconds ) {
57 sleep( $sleep_seconds );
58 }
59
60 $this->maybe_dispatch();
61 }
62
63 /**
64 * If the async request runner is needed and allowed to run, dispatch a request.
65 */
66 public function maybe_dispatch() {
67 if ( ! $this->allow() ) {
68 return;
69 }
70
71 $this->dispatch();
72 ActionScheduler_QueueRunner::instance()->unhook_dispatch_async_request();
73 }
74
75 /**
76 * Only allow async requests when needed.
77 *
78 * Also allow 3rd party code to disable running actions via async requests.
79 */
80 protected function allow() {
81
82 if ( ! has_action( 'action_scheduler_run_queue' ) || ActionScheduler::runner()->has_maximum_concurrent_batches() || ! $this->store->has_pending_actions_due() ) {
83 $allow = false;
84 } else {
85 $allow = true;
86 }
87
88 return apply_filters( 'action_scheduler_allow_async_request_runner', $allow );
89 }
90
91 /**
92 * Chaining async requests can crash MySQL. A brief sleep call in PHP prevents that.
93 */
94 protected function get_sleep_seconds() {
95 return apply_filters( 'action_scheduler_async_request_sleep_seconds', 5, $this );
96 }
97 }
98