symfony-bundles / event-queue-bundle
Symfony EventQueue Bundle
Installs: 12 646
Dependents: 0
Suggesters: 0
Security: 0
Stars: 25
Watchers: 7
Forks: 1
Type:symfony-bundle
Requires
- php: >=5.6
- symfony-bundles/bundle-dependency: ^1.0
- symfony-bundles/queue-bundle: ^1.0
- symfony/console: ^3.0
Requires (Dev)
- phpunit/php-code-coverage: ^3.3.0|^4.0
- phpunit/phpunit: ^5.3
README
Installation
- Require the bundle with composer:
composer require symfony-bundles/event-queue-bundle
- Enable the bundle in the kernel:
public function registerBundles() { $bundles = [ // ... new SymfonyBundles\EventQueueBundle\SymfonyBundlesEventQueueBundle(), // ... ]; ... }
- Configure the EventQueue bundle in your config.yml.
Defaults configuration:
sb_event_queue: service_name: 'event_queue' default_name: 'event:default' storage_path: '%kernel.cache_dir%/event-queue-daemon.%s.pid'
- Configure the redis client in your config.yml. Read more about RedisBundle configuration.
How to use
Add an event to the queue:
$dispatcher = $this->get('sb_event_queue'); $dispatcher->on(MyEvent::class, date('Y-m-d H:i:s'), 'Example message');
Your event class must implement SymfonyBundles\EventQueueBundle\EventInterface
(or extending SymfonyBundles\EventQueueBundle\Event
class)
and having constant NAME
with the event name. For example:
namespace AppBundle\Event; use SymfonyBundles\EventQueueBundle\Event; class MyEvent extends Event { const NAME = 'event.example'; private $time; private $message; public function __construct($time, $message) { $this->time = $time; $this->message = $message; } public function getTime() { return $this->time; } public function getMessage() { return $this->message; } }
Creating an Event Listener. The most common way to listen to an event is to register an event listener:
namespace AppBundle\EventListener; use AppBundle\Event\MyEvent; class MyListener { public function onEventExample(MyEvent $event) { $event->getTime(); $event->getMessage(); // and we are doing something... } }
Now that the class is created, you just need to register it as a service and notify Symfony that it is a "listener":
services: app.my_listener: class: AppBundle\EventListener\MyListener tags: - { name: kernel.event_listener, event: event.example }
Execution (dispatching) of all events from the queue:
while ($dispatcher->count()) { $dispatcher->dispatch(); }
You can separate the events by section, specifying in event manager the needed section.
$dispatcher->setName('email.notifications'); $dispatcher->on(EmailNotifyEvent::class, 'Subject', 'Message Body', ['john@domain.com', 'alex@domain.com']); $dispatcher->on(EmailNotifyEvent::class, 'Another subject', 'Another message Body', ['demo@domain.com']); $dispatcher->setName('database.reindex'); $dispatcher->on(DatabaseReindexEvent::class, 'users'); $dispatcher->on(DatabaseReindexEvent::class, 'orders'); $dispatcher->on(DatabaseReindexEvent::class, 'products');
In this example, will be executed only an events from the section email.notifications
:
$dispatcher->setName('email.notifications'); while ($dispatcher->count()) { $dispatcher->dispatch(); }
Console commands:
event:queue:dispatch
event:queue:daemon:start
event:queue:daemon:stop
In what situations is useful to apply the queue of events:
- When sending email messages
- Parsing websites
- and in other cases, when the execution time of process is very long, and the response from the server must be returned immediately.