Skip to content

Commit

Permalink
feature #50155 [Scheduler] add schedule name to MessageContext (kbond)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.3 branch.

Discussion
----------

[Scheduler] add schedule name to `MessageContext`

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a

Alternative to #49864.

Commits
-------

dc0f0e2 [Scheduler] add schedule name to `MessageContext`
  • Loading branch information
fabpot committed Apr 26, 2023
2 parents 1f8c592 + dc0f0e2 commit 32c5fb0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Symfony/Component/Scheduler/Generator/MessageContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
final class MessageContext
{
public function __construct(
public readonly string $name,
public readonly TriggerInterface $trigger,
public readonly \DateTimeImmutable $triggeredAt,
public readonly \DateTimeImmutable|null $nextTriggerAt = null,
public readonly ?\DateTimeImmutable $nextTriggerAt = null,
) {
}
}
10 changes: 4 additions & 6 deletions src/Symfony/Component/Scheduler/Generator/MessageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ final class MessageGenerator implements MessageGeneratorInterface

public function __construct(
private readonly Schedule $schedule,
string|CheckpointInterface $checkpoint,
private readonly string $name,
private readonly ClockInterface $clock = new Clock(),
CheckpointInterface $checkpoint = null,
) {
$this->waitUntil = new \DateTimeImmutable('@0');
if (\is_string($checkpoint)) {
$checkpoint = new Checkpoint('scheduler_checkpoint_'.$checkpoint, $this->schedule->getLock(), $this->schedule->getState());
}
$this->checkpoint = $checkpoint;
$this->checkpoint = $checkpoint ?? new Checkpoint('scheduler_checkpoint_'.$this->name, $this->schedule->getLock(), $this->schedule->getState());
}

public function getMessages(): \Generator
Expand Down Expand Up @@ -70,7 +68,7 @@ public function getMessages(): \Generator
}

if ($yield) {
yield (new MessageContext($trigger, $time, $nextTime)) => $message;
yield (new MessageContext($this->name, $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,6 @@
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
use Symfony\Component\Scheduler\Exception\InvalidArgumentException;
use Symfony\Component\Scheduler\Generator\Checkpoint;
use Symfony\Component\Scheduler\Generator\MessageGenerator;
use Symfony\Component\Scheduler\Schedule;

Expand Down Expand Up @@ -46,9 +45,8 @@ public function createTransport(string $dsn, array $options, SerializerInterface

/** @var Schedule $schedule */
$schedule = $this->scheduleProviders->get($scheduleName)->getSchedule();
$checkpoint = new Checkpoint('scheduler_checkpoint_'.$scheduleName, $schedule->getLock(), $schedule->getState());

return new SchedulerTransport(new MessageGenerator($schedule, $checkpoint, $this->clock));
return new SchedulerTransport(new MessageGenerator($schedule, $scheduleName, $this->clock));
}

public function supports(string $dsn, array $options): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ public function testGetFromIterator()
$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];
yield (new MessageContext('default', $trigger, $triggerAt)) => $messages[0];
yield (new MessageContext('default', $trigger, $triggerAt)) => $messages[1];
});
$transport = new SchedulerTransport($generator);

foreach ($transport->get() as $envelope) {
$this->assertInstanceOf(Envelope::class, $envelope);
$this->assertNotNull($envelope->last(ScheduledStamp::class));
$this->assertNotNull($stamp = $envelope->last(ScheduledStamp::class));
$this->assertSame(array_shift($messages), $envelope->getMessage());
$this->assertSame('default', $stamp->messageContext->name);
}

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

0 comments on commit 32c5fb0

Please sign in to comment.