Skip to content

Commit

Permalink
Add event listener on job complete that flushes the swiftmailer spool
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Luechinger committed Apr 7, 2017
1 parent a69e470 commit 7fbb6e1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
67 changes: 67 additions & 0 deletions EventListener/SwiftmailerEmailSenderListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* Gearman Bundle for Symfony2
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Dominic Luechinger <[email protected]>
*/

namespace Mmoreram\GearmanBundle\EventListener;

use Mmoreram\GearmanBundle\GearmanEvents;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Sends emails for the memory spool.
*
* Emails are sent on the gearman.client.callback.complete event.
*
* @author Dominic Luechinger <[email protected]>
*/
class SwiftmailerEmailSenderListener implements EventSubscriberInterface
{

/**
* @var ContainerInterface
*/
private $container;

/**
* Set container
*
* @param ContainerInterface $container Container
*
* @return GearmanExecute self Object
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;

return $this;
}

public function onComplete()
{
if (!$this->container->has('swiftmailer.email_sender.listener')) {
return;
}
$this->container->get('swiftmailer.email_sender.listener')->onTerminate();
}

/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return array(
GearmanEvents::GEARMAN_CLIENT_CALLBACK_COMPLETE => 'onComplete',
);
}
}
5 changes: 5 additions & 0 deletions Resources/config/classes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ parameters:
gearman.parser.class: Mmoreram\GearmanBundle\Service\GearmanParser
gearman.cache.wrapper.class: Mmoreram\GearmanBundle\Service\GearmanCacheWrapper

#
# EventListeners
#
gearman.swiftmailer_email_sender.listener.class: Mmoreram\GearmanBundle\EventListener\SwiftmailerEmailSenderListener

#
# Commands
#
Expand Down
8 changes: 7 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ services:
parent: gearman.abstract.service
calls:
- [setContainer, ["@service_container"]]
- [setEventDispatcher, ["@event_dispatcher"]]

gearman.swiftmailer_email_sender.listener:
class: "%gearman.swiftmailer_email_sender.listener.class%"
calls:
- [setContainer, ["@service_container"]]
tags:
- { name: kernel.event_subscriber }

gearman:
class: "%gearman.client.class%"
Expand Down

0 comments on commit 7fbb6e1

Please sign in to comment.