PluginProbe
WANotifier for Forms and Actions / 3.1.1
WANotifier for Forms and Actions v3.1.1
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 3.1.1, at libraries/action-scheduler/classes/ActionScheduler_AsyncRequest_QueueRunner.php

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