Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event listener on job complete that flushes the swiftmailer spool #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -47,7 +47,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