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

Skip to content

[Scheduler] Make MessageGenerator yield some scheduling context #50130

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

Merged
merged 1 commit into from
Apr 24, 2023
Merged
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
29 changes: 29 additions & 0 deletions src/Symfony/Component/Scheduler/Generator/MessageContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Scheduler\Generator;

use Symfony\Component\Scheduler\Trigger\TriggerInterface;

/**
* @author Tugdual Saunier <[email protected]>
*
* @experimental
*/
final class MessageContext
{
public function __construct(
public readonly TriggerInterface $trigger,
public readonly \DateTimeImmutable $triggeredAt,
public readonly \DateTimeImmutable|null $nextTriggerAt = null,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getMessages(): \Generator
}

if ($yield) {
yield $message;
yield (new MessageContext($trigger, $time, $nextTime)) => $message;
$this->checkpoint->save($time, $index);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
interface MessageGeneratorInterface
{
/**
* @return iterable<object>
* @return iterable<MessageContext, object>
*/
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\Generator\MessageContext;

/**
* @experimental
*/
final class ScheduledStamp implements NonSendableStampInterface
{
public function __construct(public readonly MessageContext $messageContext)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function __construct(

public function get(): iterable
{
foreach ($this->messageGenerator->getMessages() as $message) {
yield Envelope::wrap($message, [new ScheduledStamp()]);
foreach ($this->messageGenerator->getMessages() as $context => $message) {
yield Envelope::wrap($message, [new ScheduledStamp($context)]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Scheduler\Generator\MessageContext;
use Symfony\Component\Scheduler\Generator\MessageGenerator;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
Expand Down Expand Up @@ -43,14 +44,47 @@ public function testGetMessages(string $startTime, array $runs, array $schedule)
$scheduler = new MessageGenerator($schedule, 'dummy', $clock);

// Warmup. The first run is always returns nothing.
$this->assertSame([], iterator_to_array($scheduler->getMessages()));
$this->assertSame([], iterator_to_array($scheduler->getMessages(), false));

foreach ($runs as $time => $expected) {
$now = self::makeDateTime($time);
$this->assertSame($expected, iterator_to_array($scheduler->getMessages()));
$this->assertSame($expected, iterator_to_array($scheduler->getMessages(), false));
}
}

public function testYieldedContext()
{
// for referencing
$now = self::makeDateTime('22:12:00');

$clock = $this->createMock(ClockInterface::class);
$clock->method('now')->willReturnReference($now);

$message = $this->createMessage((object) ['id' => 'message'], '22:13:00', '22:14:00', '22:16:00');
$schedule = (new Schedule())->add($message);
$schedule->stateful(new ArrayAdapter());

$scheduler = new MessageGenerator($schedule, 'dummy', $clock);

// Warmup. The first run is alw ays returns nothing.
$this->assertSame([], iterator_to_array($scheduler->getMessages(), false));

$now = self::makeDateTime('22:14:10');

$iterator = $scheduler->getMessages();

$this->assertInstanceOf(MessageContext::class, $context = $iterator->key());
$this->assertSame($message->getTrigger(), $context->trigger);
$this->assertEquals(self::makeDateTime('22:13:00'), $context->triggeredAt);
$this->assertEquals(self::makeDateTime('22:14:00'), $context->nextTriggerAt);

$iterator->next();
$this->assertInstanceOf(MessageContext::class, $context = $iterator->key());
$this->assertSame($message->getTrigger(), $context->trigger);
$this->assertEquals(self::makeDateTime('22:14:00'), $context->triggeredAt);
$this->assertEquals(self::makeDateTime('22:16:00'), $context->nextTriggerAt);
}

public static function messagesProvider(): \Generator
{
$first = (object) ['id' => 'first'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Scheduler\Exception\LogicException;
use Symfony\Component\Scheduler\Generator\MessageContext;
use Symfony\Component\Scheduler\Generator\MessageGeneratorInterface;
use Symfony\Component\Scheduler\Messenger\ScheduledStamp;
use Symfony\Component\Scheduler\Messenger\SchedulerTransport;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

class SchedulerTransportTest extends TestCase
{
Expand All @@ -26,9 +28,15 @@ public function testGetFromIterator()
(object) ['id' => 'first'],
(object) ['id' => 'second'],
];
$generator = $this->createConfiguredMock(MessageGeneratorInterface::class, [
$generator = $this->createMock(MessageGeneratorInterface::class, [
'getMessages' => $messages,
]);
$generator->method('getMessages')->willReturnCallback(function () use ($messages): \Generator {
$trigger = $this->createMock(TriggerInterface::class);
$triggerAt = new \DateTimeImmutable('2020-02-20T02:00:00', new \DateTimeZone('UTC'));
yield (new MessageContext($trigger, $triggerAt)) => $messages[0];
yield (new MessageContext($trigger, $triggerAt)) => $messages[1];
});
$transport = new SchedulerTransport($generator);

foreach ($transport->get() as $envelope) {
Expand Down