Download the PHP package dotkernel/dot-queue without Composer
On this page you can find all versions of the php package dotkernel/dot-queue. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package dot-queue
dot-queue
DotKernel queue component
Note
dot-queue is abandoned and will receive no further development!
Requirements
- PHP >= 7.1
- zendframework/zend-servicemanager
- zendframework/zend-db (optional - install if using the database adapter)
Installation
Run the following command
After installing all dependencies, add the \Dot\Queue\ConfigProvider::class
to your configuration aggregate, in order to register all dependencies and console commands.
Queues
The following queue implementations are provided by this package
Dot\Queue\Queue\InMemoryQueue
- non-persisten queue, based on \SplQueue, mainly for testing purposesDot\Queue\Queue\PersistentQueue
- uses an adapter to persist/fetch jobs to/from a storage
Queues must implement Dot\Queue\Queue\QueueInterface
or extend Dot\Queue\Queue\AbstractQueue
.
Queue adapters
Queue adapters are used in collaboration with the PersistentQueue
. Queue adapters are specific to the storage used
Provided queue adapters:
Dot\Queue\Adapter\DatabaseAdapter
- based onzend-db
, enqueues/dequeues jobs using a MySql storage
Queue adapters must implement Dot\Queue\Adapter\AdapterInterface
Configuring queues
At this moment, the package offers only a database adapter to be used with the persistent queue. Therefore, the config template below shows how to configure a MySQL queue.
Create a config file in your config/autoload
folder, and replace the {{QUEUE_NAME}}
with something appropriate
queue.global.php
You can configure multiple adapters and multiple queues. Multiple queues can also use the same queue adapter.
Creating job classes
A job represent the unit of work that will be processed by the queue as the queue is consumed.
Create job classes by extending Dot\Queue\Job\AbstractJob
.
A job must declare 2 methods
process()
- will be called when the job is processed by the queue. Do your work in here.failed($e)
- called when the job fails(the max attempts are exceeded). It will receive the exception that caused the failure
You can also inject the job class with the needed dependencies. Use a factory class and register the job in the service container for that.
The QueueManager
The Dot\Queue\Queue\QueueManager
is the main class to be injected wherever you dispatch job to a queue.
In order to create and dispatch a job
Important
-
When creating jobs, always use the queue managers
->createJob(className)
method. This will make sure the job is fetched from the container and will be properly initialized. -
In order to avoid serialization complication, we advice you to set only scalar or array data into the jobs' payload. This should not represent a limitation, because you can inject services into the job, that can later fetch objects from database and so on...
- Jobs already define some sane defaults for the max attempts, timeout and other job option. Override only what you need.
Consuming jobs
Run the following dotkernel command in order to start the worker loop to consume the default queue
For details on the supported command options run
Useful consumer options
--all
- consume all defined queues, in a round robin fashion--queues=
- comma separated list of queues to consume--max-runtime=
- run the consumer only the specified number of seconds--max-jobs=
- run the consumer until the specified number of jobs have been processed(this includes also the failed jobs)--sleep=
- pause the queue for the specified amount of seconds(in case the queue is empty) Check the command's help for a full list of options
In production, we advise you to use a monitoring software, such as supervisord
in order to make sure that the consumer is kept alive.
During development you can emulate supervisord with the npm-package called forever
Database migrations
In order to generate migrations files (to be used by Phinx library) for the jobs table and failed jobs table, two commands are provided
$ php dot queue:jobs-table
$ php dot queue:failed-table
Running these commands, will generate migration files with the following default options
- the namespace will be set to
Data\Database\Migrations
- the table names will be
jobs
andfailed_jobs
respectively - the path where the files will be generated
data/database/migrations
You can override these options using the --namespace=
, --table-name=
and --path
options respectively
After you have generated the files you can run
in order to create the tables
Handling failed jobs
We provide several commands to help you manage the failed jobs
php dot queue:failed [--queue=]
lists all failed jobs, or filtered by queue namephp dot queue:flush [--queue=]
remove all failed jobs, optionally filtered by queue namephp dot queue:forget <uuid>
remove the job with specified ID from the failed job listphp dot queue:retry [<uuid>] [--queue]
re-dispatch a job back into its queue, for retrying. If no ID given, retry all failed jobs or filtered by queue
@TODO - QUEUE EVENTS
All versions of dot-queue with dependencies
zendframework/zend-servicemanager Version ^3.3
dotkernel/dot-console Version ^0.1.0
phplucidframe/console-table Version ^1.2
ramsey/uuid Version ^3.7
zendframework/zend-validator Version ^2.10
zendframework/zend-filter Version ^2.7
psr/log Version ^1.0