2018-07-06 05:40:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ActionScheduler_QueueRunner
|
|
|
|
*/
|
|
|
|
class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
|
|
|
|
const WP_CRON_HOOK = 'action_scheduler_run_queue';
|
|
|
|
|
|
|
|
const WP_CRON_SCHEDULE = 'every_minute';
|
|
|
|
|
|
|
|
/** @var ActionScheduler_QueueRunner */
|
|
|
|
private static $runner = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ActionScheduler_QueueRunner
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public static function instance() {
|
|
|
|
if ( empty(self::$runner) ) {
|
|
|
|
$class = apply_filters('action_scheduler_queue_runner_class', 'ActionScheduler_QueueRunner');
|
|
|
|
self::$runner = new $class();
|
|
|
|
}
|
|
|
|
return self::$runner;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActionScheduler_QueueRunner constructor.
|
|
|
|
*
|
|
|
|
* @param ActionScheduler_Store $store
|
|
|
|
* @param ActionScheduler_FatalErrorMonitor $monitor
|
|
|
|
* @param ActionScheduler_QueueCleaner $cleaner
|
|
|
|
*/
|
|
|
|
public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) {
|
|
|
|
parent::__construct( $store, $monitor, $cleaner );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public function init() {
|
|
|
|
|
|
|
|
add_filter( 'cron_schedules', array( self::instance(), 'add_wp_cron_schedule' ) );
|
|
|
|
|
|
|
|
if ( !wp_next_scheduled(self::WP_CRON_HOOK) ) {
|
|
|
|
$schedule = apply_filters( 'action_scheduler_run_schedule', self::WP_CRON_SCHEDULE );
|
|
|
|
wp_schedule_event( time(), $schedule, self::WP_CRON_HOOK );
|
|
|
|
}
|
|
|
|
|
|
|
|
add_action( self::WP_CRON_HOOK, array( self::instance(), 'run' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run() {
|
2018-09-20 02:16:26 +00:00
|
|
|
ActionScheduler_Compatibility::raise_memory_limit();
|
2019-03-13 13:45:39 +00:00
|
|
|
ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() );
|
2018-07-06 05:40:54 +00:00
|
|
|
do_action( 'action_scheduler_before_process_queue' );
|
|
|
|
$this->run_cleanup();
|
2018-09-20 02:16:26 +00:00
|
|
|
$processed_actions = 0;
|
2018-07-06 05:40:54 +00:00
|
|
|
if ( $this->store->get_claim_count() < $this->get_allowed_concurrent_batches() ) {
|
|
|
|
$batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 );
|
2018-09-20 02:16:26 +00:00
|
|
|
do {
|
|
|
|
$processed_actions_in_batch = $this->do_batch( $batch_size );
|
|
|
|
$processed_actions += $processed_actions_in_batch;
|
|
|
|
} while ( $processed_actions_in_batch > 0 && ! $this->batch_limits_exceeded( $processed_actions ) ); // keep going until we run out of actions, time, or memory
|
2018-07-06 05:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do_action( 'action_scheduler_after_process_queue' );
|
2018-09-20 02:16:26 +00:00
|
|
|
return $processed_actions;
|
2018-07-06 05:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function do_batch( $size = 100 ) {
|
|
|
|
$claim = $this->store->stake_claim($size);
|
|
|
|
$this->monitor->attach($claim);
|
2018-09-20 02:16:26 +00:00
|
|
|
$processed_actions = 0;
|
2018-07-06 05:40:54 +00:00
|
|
|
|
|
|
|
foreach ( $claim->get_actions() as $action_id ) {
|
|
|
|
// bail if we lost the claim
|
|
|
|
if ( ! in_array( $action_id, $this->store->find_actions_by_claim_id( $claim->get_id() ) ) ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$this->process_action( $action_id );
|
|
|
|
$processed_actions++;
|
2018-09-20 02:16:26 +00:00
|
|
|
|
|
|
|
if ( $this->batch_limits_exceeded( $processed_actions ) ) {
|
|
|
|
break;
|
|
|
|
}
|
2018-07-06 05:40:54 +00:00
|
|
|
}
|
|
|
|
$this->store->release_claim($claim);
|
|
|
|
$this->monitor->detach();
|
|
|
|
$this->clear_caches();
|
|
|
|
return $processed_actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Running large batches can eat up memory, as WP adds data to its object cache.
|
|
|
|
*
|
|
|
|
* If using a persistent object store, this has the side effect of flushing that
|
|
|
|
* as well, so this is disabled by default. To enable:
|
|
|
|
*
|
|
|
|
* add_filter( 'action_scheduler_queue_runner_flush_cache', '__return_true' );
|
|
|
|
*/
|
|
|
|
protected function clear_caches() {
|
|
|
|
if ( ! wp_using_ext_object_cache() || apply_filters( 'action_scheduler_queue_runner_flush_cache', false ) ) {
|
|
|
|
wp_cache_flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add_wp_cron_schedule( $schedules ) {
|
|
|
|
$schedules['every_minute'] = array(
|
|
|
|
'interval' => 60, // in seconds
|
2019-12-02 21:38:21 +00:00
|
|
|
'display' => __( 'Every minute', 'woocommerce' ),
|
2018-07-06 05:40:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $schedules;
|
|
|
|
}
|
|
|
|
}
|