Using Push Queues in PHP
In App Engine push queues, a task is a unit of work to be performed by the application.
- Using push queues
- Push task execution
- URL endpoints
- Push queues and the development server
- Quotas and limits for push queues
Using push queues
A PHP app sets up queues using a configuration file named
queue.yaml
(see
PHP Task Queue Configuration
). If an app does not have a
queue.yaml
file, it has a queue named
default
with some default settings.
To enqueue a task, create a
PushTask
object and call its
add()
method. You can add to a queue specified in
queue.yaml
by supplying a queue name argument to
add()
. Alternatively, calling
add()
with no arguments will add the task to the default queue.
The following code creates a task that will be sent as a POST request to the
/worker
handler of the application. The task contains name and action data, and will be processed by the default queue:
use google\appengine\api\taskqueue\PushTask;
$task = new PushTask('/worker.php', ['name' => 'john doe', 'action' => 'send_reminder']);
$task_name = $task->add();
You can also add tasks in bulk to a queue using
PushQueue
. In the following example, two
PushTask
objects are added to a
PushQueue
using the
addTasks()
method.
use google\appengine\api\taskqueue\PushTask;
use google\appengine\api\taskqueue\PushQueue;
$task1 = new PushTask('/someUrl');
$task2 = new PushTask('/someOtherUrl');
$queue = new PushQueue();
$queue->addTasks([$task1, $task2]);
Push task execution
App Engine executes push tasks by sending HTTP requests to your app. Specifying a programmatic asynchronous callback as an HTTP request is sometimes called a web hook . The web hook model enables efficient parallel processing.
The task's URL determines the handler for the task and the module that runs the handler.
The handler is determined by the path part of the URL (the forward-slash separated string following the hostname), which is specified as the first argument (the
url_path
) of the
PushTask
constructor. The path must be relative and local to your application's root directory.