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

Skip to content

[Scheduler] add schedule name to MessageContext #50155

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 26, 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
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,
Copy link
Contributor

@OskarStark OskarStark Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- CheckpointInterface $checkpoint = null,
+ ?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