Skip to content

Commit c90b629

Browse files
committed
[Scheduler] add PRE_RUN and POST_RUN events
1 parent 541e845 commit c90b629

File tree

15 files changed

+164
-18
lines changed

15 files changed

+164
-18
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner;
5252
use Symfony\Component\Runtime\Runner\Symfony\ResponseRunner;
5353
use Symfony\Component\Runtime\SymfonyRuntime;
54+
use Symfony\Component\Scheduler\Event\SchedulerEvents;
5455
use Symfony\Component\String\LazyString;
5556
use Symfony\Component\String\Slugger\AsciiSlugger;
5657
use Symfony\Component\String\Slugger\SluggerInterface;
@@ -62,6 +63,7 @@
6263
$container->parameters()->set('event_dispatcher.event_aliases', array_merge(
6364
class_exists(ConsoleEvents::class) ? ConsoleEvents::ALIASES : [],
6465
class_exists(FormEvents::class) ? FormEvents::ALIASES : [],
66+
class_exists(SchedulerEvents::class) ? SchedulerEvents::ALIASES : [],
6567
KernelEvents::ALIASES,
6668
class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
6769
));

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Messenger/DummySchedule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Messenger;
44

55
use Symfony\Component\Cache\Adapter\ArrayAdapter;
6+
use Symfony\Component\EventDispatcher\EventDispatcher;
67
use Symfony\Component\Lock\Key;
78
use Symfony\Component\Lock\Lock;
89
use Symfony\Component\Lock\Store\InMemoryStore;
@@ -17,7 +18,7 @@ class DummySchedule implements ScheduleProviderInterface
1718

1819
public function getSchedule(): Schedule
1920
{
20-
return (new Schedule())
21+
return (new Schedule(new EventDispatcher()))
2122
->add(...self::$recurringMessages)
2223
->stateful(new ArrayAdapter())
2324
->lock(new Lock(new Key('dummy'), new InMemoryStore()))

src/Symfony/Component/Messenger/Worker.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
use Symfony\Component\Messenger\Transport\Receiver\QueueReceiverInterface;
3535
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
3636
use Symfony\Component\RateLimiter\LimiterInterface;
37+
use Symfony\Component\Scheduler\Event\PostRunEvent;
38+
use Symfony\Component\Scheduler\Event\PreRunEvent;
39+
use Symfony\Component\Scheduler\Event\SchedulerEvents;
3740

3841
/**
3942
* @author Samuel Roze <samuel.roze@gmail.com>
@@ -99,6 +102,10 @@ public function run(array $options = []): void
99102
if ($queueNames) {
100103
$envelopes = $receiver->getFromQueues($queueNames);
101104
} else {
105+
if (method_exists($receiver, 'getMessageGenerator')) {
106+
$this->eventDispatcher?->dispatch(new PreRunEvent($receiver->getMessageGenerator()), SchedulerEvents::PRE_RUN);
107+
}
108+
102109
$envelopes = $receiver->get();
103110
}
104111

@@ -109,6 +116,10 @@ public function run(array $options = []): void
109116
$this->handleMessage($envelope, $transportName);
110117
$this->eventDispatcher?->dispatch(new WorkerRunningEvent($this, false));
111118

119+
if (method_exists($receiver, 'getMessageGenerator')) {
120+
$this->eventDispatcher?->dispatch(new PostRunEvent($receiver->getMessageGenerator(), $envelope), SchedulerEvents::POST_RUN);
121+
}
122+
112123
if ($this->shouldStop) {
113124
break 2;
114125
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Event;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Scheduler\Generator\MessageGenerator;
16+
17+
class PostRunEvent
18+
{
19+
public function __construct(private readonly MessageGenerator $messageGenerator, private readonly Envelope $envelope)
20+
{
21+
}
22+
23+
public function getMessageGenerator(): MessageGenerator
24+
{
25+
return $this->messageGenerator;
26+
}
27+
28+
public function getEnvelope(): Envelope
29+
{
30+
return $this->envelope;
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Event;
13+
14+
use Symfony\Component\Scheduler\Generator\MessageGenerator;
15+
16+
class PreRunEvent
17+
{
18+
public function __construct(private readonly MessageGenerator $messageGenerator)
19+
{
20+
}
21+
22+
public function getMessageGenerator(): MessageGenerator
23+
{
24+
return $this->messageGenerator;
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Scheduler\Event;
13+
14+
final class SchedulerEvents
15+
{
16+
public const PRE_RUN = 'scheduler.pre_run';
17+
18+
public const POST_RUN = 'scheduler.post_run';
19+
20+
public const ALIASES = [
21+
PreRunEvent::class => self::PRE_RUN,
22+
PostRunEvent::class => self::POST_RUN,
23+
];
24+
25+
private function __construct()
26+
{
27+
}
28+
}

src/Symfony/Component/Scheduler/Generator/MessageGenerator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public function getMessages(): \Generator
9393
$checkpoint->release($now, $this->waitUntil);
9494
}
9595

96+
public function schedule(): Schedule
97+
{
98+
return $this->schedule ??= $this->scheduleProvider->getSchedule();
99+
}
100+
96101
private function heap(\DateTimeImmutable $time, \DateTimeImmutable $startTime): TriggerHeap
97102
{
98103
if (isset($this->triggerHeap) && $this->triggerHeap->time <= $time) {
@@ -118,11 +123,6 @@ private function heap(\DateTimeImmutable $time, \DateTimeImmutable $startTime):
118123
return $this->triggerHeap = $heap;
119124
}
120125

121-
private function schedule(): Schedule
122-
{
123-
return $this->schedule ??= $this->scheduleProvider->getSchedule();
124-
}
125-
126126
private function checkpoint(): Checkpoint
127127
{
128128
return $this->checkpoint ??= new Checkpoint('scheduler_checkpoint_'.$this->name, $this->schedule()->getLock(), $this->schedule()->getState());

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,9 @@ public function send(Envelope $envelope): Envelope
5454
{
5555
throw new LogicException(sprintf('"%s" cannot send messages.', __CLASS__));
5656
}
57+
58+
public function getMessageGenerator(): MessageGeneratorInterface
59+
{
60+
return $this->messageGenerator;
61+
}
5762
}

src/Symfony/Component/Scheduler/Schedule.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,27 @@
1111

1212
namespace Symfony\Component\Scheduler;
1313

14+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1415
use Symfony\Component\Lock\LockInterface;
16+
use Symfony\Component\Scheduler\Event\SchedulerEvents;
1517
use Symfony\Component\Scheduler\Exception\LogicException;
1618
use Symfony\Contracts\Cache\CacheInterface;
1719

1820
final class Schedule implements ScheduleProviderInterface
1921
{
22+
public function __construct(private readonly EventDispatcherInterface $dispatcher)
23+
{
24+
}
25+
2026
/** @var array<string,RecurringMessage> */
2127
private array $messages = [];
2228
private ?LockInterface $lock = null;
2329
private ?CacheInterface $state = null;
2430
private bool $shouldRestart = false;
2531

26-
public static function with(RecurringMessage $message, RecurringMessage ...$messages): static
32+
public function with(RecurringMessage $message, RecurringMessage ...$messages): static
2733
{
28-
return static::doAdd(new self(), $message, ...$messages);
34+
return static::doAdd(new self($this->dispatcher), $message, ...$messages);
2935
}
3036

3137
/**
@@ -119,6 +125,20 @@ public function getSchedule(): static
119125
return $this;
120126
}
121127

128+
public function before(callable $listener, int $priority = 0): static
129+
{
130+
$this->dispatcher->addListener(SchedulerEvents::PRE_RUN, $listener, $priority);
131+
132+
return $this;
133+
}
134+
135+
public function after(callable $listener, int $priority = 0): static
136+
{
137+
$this->dispatcher->addListener(SchedulerEvents::POST_RUN, $listener, $priority);
138+
139+
return $this;
140+
}
141+
122142
public function shouldRestart(): bool
123143
{
124144
return $this->shouldRestart;

src/Symfony/Component/Scheduler/Tests/Command/DebugCommandTest.php

Lines changed: 4 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\Console\Tester\CommandTester;
16+
use Symfony\Component\EventDispatcher\EventDispatcher;
1617
use Symfony\Component\Scheduler\Command\DebugCommand;
1718
use Symfony\Component\Scheduler\RecurringMessage;
1819
use Symfony\Component\Scheduler\Schedule;
@@ -44,7 +45,7 @@ public function testExecuteWithoutSchedules()
4445

4546
public function testExecuteWithScheduleWithoutTriggerDoesNotDisplayMessage()
4647
{
47-
$schedule = new Schedule();
48+
$schedule = new Schedule(new EventDispatcher());
4849
$schedule->add(RecurringMessage::trigger(new CallbackTrigger(fn () => null, 'test'), new \stdClass()));
4950

5051
$schedules = $this->createMock(ServiceProviderInterface::class);
@@ -79,7 +80,7 @@ public function testExecuteWithScheduleWithoutTriggerDoesNotDisplayMessage()
7980

8081
public function testExecuteWithScheduleWithoutTriggerShowingNoNextRunWithAllOption()
8182
{
82-
$schedule = new Schedule();
83+
$schedule = new Schedule(new EventDispatcher());
8384
$schedule->add(RecurringMessage::trigger(new CallbackTrigger(fn () => null, 'test'), new \stdClass()));
8485

8586
$schedules = $this->createMock(ServiceProviderInterface::class);
@@ -116,7 +117,7 @@ public function testExecuteWithScheduleWithoutTriggerShowingNoNextRunWithAllOpti
116117

117118
public function testExecuteWithSchedule()
118119
{
119-
$schedule = new Schedule();
120+
$schedule = new Schedule(new EventDispatcher());
120121
$schedule->add(RecurringMessage::every('first day of next month', new \stdClass()));
121122

122123
$schedules = $this->createMock(ServiceProviderInterface::class);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy