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

Skip to content

[Scheduler] Add MessageHandler result to the PostRunEvent #58546

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
Feb 7, 2025
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 @@ -13,6 +13,7 @@

use Symfony\Component\Messenger\Message\RedispatchMessage;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;

final class RedispatchMessageHandler
Expand All @@ -22,8 +23,10 @@ public function __construct(
) {
}

public function __invoke(RedispatchMessage $message): void
public function __invoke(RedispatchMessage $message): mixed
{
$this->bus->dispatch($message->envelope, [new TransportNamesStamp($message->transportNames)]);
$envelope = $this->bus->dispatch($message->envelope, [new TransportNamesStamp($message->transportNames)]);

return $envelope->last(HandledStamp::class)?->getResult();
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add capability to skip missed periodic tasks, only the last schedule will be called
* Add MessageHandler returned result to `PostRunEvent`

6.4
---
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Scheduler/Event/PostRunEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(
private readonly ScheduleProviderInterface $schedule,
private readonly MessageContext $messageContext,
private readonly object $message,
private readonly mixed $result,
) {
}

Expand All @@ -37,4 +38,9 @@ public function getMessage(): object
{
return $this->message;
}

public function getResult(): mixed
{
return $this->result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Scheduler\Event\FailureEvent;
use Symfony\Component\Scheduler\Event\PostRunEvent;
Expand All @@ -40,7 +41,9 @@ public function onMessageHandled(WorkerMessageHandledEvent $event): void
return;
}

$this->eventDispatcher->dispatch(new PostRunEvent($this->scheduleProviderLocator->get($scheduledStamp->messageContext->name), $scheduledStamp->messageContext, $envelope->getMessage()));
$result = $envelope->last(HandledStamp::class)?->getResult();

$this->eventDispatcher->dispatch(new PostRunEvent($this->scheduleProviderLocator->get($scheduledStamp->messageContext->name), $scheduledStamp->messageContext, $envelope->getMessage(), $result));
}

public function onMessageReceived(WorkerMessageReceivedEvent $event): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function __construct(private readonly ContainerInterface $serviceLocator)
{
}

public function __invoke(ServiceCallMessage $message): void
public function __invoke(ServiceCallMessage $message): mixed
{
$this->serviceLocator->get($message->getServiceId())->{$message->getMethod()}(...$message->getArguments());
return $this->serviceLocator->get($message->getServiceId())->{$message->getMethod()}(...$message->getArguments());
}
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Scheduler/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public function run(array $options = []): void
}

try {
$this->handlers[$message::class]($message);
$result = $this->handlers[$message::class]($message);
$ran = true;

$this->dispatcher->dispatch(new PostRunEvent($generator->getSchedule(), $context, $message));
$this->dispatcher->dispatch(new PostRunEvent($generator->getSchedule(), $context, $message, $result));
} catch (\Throwable $error) {
$failureEvent = new FailureEvent($generator->getSchedule(), $context, $message, $error);
$this->dispatcher->dispatch($failureEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Scheduler\Event\FailureEvent;
use Symfony\Component\Scheduler\Event\PostRunEvent;
use Symfony\Component\Scheduler\Event\PreRunEvent;
Expand All @@ -44,8 +45,8 @@ public function testDispatchSchedulerEvents()

$listener = new DispatchSchedulerEventListener($scheduleProviderLocator, $eventDispatcher = new EventDispatcher());
$workerReceivedEvent = new WorkerMessageReceivedEvent($envelope, 'default');
$workerHandledEvent = new WorkerMessageHandledEvent($envelope, 'default');
$workerFailedEvent = new WorkerMessageFailedEvent($envelope, 'default', new \Exception());
$workerHandledEvent = new WorkerMessageHandledEvent($envelope->with(new HandledStamp('result', 'handlerName')), 'default');
$workerFailedEvent = new WorkerMessageFailedEvent($envelope, 'default', new \Exception('failed'));
$secondListener = new TestEventListener();

$eventDispatcher->addListener(PreRunEvent::class, [$secondListener, 'preRun']);
Expand All @@ -55,33 +56,34 @@ public function testDispatchSchedulerEvents()
$listener->onMessageHandled($workerHandledEvent);
$listener->onMessageFailed($workerFailedEvent);

$this->assertTrue($secondListener->preInvoked);
$this->assertTrue($secondListener->postInvoked);
$this->assertTrue($secondListener->failureInvoked);
$this->assertInstanceOf(PreRunEvent::class, $secondListener->preRunEvent);
$this->assertSame('result', $secondListener->postRunEvent->getResult());
$this->assertInstanceOf(PostRunEvent::class, $secondListener->postRunEvent);
$this->assertInstanceOf(FailureEvent::class, $secondListener->failureEvent);
$this->assertEquals(new \Exception('failed'), $secondListener->failureEvent->getError());
}
}

class TestEventListener
{
public string $name;
public bool $preInvoked = false;
public bool $postInvoked = false;
public bool $failureInvoked = false;
public ?PreRunEvent $preRunEvent = null;
public ?PostRunEvent $postRunEvent = null;
public ?FailureEvent $failureEvent = null;

/* Listener methods */

public function preRun($e)
{
$this->preInvoked = true;
$this->preRunEvent = $e;
}

public function postRun($e)
{
$this->postInvoked = true;
$this->postRunEvent = $e;
}

public function onFailure($e)
{
$this->failureInvoked = true;
$this->failureEvent = $e;
}
}
Loading