-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Mesenger] Add support for reseting container services between 2 mess…
…ages Without this patch, services are not resetted. For example Monolog Finger Cross handler is never reset nor flushed. So if the first message trigger and "error" level message, all others message will log and overflow the buffer. Usage with framework: ```yaml framework: messenger: transports: async: dsn: '%env(MESSENGER_TRANSPORT_DSN)%' reset_on_message: true failed: 'doctrine://default?queue_name=failed' sync: 'sync://' ```
- Loading branch information
Showing
13 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.4 | ||
--- | ||
* Add support for resting container services after each messages. | ||
|
||
5.3 | ||
--- | ||
|
||
|
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/Messenger/EventListener/ResetServicesListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter; | ||
use Symfony\Component\Messenger\Event\AbstractWorkerMessageEvent; | ||
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; | ||
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; | ||
|
||
/** | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class ResetServicesListener implements EventSubscriberInterface | ||
{ | ||
private $servicesResetter; | ||
private $receiversName; | ||
|
||
public function __construct(ServicesResetter $servicesResetter, array $receiversName) | ||
{ | ||
$this->servicesResetter = $servicesResetter; | ||
$this->receiversName = $receiversName; | ||
} | ||
|
||
public function resetServices(AbstractWorkerMessageEvent $event) | ||
{ | ||
if (!\in_array($event->getReceiverName(), $this->receiversName, true)) { | ||
return; | ||
} | ||
|
||
$this->servicesResetter->reset(); | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
WorkerMessageHandledEvent::class => ['resetServices'], | ||
WorkerMessageFailedEvent::class => ['resetServices'], | ||
]; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Messenger/Tests/EventListener/ResetServicesListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\EventListener; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Event\AbstractWorkerMessageEvent; | ||
use Symfony\Component\Messenger\EventListener\ResetServicesListener; | ||
|
||
class ResetServicesListenerTest extends TestCase | ||
{ | ||
public function provideTests(): iterable | ||
{ | ||
yield ['foo', true]; | ||
yield ['bar', false]; | ||
} | ||
|
||
/** @dataProvider provideTests */ | ||
public function test(string $receiverName, bool $shouldReset) | ||
{ | ||
$servicesResetter = $this->createMock(ServicesResetter::class); | ||
$servicesResetter->expects($shouldReset ? $this->once() : $this->never())->method('reset'); | ||
|
||
$event = new class(new Envelope(new \stdClass()), $receiverName) extends AbstractWorkerMessageEvent {}; | ||
|
||
$memoryLimitListener = new ResetServicesListener($servicesResetter, ['foo']); | ||
$memoryLimitListener->resetServices($event); | ||
} | ||
} |