1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WP_Queue; |
4
|
|
|
|
5
|
|
|
class Cron { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
protected $id; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var Worker |
14
|
|
|
*/ |
15
|
|
|
protected $worker; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $interval; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Timestamp of when processing the queue started. |
24
|
|
|
* |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
protected $start_time; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cron constructor. |
31
|
|
|
* |
32
|
|
|
* @param string $id |
33
|
|
|
* @param Worker $worker |
34
|
|
|
* @param int $interval |
35
|
|
|
*/ |
36
|
1 |
|
public function __construct( $id, $worker, $interval ) { |
37
|
1 |
|
$this->id = $id; |
38
|
1 |
|
$this->worker = $worker; |
39
|
1 |
|
$this->interval = $interval; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Is the cron queue worker enabled? |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
1 |
|
protected function is_enabled() { |
48
|
1 |
|
if ( defined( 'DISABLE_WP_QUEUE_CRON' ) && DISABLE_WP_QUEUE_CRON ) { |
|
|
|
|
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Init cron class. |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
1 |
|
public function init() { |
61
|
1 |
|
if ( ! $this->is_enabled() ) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
add_filter( 'cron_schedules', array( $this, 'schedule_cron' ) ); |
66
|
1 |
|
add_action( "wp_queue_worker_{$this->id}", array( $this, 'cron_worker' ) ); |
67
|
|
|
|
68
|
1 |
|
if ( ! wp_next_scheduled( "wp_queue_worker_{$this->id}" ) ) { |
|
|
|
|
69
|
|
|
// Schedule health check |
70
|
|
|
wp_schedule_event( time(), 'wp_queue_cron_interval', "wp_queue_worker_{$this->id}" ); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Add interval to cron schedules. |
78
|
|
|
* |
79
|
|
|
* @param array $schedules |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function schedule_cron( $schedules ) { |
84
|
|
|
$schedules["wp_queue_cron_interval_{$this->id}"] = array( |
85
|
|
|
'interval' => MINUTE_IN_SECONDS * $this->interval, |
|
|
|
|
86
|
|
|
'display' => sprintf( __( 'Every %d Minutes' ), $this->interval ), |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return $schedules; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Process any jobs in the queue. |
94
|
|
|
*/ |
95
|
|
|
public function cron_worker() { |
96
|
|
|
$this->start_time = time(); |
97
|
|
|
|
98
|
|
|
while ( ! $this->time_exceeded() && ! $this->memory_exceeded() ) { |
99
|
|
|
if ( ! $this->worker->process() ) { |
100
|
|
|
break; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Memory exceeded |
107
|
|
|
* |
108
|
|
|
* Ensures the worker process never exceeds 80% |
109
|
|
|
* of the maximum allowed PHP memory. |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
protected function memory_exceeded() { |
114
|
|
|
$memory_limit = $this->get_memory_limit() * 0.8; // 80% of max memory |
115
|
|
|
$current_memory = memory_get_usage( true ); |
116
|
|
|
$return = false; |
117
|
|
|
|
118
|
|
|
if ( $current_memory >= $memory_limit ) { |
119
|
|
|
$return = true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return apply_filters( 'wp_queue_cron_memory_exceeded', $return ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get memory limit. |
127
|
|
|
* |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
|
|
protected function get_memory_limit() { |
131
|
|
|
if ( function_exists( 'ini_get' ) ) { |
132
|
|
|
$memory_limit = ini_get( 'memory_limit' ); |
133
|
|
|
} else { |
134
|
|
|
$memory_limit = '256M'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ( ! $memory_limit || - 1 == $memory_limit ) { |
138
|
|
|
// Unlimited, set to 1GB |
139
|
|
|
$memory_limit = '1000M'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return intval( $memory_limit ) * 1024 * 1024; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Time exceeded |
147
|
|
|
* |
148
|
|
|
* Ensures the worker never exceeds a sensible time limit (20s by default). |
149
|
|
|
* A timeout limit of 30s is common on shared hosting. |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
protected function time_exceeded() { |
154
|
|
|
$finish = $this->start_time + apply_filters( 'wp_queue_cron_time_limit', 20 ); // 20 seconds |
155
|
|
|
$return = false; |
156
|
|
|
|
157
|
|
|
if ( time() >= $finish ) { |
158
|
|
|
$return = true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return apply_filters( 'wp_queue_cron_time_exceeded', $return ); |
162
|
|
|
} |
163
|
|
|
} |