Skip to content

Commit 7fbb6e1

Browse files
author
Dominic Luechinger
committed
Add event listener on job complete that flushes the swiftmailer spool
1 parent a69e470 commit 7fbb6e1

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/**
4+
* Gearman Bundle for Symfony2
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Dominic Luechinger <[email protected]>
12+
*/
13+
14+
namespace Mmoreram\GearmanBundle\EventListener;
15+
16+
use Mmoreram\GearmanBundle\GearmanEvents;
17+
use Psr\Log\LoggerInterface;
18+
use Symfony\Component\DependencyInjection\ContainerInterface;
19+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20+
21+
/**
22+
* Sends emails for the memory spool.
23+
*
24+
* Emails are sent on the gearman.client.callback.complete event.
25+
*
26+
* @author Dominic Luechinger <[email protected]>
27+
*/
28+
class SwiftmailerEmailSenderListener implements EventSubscriberInterface
29+
{
30+
31+
/**
32+
* @var ContainerInterface
33+
*/
34+
private $container;
35+
36+
/**
37+
* Set container
38+
*
39+
* @param ContainerInterface $container Container
40+
*
41+
* @return GearmanExecute self Object
42+
*/
43+
public function setContainer(ContainerInterface $container)
44+
{
45+
$this->container = $container;
46+
47+
return $this;
48+
}
49+
50+
public function onComplete()
51+
{
52+
if (!$this->container->has('swiftmailer.email_sender.listener')) {
53+
return;
54+
}
55+
$this->container->get('swiftmailer.email_sender.listener')->onTerminate();
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
public static function getSubscribedEvents()
62+
{
63+
return array(
64+
GearmanEvents::GEARMAN_CLIENT_CALLBACK_COMPLETE => 'onComplete',
65+
);
66+
}
67+
}

Resources/config/classes.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ parameters:
2525
gearman.parser.class: Mmoreram\GearmanBundle\Service\GearmanParser
2626
gearman.cache.wrapper.class: Mmoreram\GearmanBundle\Service\GearmanCacheWrapper
2727

28+
#
29+
# EventListeners
30+
#
31+
gearman.swiftmailer_email_sender.listener.class: Mmoreram\GearmanBundle\EventListener\SwiftmailerEmailSenderListener
32+
2833
#
2934
# Commands
3035
#

Resources/config/services.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ services:
4141
parent: gearman.abstract.service
4242
calls:
4343
- [setContainer, ["@service_container"]]
44-
- [setEventDispatcher, ["@event_dispatcher"]]
44+
45+
gearman.swiftmailer_email_sender.listener:
46+
class: "%gearman.swiftmailer_email_sender.listener.class%"
47+
calls:
48+
- [setContainer, ["@service_container"]]
49+
tags:
50+
- { name: kernel.event_subscriber }
4551

4652
gearman:
4753
class: "%gearman.client.class%"

0 commit comments

Comments
 (0)