Skip to content

php-bundles/event-queue-bundle

Repository files navigation

SymfonyBundlesEventQueueBundle

SensioLabsInsight

Build Status Scrutinizer Code Quality Code Coverage Total Downloads Latest Stable Version License

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'

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.