WP_CLI
1 year ago
abstracts
1 year ago
actions
1 year ago
data-stores
1 year ago
migration
1 year ago
schedules
1 year ago
schema
1 year ago
ActionScheduler_ActionClaim.php
1 year ago
ActionScheduler_ActionFactory.php
1 year ago
ActionScheduler_AdminView.php
1 year ago
ActionScheduler_AsyncRequest_QueueRunner.php
1 year ago
ActionScheduler_Compatibility.php
1 year ago
ActionScheduler_DataController.php
1 year ago
ActionScheduler_DateTime.php
1 year ago
ActionScheduler_Exception.php
4 years ago
ActionScheduler_FatalErrorMonitor.php
1 year ago
ActionScheduler_InvalidActionException.php
1 year ago
ActionScheduler_ListTable.php
1 year ago
ActionScheduler_LogEntry.php
1 year ago
ActionScheduler_NullLogEntry.php
1 year ago
ActionScheduler_OptionLock.php
2 years ago
ActionScheduler_QueueCleaner.php
1 year ago
ActionScheduler_QueueRunner.php
1 year ago
ActionScheduler_SystemInformation.php
1 year ago
ActionScheduler_Versions.php
1 year ago
ActionScheduler_WPCommentCleaner.php
1 year ago
ActionScheduler_wcSystemStatus.php
4 years ago
index.php
3 years ago
ActionScheduler_QueueRunner.php
110 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner { |
| 4 | const WP_CRON_HOOK = 'action_scheduler_run_queue'; |
| 5 | const WP_CRON_SCHEDULE = 'every_minute'; |
| 6 | protected $async_request; |
| 7 | private static $runner = null; |
| 8 | private $processed_actions_count = 0; |
| 9 | public static function instance() { |
| 10 | if ( empty( self::$runner ) ) { |
| 11 | $class = apply_filters( 'action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner' ); |
| 12 | self::$runner = new $class(); |
| 13 | } |
| 14 | return self::$runner; |
| 15 | } |
| 16 | public function __construct( ?ActionScheduler_Store $store = null, ?ActionScheduler_FatalErrorMonitor $monitor = null, ?ActionScheduler_QueueCleaner $cleaner = null, ?ActionScheduler_AsyncRequest_QueueRunner $async_request = null ) { |
| 17 | parent::__construct( $store, $monitor, $cleaner ); |
| 18 | if ( is_null( $async_request ) ) { |
| 19 | $async_request = new ActionScheduler_AsyncRequest_QueueRunner( $this->store ); |
| 20 | } |
| 21 | $this->async_request = $async_request; |
| 22 | } |
| 23 | public function init() { |
| 24 | add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) ); // phpcs:ignore WordPress.WP.CronInterval.CronSchedulesInterval |
| 25 | // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param. |
| 26 | $next_timestamp = wp_next_scheduled( self::WP_CRON_HOOK ); |
| 27 | if ( $next_timestamp ) { |
| 28 | wp_unschedule_event( $next_timestamp, self::WP_CRON_HOOK ); |
| 29 | } |
| 30 | $cron_context = array( 'WP Cron' ); |
| 31 | if ( ! wp_next_scheduled( self::WP_CRON_HOOK, $cron_context ) ) { |
| 32 | $schedule = apply_filters( 'action_scheduler_run_schedule', self::WP_CRON_SCHEDULE ); |
| 33 | wp_schedule_event( time(), $schedule, self::WP_CRON_HOOK, $cron_context ); |
| 34 | } |
| 35 | add_action( self::WP_CRON_HOOK, array( self::instance(), 'run' ) ); |
| 36 | $this->hook_dispatch_async_request(); |
| 37 | } |
| 38 | public function hook_dispatch_async_request() { |
| 39 | add_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
| 40 | } |
| 41 | public function unhook_dispatch_async_request() { |
| 42 | remove_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) ); |
| 43 | } |
| 44 | public function maybe_dispatch_async_request() { |
| 45 | // Only start an async queue at most once every 60 seconds. |
| 46 | if ( |
| 47 | is_admin() |
| 48 | && ! ActionScheduler::lock()->is_locked( 'async-request-runner' ) |
| 49 | && ActionScheduler::lock()->set( 'async-request-runner' ) |
| 50 | ) { |
| 51 | $this->async_request->maybe_dispatch(); |
| 52 | } |
| 53 | } |
| 54 | public function run( $context = 'WP Cron' ) { |
| 55 | ActionScheduler_Compatibility::raise_memory_limit(); |
| 56 | ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() ); |
| 57 | do_action( 'action_scheduler_before_process_queue' ); |
| 58 | $this->run_cleanup(); |
| 59 | $this->processed_actions_count = 0; |
| 60 | if ( false === $this->has_maximum_concurrent_batches() ) { |
| 61 | do { |
| 62 | $batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 ); |
| 63 | $processed_actions_in_batch = $this->do_batch( $batch_size, $context ); |
| 64 | $this->processed_actions_count += $processed_actions_in_batch; |
| 65 | } 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. |
| 66 | } |
| 67 | do_action( 'action_scheduler_after_process_queue' ); |
| 68 | return $this->processed_actions_count; |
| 69 | } |
| 70 | protected function do_batch( $size = 100, $context = '' ) { |
| 71 | $claim = $this->store->stake_claim( $size ); |
| 72 | $this->monitor->attach( $claim ); |
| 73 | $processed_actions = 0; |
| 74 | foreach ( $claim->get_actions() as $action_id ) { |
| 75 | // bail if we lost the claim. |
| 76 | if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ), true ) ) { |
| 77 | break; |
| 78 | } |
| 79 | $this->process_action( $action_id, $context ); |
| 80 | $processed_actions++; |
| 81 | if ( $this->batch_limits_exceeded( $processed_actions + $this->processed_actions_count ) ) { |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | $this->store->release_claim( $claim ); |
| 86 | $this->monitor->detach(); |
| 87 | $this->clear_caches(); |
| 88 | return $processed_actions; |
| 89 | } |
| 90 | protected function clear_caches() { |
| 91 | $flushing_runtime_cache_explicitly_supported = function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_runtime' ); |
| 92 | $flushing_runtime_cache_implicitly_supported = ! function_exists( 'wp_cache_supports' ) && function_exists( 'wp_cache_flush_runtime' ); |
| 93 | if ( $flushing_runtime_cache_explicitly_supported || $flushing_runtime_cache_implicitly_supported ) { |
| 94 | wp_cache_flush_runtime(); |
| 95 | } elseif ( |
| 96 | ! wp_using_ext_object_cache() |
| 97 | || apply_filters( 'action_scheduler_queue_runner_flush_cache', false ) |
| 98 | ) { |
| 99 | wp_cache_flush(); |
| 100 | } |
| 101 | } |
| 102 | public function add_wp_cron_schedule( $schedules ) { |
| 103 | $schedules['every_minute'] = array( |
| 104 | 'interval' => 60, // in seconds. |
| 105 | 'display' => __( 'Every minute', 'action-scheduler' ), |
| 106 | ); |
| 107 | return $schedules; |
| 108 | } |
| 109 | } |
| 110 |