Skip to content

Latest commit

 

History

History
55 lines (41 loc) · 2.63 KB

5.Workers.md

File metadata and controls

55 lines (41 loc) · 2.63 KB

Documentation - Workers

After queues are configured, jobs classes are written and pushed into a queue, they must be popped out by a worker to be executed. This is done, as explained in the introduction, by workers. A worker for SlmQueue is a class implementing the SlmQueue\Worker\WorkerInterface.

A worker is started by by calling its processQueue($queue, array $options = array()) method. The $queue parameter is a string and should be the service name of the queue as registered in the queue plugin manager. The $options parameter is an optional array which is directly passed on to the pop() method of the queue. This can contain flags specifically for the different queue implementations.

Worker stop conditions

The worker will be a long running call, due to the while(...){ /*...*/ } loop it contains inside the processQueue() method. However, there are reasons to cancel the loop and stop the worker to continue. PHP is not the best language for creating infinite running scripts.

It is wise to abort the script frequently, for example after x number of cycles in the while loop.

Various build-in strategies ('see 6.Events') are used to decide if the worker should exit. These strategies are aggregate listeners that hook into events the worker dispatches. A listener (to 'process.queue' or 'process.idle') may shortcut the event flow simply be returning a ExitWorkerLoopResult::withReason('a reason'). The worker will inspect the response collection.

Command line utility

SlmQueue provides integration with the Laminas console routes and ease the use of workers. You can start a worker for e.g. the beanstalkd queue system by running the following command*:

php public/index.php queue beanstalkd <queue-name>

*) assumed here is your working directory is the application root and your application is structured like the Laminas Skeleton Application with public directory and an index.php file.

This CLI command dispatches a SlmQueue controller. A worker is injected into the controller and it simply calls the worker's processQueue() method. Every queue implementation has it's own CLI command so read the documentation for these implementations for the exact command.

Navigation

Previous page: Queue Aware Next page: Events

  1. Introduction
  2. Configuration
  3. Jobs
  4. QueueAware
  5. Workers
  6. Events
  7. Worker management