WP_CLI
6 years ago
abstracts
4 years ago
actions
4 years ago
data-stores
4 years ago
migration
4 years ago
schedules
6 years ago
schema
4 years ago
ActionScheduler_ActionClaim.php
6 years ago
ActionScheduler_ActionFactory.php
6 years ago
ActionScheduler_AdminView.php
6 years ago
ActionScheduler_AsyncRequest_QueueRunner.php
6 years ago
ActionScheduler_Compatibility.php
6 years ago
ActionScheduler_DataController.php
6 years ago
ActionScheduler_DateTime.php
6 years ago
ActionScheduler_Exception.php
6 years ago
ActionScheduler_FatalErrorMonitor.php
6 years ago
ActionScheduler_InvalidActionException.php
6 years ago
ActionScheduler_ListTable.php
6 years ago
ActionScheduler_LogEntry.php
6 years ago
ActionScheduler_NullLogEntry.php
6 years ago
ActionScheduler_OptionLock.php
6 years ago
ActionScheduler_QueueCleaner.php
4 years ago
ActionScheduler_QueueRunner.php
6 years ago
ActionScheduler_Versions.php
6 years ago
ActionScheduler_WPCommentCleaner.php
6 years ago
ActionScheduler_wcSystemStatus.php
4 years ago
ActionScheduler_AsyncRequest_QueueRunner.php
98 lines
| 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 |