Nessuna descrizione

ActionScheduler_QueueRunner.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Class ActionScheduler_QueueRunner
  4. */
  5. class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
  6. const WP_CRON_HOOK = 'action_scheduler_run_queue';
  7. const WP_CRON_SCHEDULE = 'every_minute';
  8. /** @var ActionScheduler_AsyncRequest_QueueRunner */
  9. protected $async_request;
  10. /** @var ActionScheduler_QueueRunner */
  11. private static $runner = null;
  12. /**
  13. * @return ActionScheduler_QueueRunner
  14. * @codeCoverageIgnore
  15. */
  16. public static function instance() {
  17. if ( empty(self::$runner) ) {
  18. $class = apply_filters('action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner');
  19. self::$runner = new $class();
  20. }
  21. return self::$runner;
  22. }
  23. /**
  24. * ActionScheduler_QueueRunner constructor.
  25. *
  26. * @param ActionScheduler_Store $store
  27. * @param ActionScheduler_FatalErrorMonitor $monitor
  28. * @param ActionScheduler_QueueCleaner $cleaner
  29. */
  30. public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null, ActionScheduler_AsyncRequest_QueueRunner $async_request = null ) {
  31. parent::__construct( $store, $monitor, $cleaner );
  32. if ( is_null( $async_request ) ) {
  33. $async_request = new ActionScheduler_AsyncRequest_QueueRunner( $this->store );
  34. }
  35. $this->async_request = $async_request;
  36. }
  37. /**
  38. * @codeCoverageIgnore
  39. */
  40. public function init() {
  41. add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) );
  42. // Check for and remove any WP Cron hook scheduled by Action Scheduler < 3.0.0, which didn't include the $context param
  43. $next_timestamp = wp_next_scheduled( self::WP_CRON_HOOK );
  44. if ( $next_timestamp ) {
  45. wp_unschedule_event( $next_timestamp, self::WP_CRON_HOOK );
  46. }
  47. $cron_context = array( 'WP Cron' );
  48. if ( ! wp_next_scheduled( self::WP_CRON_HOOK, $cron_context ) ) {
  49. $schedule = apply_filters( 'action_scheduler_run_schedule', self::WP_CRON_SCHEDULE );
  50. wp_schedule_event( time(), $schedule, self::WP_CRON_HOOK, $cron_context );
  51. }
  52. add_action( self::WP_CRON_HOOK, array( self::instance(), 'run' ) );
  53. $this->hook_dispatch_async_request();
  54. }
  55. /**
  56. * Hook check for dispatching an async request.
  57. */
  58. public function hook_dispatch_async_request() {
  59. add_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) );
  60. }
  61. /**
  62. * Unhook check for dispatching an async request.
  63. */
  64. public function unhook_dispatch_async_request() {
  65. remove_action( 'shutdown', array( $this, 'maybe_dispatch_async_request' ) );
  66. }
  67. /**
  68. * Check if we should dispatch an async request to process actions.
  69. *
  70. * This method is attached to 'shutdown', so is called frequently. To avoid slowing down
  71. * the site, it mitigates the work performed in each request by:
  72. * 1. checking if it's in the admin context and then
  73. * 2. haven't run on the 'shutdown' hook within the lock time (60 seconds by default)
  74. * 3. haven't exceeded the number of allowed batches.
  75. *
  76. * The order of these checks is important, because they run from a check on a value:
  77. * 1. in memory - is_admin() maps to $GLOBALS or the WP_ADMIN constant
  78. * 2. in memory - transients use autoloaded options by default
  79. * 3. from a database query - has_maximum_concurrent_batches() run the query
  80. * $this->store->get_claim_count() to find the current number of claims in the DB.
  81. *
  82. * If all of these conditions are met, then we request an async runner check whether it
  83. * should dispatch a request to process pending actions.
  84. */
  85. public function maybe_dispatch_async_request() {
  86. if ( is_admin() && ! ActionScheduler::lock()->is_locked( 'async-request-runner' ) ) {
  87. // Only start an async queue at most once every 60 seconds
  88. ActionScheduler::lock()->set( 'async-request-runner' );
  89. $this->async_request->maybe_dispatch();
  90. }
  91. }
  92. /**
  93. * Process actions in the queue. Attached to self::WP_CRON_HOOK i.e. 'action_scheduler_run_queue'
  94. *
  95. * The $context param of this method defaults to 'WP Cron', because prior to Action Scheduler 3.0.0
  96. * that was the only context in which this method was run, and the self::WP_CRON_HOOK hook had no context
  97. * passed along with it. New code calling this method directly, or by triggering the self::WP_CRON_HOOK,
  98. * should set a context as the first parameter. For an example of this, refer to the code seen in
  99. * @see ActionScheduler_AsyncRequest_QueueRunner::handle()
  100. *
  101. * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron'
  102. * Generally, this should be capitalised and not localised as it's a proper noun.
  103. * @return int The number of actions processed.
  104. */
  105. public function run( $context = 'WP Cron' ) {
  106. ActionScheduler_Compatibility::raise_memory_limit();
  107. ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() );
  108. do_action( 'action_scheduler_before_process_queue' );
  109. $this->run_cleanup();
  110. $processed_actions = 0;
  111. if ( false === $this->has_maximum_concurrent_batches() ) {
  112. $batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 );
  113. do {
  114. $processed_actions_in_batch = $this->do_batch( $batch_size, $context );
  115. $processed_actions += $processed_actions_in_batch;
  116. } while ( $processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded( $processed_actions ) ); // keep going until we run out of actions, time, or memory
  117. }
  118. do_action( 'action_scheduler_after_process_queue' );
  119. return $processed_actions;
  120. }
  121. /**
  122. * Process a batch of actions pending in the queue.
  123. *
  124. * Actions are processed by claiming a set of pending actions then processing each one until either the batch
  125. * size is completed, or memory or time limits are reached, defined by @see $this->batch_limits_exceeded().
  126. *
  127. * @param int $size The maximum number of actions to process in the batch.
  128. * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron'
  129. * Generally, this should be capitalised and not localised as it's a proper noun.
  130. * @return int The number of actions processed.
  131. */
  132. protected function do_batch( $size = 100, $context = '' ) {
  133. $claim = $this->store->stake_claim($size);
  134. $this->monitor->attach($claim);
  135. $processed_actions = 0;
  136. foreach ( $claim->get_actions() as $action_id ) {
  137. // bail if we lost the claim
  138. if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) {
  139. break;
  140. }
  141. $this->process_action( $action_id, $context );
  142. $processed_actions++;
  143. if ( $this->batch_limits_exceeded( $processed_actions ) ) {
  144. break;
  145. }
  146. }
  147. $this->store->release_claim($claim);
  148. $this->monitor->detach();
  149. $this->clear_caches();
  150. return $processed_actions;
  151. }
  152. /**
  153. * Running large batches can eat up memory, as WP adds data to its object cache.
  154. *
  155. * If using a persistent object store, this has the side effect of flushing that
  156. * as well, so this is disabled by default. To enable:
  157. *
  158. * add_filter( 'action_scheduler_queue_runner_flush_cache', '__return_true' );
  159. */
  160. protected function clear_caches() {
  161. if ( ! wp_using_ext_object_cache() || apply_filters( 'action_scheduler_queue_runner_flush_cache', false ) ) {
  162. wp_cache_flush();
  163. }
  164. }
  165. public function add_wp_cron_schedule( $schedules ) {
  166. $schedules['every_minute'] = array(
  167. 'interval' => 60, // in seconds
  168. 'display' => __( 'Every minute', 'woocommerce' ),
  169. );
  170. return $schedules;
  171. }
  172. }