Passed
Push — master ( 495efb...ace576 )
by Brian
06:14 queued 01:46
created

ActionScheduler_Abstract_QueueRunner   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 234
rs 9.92
wmc 31

12 Methods

Rating   Name   Duplication   Size   Complexity  
A get_time_limit() 0 11 2
A get_memory_limit() 0 13 5
A run_cleanup() 0 2 1
B process_action() 0 29 7
A time_likely_to_be_exceeded() 0 9 1
A get_allowed_concurrent_batches() 0 2 1
A has_maximum_concurrent_batches() 0 2 1
A schedule_next_instance() 0 5 2
A get_execution_time() 0 13 4
A batch_limits_exceeded() 0 2 2
A __construct() 0 7 4
A memory_exceeded() 0 7 1
1
<?php
2
3
/**
4
 * Abstract class with common Queue Cleaner functionality.
5
 */
6
abstract class ActionScheduler_Abstract_QueueRunner extends ActionScheduler_Abstract_QueueRunner_Deprecated {
7
8
	/** @var ActionScheduler_QueueCleaner */
9
	protected $cleaner;
10
11
	/** @var ActionScheduler_FatalErrorMonitor */
12
	protected $monitor;
13
14
	/** @var ActionScheduler_Store */
15
	protected $store;
16
17
	/**
18
	 * The created time.
19
	 *
20
	 * Represents when the queue runner was constructed and used when calculating how long a PHP request has been running.
21
	 * For this reason it should be as close as possible to the PHP request start time.
22
	 *
23
	 * @var int
24
	 */
25
	private $created_time;
26
27
	/**
28
	 * ActionScheduler_Abstract_QueueRunner constructor.
29
	 *
30
	 * @param ActionScheduler_Store             $store
31
	 * @param ActionScheduler_FatalErrorMonitor $monitor
32
	 * @param ActionScheduler_QueueCleaner      $cleaner
33
	 */
34
	public function __construct( ActionScheduler_Store $store = null, ActionScheduler_FatalErrorMonitor $monitor = null, ActionScheduler_QueueCleaner $cleaner = null ) {
35
36
		$this->created_time = microtime( true );
37
38
		$this->store   = $store ? $store : ActionScheduler_Store::instance();
39
		$this->monitor = $monitor ? $monitor : new ActionScheduler_FatalErrorMonitor( $this->store );
40
		$this->cleaner = $cleaner ? $cleaner : new ActionScheduler_QueueCleaner( $this->store );
41
	}
42
43
	/**
44
	 * Process an individual action.
45
	 *
46
	 * @param int $action_id The action ID to process.
47
	 * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron'
48
	 *        Generally, this should be capitalised and not localised as it's a proper noun.
49
	 */
50
	public function process_action( $action_id, $context = '' ) {
51
		try {
52
			$valid_action = false;
53
			do_action( 'action_scheduler_before_execute', $action_id, $context );
54
55
			if ( ActionScheduler_Store::STATUS_PENDING !== $this->store->get_status( $action_id ) ) {
56
				do_action( 'action_scheduler_execution_ignored', $action_id, $context );
57
				return;
58
			}
59
60
			$valid_action = true;
61
			do_action( 'action_scheduler_begin_execute', $action_id, $context );
62
63
			$action = $this->store->fetch_action( $action_id );
64
			$this->store->log_execution( $action_id );
65
			$action->execute();
66
			do_action( 'action_scheduler_after_execute', $action_id, $action, $context );
67
			$this->store->mark_complete( $action_id );
68
		} catch ( Exception $e ) {
69
			if ( $valid_action ) {
70
				$this->store->mark_failure( $action_id );
71
				do_action( 'action_scheduler_failed_execution', $action_id, $e, $context );
72
			} else {
73
				do_action( 'action_scheduler_failed_validation', $action_id, $e, $context );
74
			}
75
		}
76
77
		if ( isset( $action ) && is_a( $action, 'ActionScheduler_Action' ) && $action->get_schedule()->is_recurring() ) {
78
			$this->schedule_next_instance( $action, $action_id );
79
		}
80
	}
81
82
	/**
83
	 * Schedule the next instance of the action if necessary.
84
	 *
85
	 * @param ActionScheduler_Action $action
86
	 * @param int $action_id
87
	 */
88
	protected function schedule_next_instance( ActionScheduler_Action $action, $action_id ) {
89
		try {
90
			ActionScheduler::factory()->repeat( $action );
91
		} catch ( Exception $e ) {
92
			do_action( 'action_scheduler_failed_to_schedule_next_instance', $action_id, $e, $action );
93
		}
94
	}
95
96
	/**
97
	 * Run the queue cleaner.
98
	 *
99
	 * @author Jeremy Pry
100
	 */
101
	protected function run_cleanup() {
102
		$this->cleaner->clean( 10 * $this->get_time_limit() );
103
	}
104
105
	/**
106
	 * Get the number of concurrent batches a runner allows.
107
	 *
108
	 * @return int
109
	 */
110
	public function get_allowed_concurrent_batches() {
111
		return apply_filters( 'action_scheduler_queue_runner_concurrent_batches', 1 );
112
	}
113
114
	/**
115
	 * Check if the number of allowed concurrent batches is met or exceeded.
116
	 *
117
	 * @return bool
118
	 */
119
	public function has_maximum_concurrent_batches() {
120
		return $this->store->get_claim_count() >= $this->get_allowed_concurrent_batches();
121
	}
122
123
	/**
124
	 * Get the maximum number of seconds a batch can run for.
125
	 *
126
	 * @return int The number of seconds.
127
	 */
128
	protected function get_time_limit() {
129
130
		$time_limit = 30;
131
132
		// Apply deprecated filter from deprecated get_maximum_execution_time() method
133
		if ( has_filter( 'action_scheduler_maximum_execution_time' ) ) {
134
			_deprecated_function( 'action_scheduler_maximum_execution_time', '2.1.1', 'action_scheduler_queue_runner_time_limit' );
135
			$time_limit = apply_filters( 'action_scheduler_maximum_execution_time', $time_limit );
136
		}
137
138
		return absint( apply_filters( 'action_scheduler_queue_runner_time_limit', $time_limit ) );
139
	}
140
141
	/**
142
	 * Get the number of seconds the process has been running.
143
	 *
144
	 * @return int The number of seconds.
145
	 */
146
	protected function get_execution_time() {
147
		$execution_time = microtime( true ) - $this->created_time;
148
149
		// Get the CPU time if the hosting environment uses it rather than wall-clock time to calculate a process's execution time.
150
		if ( function_exists( 'getrusage' ) && apply_filters( 'action_scheduler_use_cpu_execution_time', defined( 'PANTHEON_ENVIRONMENT' ) ) ) {
151
			$resource_usages = getrusage();
152
153
			if ( isset( $resource_usages['ru_stime.tv_usec'], $resource_usages['ru_stime.tv_usec'] ) ) {
154
				$execution_time = $resource_usages['ru_stime.tv_sec'] + ( $resource_usages['ru_stime.tv_usec'] / 1000000 );
155
			}
156
		}
157
158
		return $execution_time;
159
	}
160
161
	/**
162
	 * Check if the host's max execution time is (likely) to be exceeded if processing more actions.
163
	 *
164
	 * @param int $processed_actions The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action
165
	 * @return bool
166
	 */
167
	protected function time_likely_to_be_exceeded( $processed_actions ) {
168
169
		$execution_time        = $this->get_execution_time();
170
		$max_execution_time    = $this->get_time_limit();
171
		$time_per_action       = $execution_time / $processed_actions;
172
		$estimated_time        = $execution_time + ( $time_per_action * 3 );
173
		$likely_to_be_exceeded = $estimated_time > $max_execution_time;
174
175
		return apply_filters( 'action_scheduler_maximum_execution_time_likely_to_be_exceeded', $likely_to_be_exceeded, $this, $processed_actions, $execution_time, $max_execution_time );
176
	}
177
178
	/**
179
	 * Get memory limit
180
	 *
181
	 * Based on WP_Background_Process::get_memory_limit()
182
	 *
183
	 * @return int
184
	 */
185
	protected function get_memory_limit() {
186
		if ( function_exists( 'ini_get' ) ) {
187
			$memory_limit = ini_get( 'memory_limit' );
188
		} else {
189
			$memory_limit = '128M'; // Sensible default, and minimum required by WooCommerce
190
		}
191
192
		if ( ! $memory_limit || -1 === $memory_limit || '-1' === $memory_limit ) {
193
			// Unlimited, set to 32GB.
194
			$memory_limit = '32G';
195
		}
196
197
		return ActionScheduler_Compatibility::convert_hr_to_bytes( $memory_limit );
198
	}
199
200
	/**
201
	 * Memory exceeded
202
	 *
203
	 * Ensures the batch process never exceeds 90% of the maximum WordPress memory.
204
	 *
205
	 * Based on WP_Background_Process::memory_exceeded()
206
	 *
207
	 * @return bool
208
	 */
209
	protected function memory_exceeded() {
210
211
		$memory_limit    = $this->get_memory_limit() * 0.90;
212
		$current_memory  = memory_get_usage( true );
213
		$memory_exceeded = $current_memory >= $memory_limit;
214
215
		return apply_filters( 'action_scheduler_memory_exceeded', $memory_exceeded, $this );
216
	}
217
218
	/**
219
	 * See if the batch limits have been exceeded, which is when memory usage is almost at
220
	 * the maximum limit, or the time to process more actions will exceed the max time limit.
221
	 *
222
	 * Based on WC_Background_Process::batch_limits_exceeded()
223
	 *
224
	 * @param int $processed_actions The number of actions processed so far - used to determine the likelihood of exceeding the time limit if processing another action
225
	 * @return bool
226
	 */
227
	protected function batch_limits_exceeded( $processed_actions ) {
228
		return $this->memory_exceeded() || $this->time_likely_to_be_exceeded( $processed_actions );
229
	}
230
231
	/**
232
	 * Process actions in the queue.
233
	 *
234
	 * @author Jeremy Pry
235
	 * @param string $context Optional identifer for the context in which this action is being processed, e.g. 'WP CLI' or 'WP Cron'
236
	 *        Generally, this should be capitalised and not localised as it's a proper noun.
237
	 * @return int The number of actions processed.
238
	 */
239
	abstract public function run( $context = '' );
240
}
241