8000 feature #51230 [Scheduler] add `ScheduledStamp` to `RedispatchMessage… · symfony/symfony@6a36e02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a36e02

Browse files
committed
feature #51230 [Scheduler] add ScheduledStamp to RedispatchMessage (kbond)
This PR was merged into the 6.4 branch. Discussion ---------- [Scheduler] add `ScheduledStamp` to `RedispatchMessage` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #51205 | License | MIT | Doc PR | n/a This solves my specific issue in #51205. Now, we can track messages that were generated by the scheduler even if they are _re-dispatched_. Commits ------- f38eaad [Scheduler] add `ScheduledStamp` to `RedispatchMessage`
2 parents 7a8959b + f38eaad commit 6a36e02

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/Symfony/Component/Scheduler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Allow setting timezone of next run date in CronExpressionTrigger
88
* Add `AbstractTriggerDecorator`
99
* Make `ScheduledStamp` "send-able"
10+
* Add `ScheduledStamp` to `RedispatchMessage`
1011

1112
6.3
1213
---

src/Symfony/Component/Scheduler/Messenger/SchedulerTransport.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Scheduler\Messenger;
1313

1414
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\Message\RedispatchMessage;
1516
use Symfony\Component\Messenger\Transport\TransportInterface;
1617
use Symfony\Component\Scheduler\Exception\LogicException;
1718
use Symfony\Component\Scheduler\Generator\MessageGeneratorInterface;
@@ -29,7 +30,16 @@ public function __construct(
2930
public function get(): iterable
3031
{
3132
foreach ($this->messageGenerator->getMessages() as $context => $message) {
32-
yield Envelope::wrap($message, [new ScheduledStamp($context)]);
33+
$stamp = new ScheduledStamp($context);
34+
35+
if ($message instanceof RedispatchMessage) {
36+
$message = new RedispatchMessage(
37+
Envelope::wrap($message->envelope, [$stamp]),
38+
$message->transportNames,
39+
);
40+
}
41+
42+
yield Envelope::wrap($message, [$stamp]);
3343
}
3444
}
3545

src/Symfony/Component/Scheduler/Tests/Messenger/SchedulerTransportTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Envelope;
16+
use Symfony\Component\Messenger\Message\RedispatchMessage;
1617
use Symfony\Component\Scheduler\Exception\LogicException;
1718
use Symfony\Component\Scheduler\Generator\MessageContext;
1819
use Symfony\Component\Scheduler\Generator\MessageGeneratorInterface;
@@ -28,9 +29,7 @@ public function testGetFromIterator()
2829
(object) ['id' => 'first'],
2930
(object) ['id' => 'second'],
3031
];
31-
$generator = $this->createMock(MessageGeneratorInterface::class, [
32-
'getMessages' => $messages,
33-
]);
32+
$generator = $this->createMock(MessageGeneratorInterface::class);
3433
$generator->method('getMessages')->willReturnCallback(function () use ($messages): \Generator {
3534
$trigger = $this->createMock(TriggerInterface::class);
3635
$triggerAt = new \DateTimeImmutable('2020-02-20T02:00:00', new \DateTimeZone('UTC'));
@@ -50,6 +49,22 @@ public function testGetFromIterator()
5049
$this->assertEmpty($messages);
5150
}
5251

52+
public function testAddsStampToInnerRedispatchMessageEnvelope()
53+
{
54+
$generator = $this->createMock(MessageGeneratorInterface::class);
55+
$generator->method('getMessages')->willReturnCallback(function (): \Generator {
56+
yield new MessageContext('default', 'id', $this->createMock(TriggerInterface::class), new \DateTimeImmutable()) =>
57+
new RedispatchMessage(new \stdClass(), ['transport']);
58+
});
59+
$envelopes = \iterator_to_array((new SchedulerTransport($generator))->get());
60+
61+
$stamp = $envelopes[0]->getMessage()->envelope->last(ScheduledStamp::class);
62+
63+
$this->assertSame($stamp, $envelopes[0]->last(ScheduledStamp::class));
64+
$this->assertSame('default', $stamp->messageContext->name);
65+
$this->assertSame('id', $stamp->messageContext->id);
66+
}
67+
5368
public function testAckIgnored()
5469
{
5570
$transport = new SchedulerTransport($this->createMock(MessageGeneratorInterface::class));

0 commit comments

Comments
 (0)
0