A tiny (and extremely opinionated) framework to use mqueue and stop to use tables as queues.
When companies need to make fast solutions for notification or communication with customer a lot of them use database tables as queues which are hard to maintain and use a lot of database resources. Besides all, it always involves nasty things (and you can see some of them here) and side effects.
It's really, really simple:
- Declare a queue with a string name;
- Create a consumer for that queue;
- Create a producer for the same queue;
- Publish messages!
MicroQueue
does not have any configuration and use events to give you more flexibility! (Of course it was the only thing that I could think to avoid my consumer to know the producer... So, sorry).
The script below is an example of how simple it is:
// Consumer.php
$queue = MicroQueue\Queue::declareQueue('sms');
$eventDispatcher = new \Armadillo\EventDispatcher;
$consumer = new MicroQueue\Consumer($queue, $eventDispatcher);
$consumer->consume(function($message, $eventDispatcher) {
// your code for consuming messages
}
// Producer.php
$queue = MicroQueue\Queue::declareQueue('sms');
$producer = new MicroQueue\Producer($queue);
$producer->publish('This is a test' . PHP_EOL);
This should be enough to start publishing messages. The examples folder has one example on how to use events to extend consumer functionality.
This package is available on Packagist and you can use composer to install it.
NO. Because of the following reasons:
MicroQueue
has a blocking nature (and most of the other services have not) and this is not nice for big jobs;MicroQueue
is limited by session (consumer and producer need to exist in same session of Linux, for example) and by your OS (OS controls number of queues, number of messages and message size);MicroQueue
tests will start soon (in a production environment) so I cannot ensure its reliability.
Producer
and Consumer
test will use mqueue
installed in your machine. If you cannot run them for some hardware limitation, please use the vagrant machine to do it. And sorry for have no idea on how I could have done unit tests.
- Use another event lib like
evenement
(that is also tiny); - Create a console app to help to create consumers as daemons;