slm / queue
Laminas module that integrates with various queue management systems
Installs: 738 007
Dependents: 14
Suggesters: 1
Security: 0
Stars: 138
Watchers: 15
Forks: 55
Open Issues: 2
Requires
- php: ~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- ext-json: *
- laminas/laminas-cli: ^1.2
- laminas/laminas-eventmanager: ^3.4
- laminas/laminas-servicemanager: ^3.11
- laminas/laminas-stdlib: ^3.7.1
Requires (Dev)
- laminas/laminas-config: ^3.7
- laminas/laminas-i18n: ^2.12
- laminas/laminas-log: ^2.15
- laminas/laminas-modulemanager: ^2.11
- laminas/laminas-mvc: ^3.3
- laminas/laminas-serializer: ^2.11
- laminas/laminas-view: ^2.13
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: ^3.6.2
Suggests
- rnd-cosoft/slm-queue-rabbitmq: If you are using RabbitMQ
- slm/queue-beanstalkd: If you are using Beanstalk
- slm/queue-doctrine: If you are using Doctrine ORM
- slm/queue-sqs: If you are using Amazon SQS
- 3.3.0
- 3.2.0
- 3.1.1
- 3.1.0
- 3.1-beta
- 3.0.1
- 3.0.0
- 3.0-beta.4
- 3.0-beta.3
- 3.0-beta.2
- 3.0-beta
- 2.1.0
- 2.0.1
- 2.0.0
- dev-master / 1.1.x-dev
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.8.0
- 0.7.0
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.2
- 0.4.1
- 0.4.0
- 0.4.0-beta3
- 0.4.0-beta2
- 0.4.0-beta1
- 0.3.0
- 0.3.0-rc1
- 0.3.0-beta3
- 0.3.0-beta2
- 0.3.0-beta1
- 0.2.5
- 0.2.4
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2
- 0.1
- dev-feature/test-build
- dev-id
- dev-correct-count
- dev-feature/simplified-listeners
This package is auto-updated.
Last update: 2024-07-03 10:19:30 UTC
README
SlmQueue
Important notice
We decided to move onto Symfony Messenger and we are therefore not maintaining this repository anymore. Feel free to fork it and make it your own.
SlmQueue is a job queue abstraction layer for Laminas (formerly Zend Framework) and Mezzio (formerly Zend Expressive) applications. It supports various job queue systems and makes your application independent from the underlying system you use. The currently supported systems have each their own adapter library:
- Doctrine ORM
- RabbitMQ
- Amazon SQS (outdated, maintainers wanted)
When to use
A job queue helps to offload long or memory-intensive processes from the HTTP requests clients sent to the Laminas application. This will make your response times shorter and your visitors happier. There are many use cases for asynchronous jobs and a few examples are:
- Send an email
- Create a PDF file
- Connect to a third party server over HTTP
In all cases you want to serve the response as soon as possible to your visitor, without letting them wait for this long process. SlmQueue enables you to implement a job queue system very easily within your existing application.
Installation
SlmQueue works with Composer. Make sure you have the composer.phar downloaded and you have a
composer.json
file at the root of your project. To install it, add the following line into your composer.json
file:
"require": { "slm/queue": "^3.0" }
After installation of the package, you need to complete the following steps to use SlmQueue:
- Enable the module by adding
SlmQueue
in yourapplication.config.php
file. - Copy the
slm_queue.global.php.dist
(you can find this file in theconfig
folder of SlmQueue) into yourconfig/autoload
folder and apply any setting you want.
NB. SlmQueue is a skeleton and therefore useless by itself. Enable an adapter to give you the implementation details you need to push jobs into the queue. Install one of the available adapters (SlmQueueDoctrine, SlmQueueRabbitMq, or SlmQueueSqs).
Requirements
- PHP >= 7.4
- laminas-servicemanager >= 3.3.1
Code samples
Below are a few snippets which show the power of SlmQueue in your application. The full documentation is available in docs/ directory.
A sample job to send an email with php's mail()
might look like this:
namespace MyModule\Job; use SlmQueue\Job\AbstractJob; class EmailJob extends AbstractJob { public static function create(string $to, string $subject, string $message): self { // This will bypass the constructor, and thus load a job without // having to load the dependencies. $job = self::createEmptyJob([ 'subject' => $subject, 'to' => $to, 'message' => $message, ]); // Add some metadata, so we see what is going on. $job->setMetadata('to', $to); return $job; } private SomeMailService $mailService; public function __construct(SomeMailService $mailService) { $this->mailService = $mailService; } public function execute() { $payload = $this->getContent(); $to = $payload['to']; $subject = $payload['subject']; $message = $payload['message']; $this->mailService->send($to, $subject, $message); } }
If you want to inject this job into a queue, you can do this for instance in your controller:
namespace MyModule\Controller; use MyModule\Job\Email as EmailJob; use SlmQueue\Queue\QueueInterface; use Laminas\Mvc\Controller\AbstractActionController; class MyController extends AbstractActionController { protected $queue; public function __construct(QueueInterface $queue) { $this->queue = $queue; } public function fooAction() { // Do some work $this->queue->push( EmailJob::create('john@doe.com', 'Just hi', 'Hi, I want to say hi!'), ['delay' => 60] ); } }
Now the above code lets you insert jobs in a queue, but then you need to spin up a worker which can process these jobs.
SlmQueue integrates with laminas-cli
for command line usage. You can start up a worker for queue "default" with the following command:
$ vendor/bin/laminas slm-queue:start default
Contributing
SlmQueue is developed by various fanatic Laminas users. The code is written to be as generic as possible for Laminas applications. If you want to contribute to SlmQueue, fork this repository and start hacking!
Any bugs can be reported as an issue at GitHub. If you want to contribute, please be aware of the following guidelines:
- Fork the project to your own repository
- Use branches to work on your own part
- Create a Pull Request at the canonical SlmQueue repository
- Make sure to cover changes with the right amount of unit tests
- If you add a new feature, please work on some documentation as well
For long-term contributors, push access to this repository is granted.
Who to thank?
Jurian Sluiman and Michaël Gallego did the initial work on creating this repo, and maintained it for a long time.
Currently it is maintained by: