Skip to content

Commit 9a8db82

Browse files
committed
🚀 Adding first version
1 parent da17236 commit 9a8db82

File tree

5 files changed

+1162
-0
lines changed

5 files changed

+1162
-0
lines changed

‎README.md‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# WP Queue Process
2+
3+
WP Queue Process can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks.
4+
5+
* Inspired by [TechCrunch WP Asynchronous Tasks](https://github.com/techcrunch/wp-async-task).
6+
* Forked from [WP Background Processing](https://github.com/deliciousbrains/wp-background-processing).
7+
8+
__Requires PHP 5.2+__
9+
10+
## Install
11+
12+
The recommended way to install this library in your project is by loading it through Composer:
13+
14+
```
15+
composer require duckdev/wp-queue-process
16+
```
17+
18+
## Usage
19+
20+
### Async Request
21+
22+
Async requests are useful for pushing slow one-off tasks such as sending emails to a background process. Once the request has been dispatched it will process in the background instantly.
23+
24+
Extend the `\DuckDev\Queue\Async` class:
25+
26+
```php
27+
class WP_Example_Request extends \DuckDev\Queue\Async {
28+
29+
/**
30+
* @var string
31+
*/
32+
protected $action = 'example_request';
33+
34+
/**
35+
* Handle
36+
*
37+
* Override this method to perform any actions required
38+
* during the async request.
39+
*/
40+
protected function handle() {
41+
// Actions to perform
42+
}
43+
}
44+
```
45+
46+
##### `protected $action`
47+
48+
Should be set to a unique name.
49+
50+
##### `protected function handle()`
51+
52+
Should contain any logic to perform during the non-blocking request. The data passed to the request will be accessible via `$_POST`.
53+
54+
##### Dispatching Requests
55+
56+
Instantiate your request:
57+
58+
`$this->example_request = new WP_Example_Request();`
59+
60+
Add data to the request if required:
61+
62+
`$this->example_request->data( array( 'value1' => $value1, 'value2' => $value2 ) );`
63+
64+
Fire off the request:
65+
66+
`$this->example_request->dispatch();`
67+
68+
Chaining is also supported:
69+
70+
`$this->example_request->data( array( 'data' => $data ) )->dispatch();`
71+
72+
### Background Process
73+
74+
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.
75+
76+
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.
77+
78+
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.
79+
80+
Extend the `\DuckDev\Queue\Task` class:
81+
82+
```php
83+
class WP_Example_Process extends \DuckDev\Queue\Task {
84+
85+
/**
86+
* @var string
87+
*/
88+
protected $action = 'example_process';
89+
90+
/**
91+
* Task
92+
*
93+
* Override this method to perform any actions required on each
94+
* queue item. Return the modified item for further processing
95+
* in the next pass through. Or, return false to remove the
96+
* item from the queue.
97+
*
98+
* @param mixed $item Queue item to iterate over
99+
*
100+
* @return mixed
101+
*/
102+
protected function task( $item ) {
103+
// Actions to perform
104+
105+
return false;
106+
}
107+
108+
/**
109+
* Complete
110+
*
111+
* Override if applicable, but ensure that the below actions are
112+
* performed, or, call parent::complete().
113+
*/
114+
protected function complete() {
115+
parent::complete();
116+
117+
// Show notice to user or perform some other arbitrary task...
118+
}
119+
}
120+
```
121+
122+
##### `protected $action`
123+
124+
Should be set to a unique name.
125+
126+
##### `protected function task( $item )`
127+
128+
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.
129+
130+
##### `protected function complete()`
131+
132+
Optionally contain any logic to perform once the queue has completed.
133+
134+
##### Dispatching Processes
135+
136+
Instantiate your process:
137+
138+
`$this->example_process = new WP_Example_Process();`
139+
140+
**Note:** You must instantiate your process unconditionally. All requests should do this, even if nothing is pushed to the queue.
141+
142+
Push items to the queue:
143+
144+
```php
145+
foreach ( $items as $item ) {
146+
$this->example_process->push_to_queue( $item );
147+
}
148+
```
149+
150+
Save and dispatch the queue:
151+
152+
`$this->example_process->save()->dispatch();`
153+
154+
### BasicAuth
155+
156+
If your site is behind BasicAuth, both async requests and background processes will fail to complete. This is because WP Background Processing relies on the [WordPress HTTP API](http://codex.wordpress.org/HTTP_API), which requires you to attach your BasicAuth credentials to requests. The easiest way to do this is using the following filter:
157+
158+
```php
159+
function ddqueue_http_request_args( $r, $url ) {
160+
$r['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );
161+
162+
return $r;
163+
}
164+
add_filter( 'http_request_args', 'ddqueue_http_request_args', 10, 2);
165+
```
166+
167+
### Credits
168+
* This is a forked, improved library of [WP Background Processing](https://github.com/deliciousbrains/wp-background-processing).
169+
* Maintained by [Joel James](https://github.com/joel-james/)
170+
171+
### License
172+
173+
[GPLv2+](http://www.gnu.org/licenses/gpl-2.0.html)

‎composer.json‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "duckdev/wp-queue-process",
3+
"description": "WordPress background queue processing library.",
4+
"license": "GPL-2.0-or-later",
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "Joel James",
9+
"email": "[email protected]",
10+
"homepage": "https://duckdev.com"
11+
}
12+
],
13+
"keywords": [
14+
"wordpress",
15+
"reviews",
16+
"notices"
17+
],
18+
"support": {
19+
"issues": "https://github.com/duckdev/wp-queue-process/issues/"
20+
},
21+
"require": {
22+
"php": ">=5.6"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"DuckDev\\Queue\\": "src"
27+
}
28+
},
29+
"minimum-stability": "dev"
30+
}

‎gitignore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Dependencies
2+
/vendor
3+
4+
# Generated
5+
/tests/.tmp
6+
composer.lock

0 commit comments

Comments
 (0)