You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Background processes work in a similar fashion to async requests, but they allow you to queue tasks. Items pushed onto the queue will be processed in the background once the queue has been dispatched. Queues will also scale based on available server resources, so higher end servers will process more items per batch. Once a batch has completed, the next batch will start instantly.
93
+
Background processes work in a similar fashion to async requests, but they allow you to queue tasks. Items pushed onto the queue will be processed in the background once the queue has been saved and dispatched. Queues will also scale based on available server resources, so higher end servers will process more items per batch. Once a batch has completed, the next batch will start instantly.
77
94
78
95
Health checks run by default every 5 minutes to ensure the queue is running when queued items exist. If the queue has failed it will be restarted.
79
96
80
-
Queues work on a first in first out basis, which allows additional items to be pushed to the queue even if it’s already processing.
97
+
Queues work on a first in first out basis, which allows additional items to be pushed to the queue even if it’s already processing. Saving a new batch of queued items and dispatching while another background processing instance is already running will result in the dispatch shortcutting out and the existing instance eventually picking up the new items and processing them when it is their turn.
81
98
82
99
Extend the `WP_Background_Process` class:
83
100
84
101
```php
85
102
class WP_Example_Process extends WP_Background_Process {
86
103
104
+
/**
105
+
* @var string
106
+
*/
107
+
protected $prefix = 'my_plugin';
108
+
87
109
/**
88
110
* @var string
89
111
*/
@@ -122,23 +144,29 @@ class WP_Example_Process extends WP_Background_Process {
122
144
}
123
145
```
124
146
125
-
##### `protected $action`
147
+
#### `protected $prefix`
148
+
149
+
Should be set to a unique prefix associated with your plugin, theme, or site's custom function prefix.
150
+
151
+
#### `protected $action`
126
152
127
153
Should be set to a unique name.
128
154
129
-
#####`protected function task( $item )`
155
+
#### `protected function task( $item )`
130
156
131
157
Should contain any logic to perform on the queued item. Return `false` to remove the item from the queue or return `$item` to push it back onto the queue for further processing. If the item has been modified and is pushed back onto the queue the current state will be saved before the batch is exited.
132
158
133
-
#####`protected function complete()`
159
+
#### `protected function complete()`
134
160
135
161
Optionally contain any logic to perform once the queue has completed.
136
162
137
-
#####Dispatching Processes
163
+
#### Dispatching Processes
138
164
139
165
Instantiate your process:
140
166
141
-
`$this->example_process = new WP_Example_Process();`
167
+
```php
168
+
$this->example_process = new WP_Example_Process();
169
+
```
142
170
143
171
**Note:** You must instantiate your process unconditionally. All requests should do this, even if nothing is pushed to the queue.
A background process can be queued, processing, paused, cancelled, or none of the above (not started or has completed).
190
+
191
+
##### Queued
192
+
193
+
To check whether a background process has queued items use `is_queued()`.
194
+
195
+
```php
196
+
if ( $this->example_process->is_queued() ) {
197
+
// Do something because background process has queued items, e.g. add notice in admin UI.
198
+
}
199
+
```
200
+
201
+
##### Processing
202
+
203
+
To check whether a background process is currently handling a queue of items use `is_processing()`.
204
+
205
+
```php
206
+
if ( $this->example_process->is_processing() ) {
207
+
// Do something because background process is running, e.g. add notice in admin UI.
208
+
}
209
+
```
210
+
211
+
##### Paused
212
+
213
+
You can pause a background process with `pause()`.
214
+
215
+
```php
216
+
$this->example_process->pause();
217
+
```
218
+
219
+
The currently processing batch will continue until it either completes or reaches the time or memory limit. At that point it'll unlock the process and either complete the batch if the queue is empty, or perform a dispatch that will result in the handler removing the healthcheck cron and firing a "paused" action.
220
+
221
+
To check whether a background process is currently paused use `is_paused()`.
222
+
223
+
```php
224
+
if ( $this->example_process->is_paused() ) {
225
+
// Do something because background process is paused, e.g. add notice in admin UI.
226
+
}
227
+
```
228
+
229
+
You can perform an action in response to background processing being paused by handling the "paused" action for the background process's identifier ($prefix + $action).
// Do something because background process is paused, e.g. add notice in admin UI.
234
+
});
235
+
```
236
+
237
+
You can resume a background process with `resume()`.
238
+
239
+
```php
240
+
$this->example_process->resume();
241
+
```
242
+
243
+
You can perform an action in response to background processing being resumed by handling the "resumed" action for the background process's identifier ($prefix + $action).
// Do something because background process is resumed, e.g. add notice in admin UI.
248
+
});
249
+
```
250
+
251
+
##### Cancelled
252
+
253
+
You can cancel a background process with `cancel()`.
254
+
255
+
```php
256
+
$this->example_process->cancel();
257
+
```
258
+
259
+
The currently processing batch will continue until it either completes or reaches the time or memory limit. At that point it'll unlock the process and either complete the batch if the queue is empty, or perform a dispatch that will result in the handler removing the healthcheck cron, deleting all batches of queued items and firing a "cancelled" action.
260
+
261
+
To check whether a background process is currently cancelled use `is_cancelled()`.
262
+
263
+
```php
264
+
if ( $this->example_process->is_cancelled() ) {
265
+
// Do something because background process is cancelled, e.g. add notice in admin UI.
266
+
}
267
+
```
268
+
269
+
You can perform an action in response to background processing being cancelled by handling the "cancelled" action for the background process's identifier ($prefix + $action).
// Do something because background process is paused, e.g. add notice in admin UI.
274
+
});
275
+
```
276
+
277
+
The "cancelled" action fires once the queue has been cleared down and cancelled status removed. After which `is_cancelled()` will no longer be true as the background process is now dormant.
278
+
279
+
##### Active
280
+
281
+
To check whether a background process has queued items, is processing, is paused, or is cancelling, use `is_active()`.
282
+
283
+
```php
284
+
if ( $this->example_process->is_active() ) {
285
+
// Do something because background process is active, e.g. add notice in admin UI.
286
+
}
287
+
```
288
+
289
+
If a background process is not active, then it either has not had anything queued yet and not started, or has finished processing all queued items.
0 commit comments