Skip to content

Commit 61b79e3

Browse files
committed
Deprecate max execution time
And instead, use a consistent (but filterable) 30 seconds as both the maximum execution time, and the runner time. Reducing the time limit to 30 seconds, from an unknown amount in many cases, helps reduce the likelyhood of timeouts affecting batches. This is particularly important now that Action Scheduler will loop over batches in the available time and memory limits (with the merge of woocommerce#192) instead of simply attempting one batch and then completing, meaning any time there are more than the default batch size of actions sitting in the queue, actions will be processed until we run out of actions, time or memory.. Unifying the time limit API also fixes a few issues: 1. set_time_limit() was being called with a value that was different to the return value of get_maximum_execution_time() in many cases. This was at best ineffecient, and at worst dangerous as we could perceive of having two different time limits. 2. As one consequence of the above, ini_get( 'max_execution_time' ) - the default value when not on a known host - would presumably return 600, but that was intended to more of an optimistic increase not a reliably value for the amount of time we could consume. 3. To consistently change the amount of time allowed for a queue, the a callback on the 'action_scheduler_queue_runner_time_limit' filter was no longer enough (it would have been prior to 2.0.0), instead both it and 'action_scheduler_maximum_execution_time' needed to have a callback. Fixes woocommerce#207.
1 parent ede6b3c commit 61b79e3

5 files changed

+47
-14
lines changed

classes/ActionScheduler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public static function plugin_url( $path ) {
5959

6060
public static function autoload( $class ) {
6161
$d = DIRECTORY_SEPARATOR;
62-
if ( strpos( $class, 'ActionScheduler' ) === 0 ) {
62+
if ( 'Deprecated' === substr( $class, -10 ) ) {
63+
$dir = self::plugin_path('deprecated'.$d);
64+
} elseif ( strpos( $class, 'ActionScheduler' ) === 0 ) {
6365
$dir = self::plugin_path('classes'.$d);
6466
} elseif ( strpos( $class, 'CronExpression' ) === 0 ) {
6567
$dir = self::plugin_path('lib'.$d.'cron-expression'.$d);

classes/ActionScheduler_Abstract_QueueRunner.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Abstract class with common Queue Cleaner functionality.
55
*/
6-
abstract class ActionScheduler_Abstract_QueueRunner {
6+
abstract class ActionScheduler_Abstract_QueueRunner extends ActionScheduler_Abstract_QueueRunner_Deprecated {
77

88
/** @var ActionScheduler_QueueCleaner */
99
protected $cleaner;
@@ -103,18 +103,17 @@ public function get_allowed_concurrent_batches() {
103103
*
104104
* @return int The number of seconds.
105105
*/
106-
protected function get_maximum_execution_time() {
106+
protected function get_time_limit() {
107107

108-
// There are known hosts with a strict 60 second execution time.
109-
if ( defined( 'WPENGINE_ACCOUNT' ) || defined( 'PANTHEON_ENVIRONMENT' ) ) {
110-
$maximum_execution_time = 60;
111-
} elseif ( false !== strpos( getenv( 'HOSTNAME' ), '.siteground.' ) ) {
112-
$maximum_execution_time = 120;
113-
} else {
114-
$maximum_execution_time = ini_get( 'max_execution_time' );
108+
$time_limit = 30;
109+
110+
// Apply deprecated filter from deprecated get_maximum_execution_time() method
111+
if ( has_filter( 'action_scheduler_maximum_execution_time' ) ) {
112+
_deprecated_function( 'action_scheduler_maximum_execution_time', '2.1.1', 'action_scheduler_queue_runner_time_limit' );
113+
$time_limit = apply_filters( 'action_scheduler_maximum_execution_time', $time_limit );
115114
}
116115

117-
return absint( apply_filters( 'action_scheduler_maximum_execution_time', $maximum_execution_time ) );
116+
return absint( apply_filters( 'action_scheduler_queue_runner_time_limit', $time_limit ) );
118117
}
119118

120119
/**
@@ -146,7 +145,7 @@ protected function get_execution_time() {
146145
protected function time_likely_to_be_exceeded( $processed_actions ) {
147146

148147
$execution_time = $this->get_execution_time();
149-
$max_execution_time = $this->get_maximum_execution_time();
148+
$max_execution_time = $this->get_time_limit();
150149
$time_per_action = $execution_time / $processed_actions;
151150
$estimated_time = $execution_time + ( $time_per_action * 3 );
152151
$likely_to_be_exceeded = $estimated_time > $max_execution_time;

classes/ActionScheduler_Compatibility.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ public static function raise_memory_limit() {
8585
*
8686
* @param int The time limit in seconds.
8787
*/
88-
public static function increase_time_limit( $limit = 0 ) {
88+
public static function raise_time_limit( $limit = 0 ) {
89+
90+
if ( $limit < ini_get( 'max_execution_time' ) ) {
91+
return;
92+
}
93+
8994
if ( function_exists( 'wc_set_time_limit' ) ) {
9095
return wc_set_time_limit( $limit );
9196
} elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {

classes/ActionScheduler_QueueRunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function init() {
5151

5252
public function run() {
5353
ActionScheduler_Compatibility::raise_memory_limit();
54-
@set_time_limit( apply_filters( 'action_scheduler_queue_runner_time_limit', 600 ) );
54+
ActionScheduler_Compatibility::raise_time_limit( $this->get_time_limit() );
5555
do_action( 'action_scheduler_before_process_queue' );
5656
$this->run_cleanup();
5757
$processed_actions = 0;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* Abstract class with common Queue Cleaner functionality.
5+
*/
6+
abstract class ActionScheduler_Abstract_QueueRunner_Deprecated {
7+
8+
/**
9+
* Get the maximum number of seconds a batch can run for.
10+
*
11+
* @deprecated 2.1.1
12+
* @return int The number of seconds.
13+
*/
14+
protected function get_maximum_execution_time() {
15+
_deprecated_function( __METHOD__, '2.1.1', 'ActionScheduler_Abstract_QueueRunner::get_time_limit()' );
16+
17+
$maximum_execution_time = 30;
18+
19+
// Apply deprecated filter
20+
if ( has_filter( 'action_scheduler_maximum_execution_time' ) ) {
21+
_deprecated_function( 'action_scheduler_maximum_execution_time', '2.1.1', 'action_scheduler_queue_runner_time_limit' );
22+
$maximum_execution_time = apply_filters( 'action_scheduler_maximum_execution_time', $maximum_execution_time );
23+
}
24+
25+
return absint( $maximum_execution_time );
26+
}
27+
}

0 commit comments

Comments
 (0)