Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 113fa0b

Browse files
committed
bug #33298 [Messenger] Stop worker when it should stop (tienvx)
This PR was merged into the 4.3 branch. Discussion ---------- [Messenger] Stop worker when it should stop | Q | A | ------------- | --- | Branch? | 4.3 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | NA <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | NA <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/roadmap): - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against branch 4.4. - Legacy code removals go to the master branch. --> There are 2 things about this PR: * This PR fix the bug when using `limit`, `memory-limit`, `time-limit` options with command `messenger:consume`, these options does not work if the receiver return multiple messages * This PR is the continue work of #32783 Commits ------- 5c1f3a2 [Messenger] Stop worker when it should stop
2 parents 861b483 + 5c1f3a2 commit 113fa0b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Symfony/Component/Messenger/Tests/WorkerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2828
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
2929
use Symfony\Component\Messenger\Worker;
30+
use Symfony\Component\Messenger\Worker\StopWhenMessageCountIsExceededWorker;
3031
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
3132

3233
/**
@@ -361,6 +362,30 @@ public function testWorkerWithMultipleReceivers()
361362
// make sure they were processed in the correct order
362363
$this->assertSame([$envelope1, $envelope2, $envelope3, $envelope4, $envelope5, $envelope6], $processedEnvelopes);
363364
}
365+
366+
public function testWorkerWithDecorator()
367+
{
368+
$envelope1 = new Envelope(new DummyMessage('message1'));
369+
$envelope2 = new Envelope(new DummyMessage('message2'));
370+
$envelope3 = new Envelope(new DummyMessage('message3'));
371+
372+
$receiver = new DummyReceiver([
373+
[$envelope1, $envelope2, $envelope3],
374+
]);
375+
376+
$bus = $this->getMockBuilder(MessageBusInterface::class)->getMock();
377+
378+
$worker = new Worker([$receiver], $bus);
379+
$workerWithDecorator = new StopWhenMessageCountIsExceededWorker($worker, 2);
380+
$processedEnvelopes = [];
381+
$workerWithDecorator->run([], function (?Envelope $envelope) use ($worker, &$processedEnvelopes) {
382+
if (null !== $envelope) {
383+
$processedEnvelopes[] = $envelope;
384+
}
385+
});
386+
387+
$this->assertSame([$envelope1, $envelope2], $processedEnvelopes);
388+
}
364389
}
365390

366391
class DummyReceiver implements ReceiverInterface

src/Symfony/Component/Messenger/Worker.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ public function run(array $options = [], callable $onHandledCallback = null): vo
9292

9393
$this->handleMessage($envelope, $receiver, $transportName, $this->retryStrategies[$transportName] ?? null);
9494
$onHandled($envelope);
95+
96+
if ($this->shouldStop) {
97+
break 2;
98+
}
9599
}
96100

97101
// after handling a single receiver, quit and start the loop again

0 commit comments

Comments
 (0)