-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #51805 [Scheduler] pre_run and post_run events (alli83)
This PR was merged into the 6.4 branch. Discussion ---------- [Scheduler] pre_run and post_run events | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | #49803 (comment) | License | MIT Based on #49803 `@kbond` and taking into account #51553 The aim of this PR is to be able to act on the accumulated messages 'if something happens' and to be able to recalculate the heap via events (currently pre_run and post_run). The aim is to have access to - the the schedule and therefore add/cancel a certain type of message. - MessageContexte (e.g. access the id) - The message itself This PR would complement `@Jeroeny` #51553 PR by enabling action via events. Commits ------- 20fd21a [Scheduler] add PRE_RUN and POST_RUN events
- Loading branch information
Showing
12 changed files
with
319 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Scheduler\Event; | ||
|
||
use Symfony\Component\Scheduler\Generator\MessageContext; | ||
use Symfony\Component\Scheduler\ScheduleProviderInterface; | ||
|
||
class PostRunEvent | ||
{ | ||
public function __construct( | ||
private readonly ScheduleProviderInterface $schedule, | ||
private readonly MessageContext $messageContext, | ||
private readonly object $message, | ||
) { | ||
} | ||
|
||
public function getMessageContext(): MessageContext | ||
{ | ||
return $this->messageContext; | ||
} | ||
|
||
public function getSchedule(): ScheduleProviderInterface | ||
{ | ||
return $this->schedule; | ||
} | ||
|
||
public function getMessage(): object | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Scheduler\Event; | ||
|
||
use Symfony\Component\Scheduler\Generator\MessageContext; | ||
use Symfony\Component\Scheduler\ScheduleProviderInterface; | ||
|
||
class PreRunEvent | ||
{ | ||
private bool $shouldCancel = false; | ||
|
||
public function __construct( | ||
private readonly ScheduleProviderInterface $schedule, | ||
private readonly MessageContext $messageContext, | ||
private readonly object $message, | ||
) { | ||
} | ||
|
||
public function getMessageContext(): MessageContext | ||
{ | ||
return $this->messageContext; | ||
} | ||
|
||
public function getSchedule(): ScheduleProviderInterface | ||
{ | ||
return $this->schedule; | ||
} | ||
|
||
public function getMessage(): object | ||
{ | ||
return $this->message; | ||
} | ||
|
||
public function shouldCancel(bool $shouldCancel = null): bool | ||
{ | ||
if (null !== $shouldCancel) { | ||
$this->shouldCancel = $shouldCancel; | ||
} | ||
|
||
return $this->shouldCancel; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/Symfony/Component/Scheduler/EventListener/DispatchSchedulerEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Scheduler\EventListener; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; | ||
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; | ||
use Symfony\Component\Scheduler\Event\PostRunEvent; | ||
use Symfony\Component\Scheduler\Event\PreRunEvent; | ||
use Symfony\Component\Scheduler\Messenger\ScheduledStamp; | ||
|
||
class DispatchSchedulerEventListener implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private readonly ContainerInterface $scheduleProviderLocator, | ||
private readonly EventDispatcherInterface $eventDispatcher, | ||
) { | ||
} | ||
|
||
public function onMessageHandled(WorkerMessageHandledEvent $event): void | ||
{ | ||
$envelope = $event->getEnvelope(); | ||
if (!$scheduledStamp = $envelope->last(ScheduledStamp::class)) { | ||
return; | ||
} | ||
|
||
if (!$this->scheduleProviderLocator->has($scheduledStamp->messageContext->name)) { | ||
return; | ||
} | ||
|
||
$this->eventDispatcher->dispatch(new PostRunEvent($this->scheduleProviderLocator->get($scheduledStamp->messageContext->name), $scheduledStamp->messageContext, $envelope->getMessage())); | ||
} | ||
|
||
public function onMessageReceived(WorkerMessageReceivedEvent $event): void | ||
{ | ||
$envelope = $event->getEnvelope(); | ||
|
||
if (!$scheduledStamp = $envelope->last(ScheduledStamp::class)) { | ||
return; | ||
} | ||
|
||
if (!$this->scheduleProviderLocator->has($scheduledStamp->messageContext->name)) { | ||
return; | ||
} | ||
|
||
$preRunEvent = new PreRunEvent($this->scheduleProviderLocator->get($scheduledStamp->messageContext->name), $scheduledStamp->messageContext, $envelope->getMessage()); | ||
|
||
$this->eventDispatcher->dispatch($preRunEvent); | ||
|
||
if ($preRunEvent->shouldCancel()) { | ||
$event->shouldHandle(false); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
WorkerMessageReceivedEvent::class => ['onMessageReceived'], | ||
WorkerMessageHandledEvent::class => ['onMessageHandled'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.