Skip to content

[Scheduler] allow MessageGenerator's to return RecurringMessage[] #49865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Symfony/Component/Scheduler/Generator/MessageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Psr\Clock\ClockInterface;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

Expand All @@ -37,6 +38,9 @@ public function __construct(
$this->checkpoint = $checkpoint;
}

/**
* @return \Generator<RecurringMessage>
*/
public function getMessages(): \Generator
{
if (!$this->waitUntil
Expand All @@ -54,8 +58,8 @@ public function getMessages(): \Generator
/** @var TriggerInterface $trigger */
/** @var int $index */
/** @var \DateTimeImmutable $time */
/** @var object $message */
[$time, $index, $trigger, $message] = $heap->extract();
/** @var object $recurringMessage */
[$time, $index, $trigger, $recurringMessage] = $heap->extract();
$yield = true;

if ($time < $lastTime) {
Expand All @@ -66,11 +70,11 @@ public function getMessages(): \Generator
}

if ($nextTime = $trigger->getNextRunDate($time)) {
$heap->insert([$nextTime, $index, $trigger, $message]);
$heap->insert([$nextTime, $index, $trigger, $recurringMessage]);
}

if ($yield) {
yield $message;
yield $recurringMessage;
$this->checkpoint->save($time, $index);
}
}
Expand All @@ -93,7 +97,7 @@ private function heap(\DateTimeImmutable $time): TriggerHeap
continue;
}

$heap->insert([$nextTime, $index, $recurringMessage->getTrigger(), $recurringMessage->getMessage()]);
$heap->insert([$nextTime, $index, $recurringMessage->getTrigger(), $recurringMessage]);
}

return $this->triggerHeap = $heap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

namespace Symfony\Component\Scheduler\Generator;

use Symfony\Component\Scheduler\RecurringMessage;

/**
* @experimental
*/
interface MessageGeneratorInterface
{
/**
* @return iterable<object>
* @return iterable<object|RecurringMessage>
*/
public function getMessages(): iterable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
namespace Symfony\Component\Scheduler\Messenger;

use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
use Symfony\Component\Scheduler\RecurringMessage;

/**
* @experimental
*/
final class ScheduledStamp implements NonSendableStampInterface
{
public function __construct(public readonly RecurringMessage $recurringMessage)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Component\Scheduler\Exception\LogicException;
use Symfony\Component\Scheduler\Generator\MessageGeneratorInterface;
use Symfony\Component\Scheduler\RecurringMessage;

/**
* @experimental
Expand All @@ -29,7 +30,11 @@ public function __construct(
public function get(): iterable
{
foreach ($this->messageGenerator->getMessages() as $message) {
yield Envelope::wrap($message, [new ScheduledStamp()]);
if (!$message instanceof RecurringMessage) {
throw new LogicException(sprintf('Messages from "%s" must be instances of "%s". Got "%s".', __CLASS__, RecurringMessage::class, get_debug_type($message)));
}

yield Envelope::wrap($message->getMessage(), [new ScheduledStamp($message)]);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Scheduler/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public function run(array $options = []): void
$ran = false;
foreach ($this->generators as $generator) {
foreach ($generator->getMessages() as $message) {
if ($message instanceof RecurringMessage) {
$message = $message->getMessage();
}

$this->handlers[$message::class]($message);
$ran = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function testGetMessages(string $startTime, array $runs, array $schedule)

foreach ($runs as $time => $expected) {
$now = self::makeDateTime($time);
$this->assertSame($expected, iterator_to_array($scheduler->getMessages()));
$messages = array_map(fn (RecurringMessage $m) => $m->getMessage(), iterator_to_array($scheduler->getMessages()));
$this->assertSame($expected, $messages);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,28 @@
use Symfony\Component\Scheduler\Generator\MessageGeneratorInterface;
use Symfony\Component\Scheduler\Messenger\ScheduledStamp;
use Symfony\Component\Scheduler\Messenger\SchedulerTransport;
use Symfony\Component\Scheduler\RecurringMessage;

class SchedulerTransportTest extends TestCase
{
public function testGetFromIterator()
{
$messages = [
(object) ['id' => 'first'],
(object) ['id' => 'second'],
RecurringMessage::cron('* * * * *', (object) ['id' => 'first']),
RecurringMessage::cron('* * * * *', (object) ['id' => 'second']),
];
$generator = $this->createConfiguredMock(MessageGeneratorInterface::class, [
'getMessages' => $messages,
]);
$transport = new SchedulerTransport($generator);

foreach ($transport->get() as $envelope) {
$message = array_shift($messages);

$this->assertInstanceOf(Envelope::class, $envelope);
$this->assertNotNull($envelope->last(ScheduledStamp::class));
$this->assertSame(array_shift($messages), $envelope->getMessage());
$this->assertEquals($message, $envelope->last(ScheduledStamp::class)?->recurringMessage);
$this->assertSame($message->getMessage(), $envelope->getMessage());
$this->assertSame($message->getMessage(), $envelope->getMessage());
}

$this->assertEmpty($messages);
Expand Down