This commit is contained in:
Prospress Inc
2018-12-12 14:48:11 +01:00
committed by Remco Tolsma
parent 37367ac369
commit 721dda6e5c
340 changed files with 5114 additions and 2125 deletions

View File

@@ -11,16 +11,6 @@ class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
/** @var ActionScheduler_QueueRunner */
private static $runner = null;
/**
* The created time.
*
* Represents when the queue runner was constructed and used when calculating how long a PHP request has been running.
* For this reason it should be as close as possible to the PHP request start time.
*
* @var int
*/
private $created_time;
/**
* @return ActionScheduler_QueueRunner
* @codeCoverageIgnore
@@ -42,7 +32,6 @@ class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
*/
public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) {
parent::__construct( $store, $monitor, $cleaner );
$this->created_time = microtime( true );
}
/**
@@ -61,43 +50,39 @@ class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
}
public function run() {
@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
@set_time_limit( apply_filters( 'action_scheduler_queue_runner_time_limit', 600 ) );
ActionScheduler_Compatibility::raise_memory_limit();
ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() );
do_action( 'action_scheduler_before_process_queue' );
$this->run_cleanup();
$count = 0;
$processed_actions = 0;
if ( $this->store->get_claim_count() < $this->get_allowed_concurrent_batches() ) {
$batch_size = apply_filters( 'action_scheduler_queue_runner_batch_size', 25 );
$count = $this->do_batch( $batch_size );
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
}
do_action( 'action_scheduler_after_process_queue' );
return $count;
return $processed_actions;
}
protected function do_batch( $size = 100 ) {
$claim = $this->store->stake_claim($size);
$this->monitor->attach($claim);
$processed_actions = 0;
$maximum_execution_time = $this->get_maximum_execution_time();
$processed_actions = 0;
foreach ( $claim->get_actions() as $action_id ) {
if ( 0 !== $processed_actions ) {
$time_elapsed = $this->get_execution_time();
$average_processing_time = $time_elapsed / $processed_actions;
// Bail early if the time it has taken to process this batch is approaching the maximum execution time.
if ( $time_elapsed + ( $average_processing_time * 2 ) > $maximum_execution_time ) {
break;
}
}
// 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++;
if ( $this->batch_limits_exceeded( $processed_actions ) ) {
break;
}
}
$this->store->release_claim($claim);
$this->monitor->detach();
@@ -127,43 +112,4 @@ class ActionScheduler_QueueRunner extends ActionScheduler_Abstract_QueueRunner {
return $schedules;
}
/**
* Get the maximum number of seconds a batch can run for.
*
* @return int The number of seconds.
*/
protected function get_maximum_execution_time() {
// There are known hosts with a strict 60 second execution time.
if ( defined( 'WPENGINE_ACCOUNT' ) || defined( 'PANTHEON_ENVIRONMENT' ) ) {
$maximum_execution_time = 60;
} elseif ( false !== strpos( getenv( 'HOSTNAME' ), '.siteground.' ) ) {
$maximum_execution_time = 120;
} else {
$maximum_execution_time = ini_get( 'max_execution_time' );
}
return absint( apply_filters( 'action_scheduler_maximum_execution_time', $maximum_execution_time ) );
}
/**
* Get the number of seconds a batch has run for.
*
* @return int The number of seconds.
*/
protected function get_execution_time() {
$execution_time = microtime( true ) - $this->created_time;
// Get the CPU time if the hosting environment uses it rather than wall-clock time to calculate a process's execution time.
if ( function_exists( 'getrusage' ) && apply_filters( 'action_scheduler_use_cpu_execution_time', defined( 'PANTHEON_ENVIRONMENT' ) ) ) {
$resource_usages = getrusage();
if ( isset( $resource_usages['ru_stime.tv_usec'], $resource_usages['ru_stime.tv_usec'] ) ) {
$execution_time = $resource_usages['ru_stime.tv_sec'] + ( $resource_usages['ru_stime.tv_usec'] / 1000000 );
}
}
return $execution_time;
}
}