Skip to content

Commit 6e91f14

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

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
20+
/**
21+
* Sends emails for the memory spool.
22+
*
23+
* Emails are sent on the gearman.client.callback.complete event.
24+
*
25+
* @author Dominic Luechinger <[email protected]>
26+
*/
27+
class SwiftmailerEmailSenderListener implements EventSubscriberInterface
28+
{
29+
30+
/**
31+
* @var ContainerInterface
32+
*/
33+
private $container;
34+
35+
/**
36+
* Set container
37+
*
38+
* @param ContainerInterface $container Container
39+
*
40+
* @return GearmanExecute self Object
41+
*/
42+
public function setContainer(ContainerInterface $container)
43+
{
44+
$this->container = $container;
45+
46+
return $this;
47+
}
48+
49+
public function onComplete()
50+
{
51+
if (!$this->container->has('swiftmailer.email_sender.listener')) {
52+
return;
53+
}
54+
$this->container->get('swiftmailer.email_sender.listener')->onTerminate();
55+
}
56+
57+
/**
58+
* {@inheritDoc}
59+
*/
60+
public static function getSubscribedEvents()
61+
{
62+
return array(
63+
GearmanEvents::GEARMAN_CLIENT_CALLBACK_COMPLETE => 'onComplete',
64+
);
65+
}
66+
}

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)