-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature][experimental] add MessageTask to schedule messenger messages (
#28)
- Loading branch information
Showing
16 changed files
with
563 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/composer.lock | ||
/phpunit.xml | ||
/vendor/ | ||
/build/ | ||
/.php_cs.cache | ||
/.phpunit.result.cache |
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
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,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="Zenstruck\ScheduleBundle\Schedule\Task\Runner\MessageTaskRunner"> | ||
<tag name="schedule.task_runner" /> | ||
</service> | ||
</services> | ||
</container> |
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,77 @@ | ||
<?php | ||
|
||
namespace Zenstruck\ScheduleBundle\Schedule\Task; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
use Zenstruck\ScheduleBundle\Schedule\HasMissingDependencyMessage; | ||
use Zenstruck\ScheduleBundle\Schedule\Task; | ||
|
||
/** | ||
* @experimental This is experimental and may experience BC breaks | ||
* | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class MessageTask extends Task implements HasMissingDependencyMessage | ||
{ | ||
private $message; | ||
private $stamps; | ||
|
||
/** | ||
* @param object|Envelope $message | ||
* @param StampInterface[] $stamps | ||
*/ | ||
public function __construct(object $message, array $stamps = []) | ||
{ | ||
$this->message = $message; | ||
$this->stamps = $stamps; | ||
|
||
parent::__construct($this->messageClass()); | ||
} | ||
|
||
/** | ||
* @return object|Envelope | ||
*/ | ||
public function getMessage(): object | ||
{ | ||
return $this->message; | ||
} | ||
|
||
/** | ||
* @return StampInterface[] | ||
*/ | ||
public function getStamps(): array | ||
{ | ||
return $this->stamps; | ||
} | ||
|
||
public function getContext(): array | ||
{ | ||
$stamps = \array_merge( | ||
$this->message instanceof Envelope ? \array_keys($this->message->all()) : [], | ||
\array_map(static function(StampInterface $stamp) { return \get_class($stamp); }, $this->stamps) | ||
); | ||
$stamps = \array_map( | ||
static function($stamp) { | ||
return (new \ReflectionClass($stamp))->getShortName(); | ||
}, | ||
$stamps | ||
); | ||
$stamps = \implode(', ', \array_unique($stamps)); | ||
|
||
return [ | ||
'Message' => $this->messageClass(), | ||
'Stamps' => $stamps ?: '(none)', | ||
]; | ||
} | ||
|
||
public static function getMissingDependencyMessage(): string | ||
{ | ||
return 'To use the message task you must install symfony/messenger (composer require symfony/messenger) and enable (config path: "zenstruck_schedule.messenger").'; | ||
} | ||
|
||
private function messageClass(): string | ||
{ | ||
return $this->message instanceof Envelope ? \get_class($this->message->getMessage()) : \get_class($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,79 @@ | ||
<?php | ||
|
||
namespace Zenstruck\ScheduleBundle\Schedule\Task\Runner; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
use Symfony\Component\Messenger\Stamp\SentStamp; | ||
use Zenstruck\ScheduleBundle\Schedule\Task; | ||
use Zenstruck\ScheduleBundle\Schedule\Task\MessageTask; | ||
use Zenstruck\ScheduleBundle\Schedule\Task\Result; | ||
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunner; | ||
|
||
/** | ||
* @experimental This is experimental and may experience BC breaks | ||
* | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class MessageTaskRunner implements TaskRunner | ||
{ | ||
private $bus; | ||
|
||
public function __construct(MessageBusInterface $bus) | ||
{ | ||
$this->bus = $bus; | ||
} | ||
|
||
/** | ||
* @param MessageTask|Task $task | ||
*/ | ||
public function __invoke(Task $task): Result | ||
{ | ||
$envelope = $this->bus->dispatch($task->getMessage(), $task->getStamps()); | ||
$output = $this->handlerOutput($envelope); | ||
|
||
if (empty($output)) { | ||
return Result::failure($task, 'Message not handled or sent to transport.'); | ||
} | ||
|
||
return Result::successful($task, \implode("\n", $output)); | ||
} | ||
|
||
public function supports(Task $task): bool | ||
{ | ||
return $task instanceof MessageTask; | ||
} | ||
|
||
private function handlerOutput(Envelope $envelope): array | ||
{ | ||
$output = []; | ||
|
||
foreach ($envelope->all(HandledStamp::class) as $stamp) { | ||
/* @var HandledStamp $stamp */ | ||
$output[] = \sprintf('Handled by: "%s", return: %s', $stamp->getHandlerName(), $this->handledStampReturn($stamp)); | ||
} | ||
|
||
foreach ($envelope->all(SentStamp::class) as $stamp) { | ||
/* @var SentStamp $stamp */ | ||
$output[] = \sprintf('Sent to: "%s"', $stamp->getSenderClass()); | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
private function handledStampReturn(HandledStamp $stamp): string | ||
{ | ||
$result = $stamp->getResult(); | ||
|
||
switch (true) { | ||
case null === $result: | ||
return '(none)'; | ||
|
||
case \is_scalar($result): | ||
return \sprintf('(%s) "%s"', get_debug_type($result), $result); | ||
} | ||
|
||
return \sprintf('(%s)', get_debug_type($result)); | ||
} | ||
} |
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.