Skip to content

Commit 8b95ce3

Browse files
committed
Listen to kernel.terminate to decide if messages should be flushed instantly
1 parent 767fbb9 commit 8b95ce3

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

DependencyInjection/MonologExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
277277
$handler['bubble'],
278278
));
279279
$definition->addMethodCall('setTransport', array(new Reference('swiftmailer.transport.real')));
280+
$definition->addTag('kernel.event_listener', array('event' => 'kernel.terminate', 'method' => 'onKernelTerminate'));
280281
break;
281282

282283
case 'native_mailer':

Handler/SwiftMailerHandler.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

33
/*
4-
* This file is part of the Monolog package.
4+
* This file is part of the Symfony package.
55
*
6-
* (c) Jordi Boggiano <[email protected]>
6+
* (c) Fabien Potencier <[email protected]>
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\MonologBundle\Handler;
1313

1414
use Monolog\Handler\SwiftMailerHandler as BaseSwiftMailerHandler;
15+
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
1516

1617
/**
1718
* Extended SwiftMailerHandler that flushes mail queue if necessary
@@ -22,6 +23,8 @@ class SwiftMailerHandler extends BaseSwiftMailerHandler
2223
{
2324
protected $transport;
2425

26+
protected $instantFlush = false;
27+
2528
/**
2629
* @param \Swift_Transport $transport
2730
*/
@@ -30,20 +33,32 @@ public function setTransport(\Swift_Transport $transport)
3033
$this->transport = $transport;
3134
}
3235

36+
/**
37+
* After the kernel has been terminated we will always flush messages
38+
*
39+
* @param PostResponseEvent $event
40+
*/
41+
public function onKernelTerminate(PostResponseEvent $event)
42+
{
43+
$this->instantFlush = true;
44+
}
45+
3346
/**
3447
* {@inheritdoc}
3548
*/
3649
protected function send($content, array $records)
3750
{
3851
parent::send($content, $records);
3952

40-
$this->flushQueue();
53+
if ($this->instantFlush) {
54+
$this->flushMemorySpool();
55+
}
4156
}
4257

4358
/**
44-
* Flushes the mail queue if necessary
59+
* Flushes the mail queue if a memory spool is used
4560
*/
46-
private function flushQueue()
61+
private function flushMemorySpool()
4762
{
4863
$transport = $this->mailer->getTransport();
4964
if (!$transport instanceof \Swift_Transport_SpoolTransport) {

0 commit comments

Comments
 (0)