Download the PHP package nilportugues/eventbus-queue without Composer
On this page you can find all versions of the php package nilportugues/eventbus-queue. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nilportugues/eventbus-queue
More information about nilportugues/eventbus-queue
Files in nilportugues/eventbus-queue
Package eventbus-queue
Short Description Event Bus Queue library. Allows implementing asynchronous Event Bus.
License MIT
Homepage http://nilportugues.com
Informations about the package eventbus-queue
EventBus Queue
- Installation
- Usage
- ProducerEventBus
- Consumer for the ProducerEventBus
- EventBusWorker
- Adapter Configurations
- PDOQueue
- MongoDBQueue
- RedisQueue
- PredisQueue
- FileSystemQueue
- AmqpQueue
- BeanstalkdQueue
- Contribute
- Support
- Authors
- License
This package is an extension library for nilportugues/messagebus, adding queues to the EventBus implementation.
This package will provide you for the classes necessary to build:
- The Producer: code that sends the Event to a Queue by serializing the Event. This happens synchronously.
- The Consumer: code that reads in the background, therefore asynchronously, reads and unserializes the Event from the Queue and passes it to the EventBus to do the heavy lifting.
Why?
It's all about deciding which command logic can be delayed, or hidden away in order to make it faster. And this is what we want to do.
You never remove a bottleneck, you just move it. The downside is that we might have to assume a possible delay.
Installation
In order to start using this project you require to install it using Composer:
Usage
This package will provide you with a new middleware: ProducerEventBusMiddleware
.
This middleware requires a serializer and a storage that will depend on the Queue Adapter used. Supported adapters are:
- PDOQueue: queue built with a SQL database using Doctrine's DBAL.
- MongoDBQueue: queue built with MongoDB library.
- RedisQueue: queue using the Redis PHP extension.
- PredisQueue: queue using the Predis library.
- FileSystemQueue: queue built with using the local file system.
- AmqpQueue: use RabbitMQ or any queue implementing the Amqp protocol.
- BeanstalkdQueue: use Beanstalk as queue.
To set it up, register the ProducerEventBusMiddleware
to the Event Bus. Because we'll need to define a second EventBus (consumer), we'll call this the ProducerEventBus
.
ProducerEventBus
Consumer for the ProducerEventBus
The Consumer will need to be a script that reads the EventBus definitions and subscribed events in order to run until all events are handled. To do so, we'll need to register a new EventBus
we'll refer as ConsumerEventBus
.
We will also like to store events that could not be handled or raised an exception. So a new Queue will be required. For instance, let's store errors in a MongoDB database.
This could be as simple as follows:
EventBusWorker
Finally, we'll have to call a consumer. This package already provides a fully working consumer implementation: EventBusWorker
.
Use it as follows:
Consumer class will run the consume
method until all events are consumed. Then it will exit. This is optimal to make sure it will not leak memory.
If you need to keep the consumer running forever use server scripts like Supervisor.
Supervisor Configuration
Supervisor is a process monitor for the Linux operating system, and will automatically restart your workers if they fail. To install Supervisor on Ubuntu, you may use the following command:
Supervisor configuration files are typically stored in the /etc/supervisor/conf.d
directory. Within this directory, you may create any number of configuration files that instruct how your processes should be monitored.
For instance, let's create /etc/supervisor/conf.d/my_worker.conf
so that it starts and monitors a worker script named my_worker.php
:
In this file, we tell Supervisor that we want 20 instances always running. If the my_worker.php
ends or fails it will spin up a new one.
In order to make this task run forever, you'll have to type in the following commands:
Adapter Configurations
PDOQueue
For this to work, you'll be required to create a table in your database.
For instance, sqlite dialect table creation would be:
MongoDBQueue
In order to use it, you require to install PHP 7's mongodb extension.
RedisQueue
In order to use it, you require to install PHP 7's phpredis extension.
PredisQueue
Nothing, but performs better if phpredis extension is found.
FileSystemQueue
Nothing to do.
AmqpQueue
Nothing to do other than having access to a amqp server.
Beanstalkd
Nothing to do other than having access to a beanstalkd server.
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
Support
Get in touch with me using one of the following means:
- Emailing me at [email protected]
- Opening an Issue
Authors
License
The code base is licensed under the MIT license.