-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Adding support for record messages #27844
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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\Messenger\Exception; | ||
|
||
/** | ||
* When handling messages, some handlers caused an exception. This exception | ||
* contains all those handler exceptions. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class MessageHandlingException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
private $exceptions = array(); | ||
|
||
public function __construct(array $exceptions) | ||
{ | ||
$message = sprintf( | ||
"Some handlers for recorded messages threw an exception. Their messages were: \n\n%s", | ||
implode(", \n", array_map(function (\Throwable $e) { | ||
return $e->getMessage(); | ||
}, $exceptions)) | ||
); | ||
|
||
$this->exceptions = $exceptions; | ||
parent::__construct($message); | ||
} | ||
|
||
public function getExceptions(): array | ||
{ | ||
return $this->exceptions; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?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\Messenger; | < 8000 /tr>||
|
||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
class MessageRecorder implements MessageRecorderInterface, RecordedMessageCollectionInterface, ResetInterface | ||
{ | ||
private $messages = array(); | ||
chalasr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRecordedMessages(): array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not fan of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I know, this is the "symfony way". Im not sure if I can apply " There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok I understand :( |
||
{ | ||
return $this->messages; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resetRecordedMessages(): void | ||
{ | ||
$this->reset(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reset() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you keep this method? You should remove it. Like I said, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. But I need this service to implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok 👍 |
||
{ | ||
$this->messages = array(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function record($message): void | ||
{ | ||
$this->messages[] = $message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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\Messenger; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
interface MessageRecorderInterface | ||
{ | ||
/** | ||
* Record a message. | ||
* | ||
* @param object $message | ||
*/ | ||
public function record($message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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\Messenger\Middleware; | ||
|
||
use Symfony\Component\Messenger\Exception\MessageHandlingException; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\RecordedMessageCollectionInterface; | ||
|
||
/** | ||
* A middleware that takes all recorded messages and dispatch them to the bus. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
class HandleRecordedMessageMiddleware implements MiddlewareInterface | ||
{ | ||
private $messageRecorder; | ||
private $messageBus; | ||
|
||
public function __construct(MessageBusInterface $messageBus, RecordedMessageCollectionInterface $messageRecorder) | ||
{ | ||
$this->messageRecorder = $messageRecorder; | ||
$this->messageBus = $messageBus; | ||
} | ||
|
||
public function handle($message, callable $next) | ||
{ | ||
// Make sure the recorder is empty before we begin | ||
$this->messageRecorder->resetRecordedMessages(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's imagine the following example:
The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct IF the current bus and I think I should remove this line. Are you okey with that @ogizanagi? It was added by your suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be fine removing it, but the case described above is a more complex topic then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which is equivalent to the |
||
|
||
try { | ||
$returnData = $next($message); | ||
} catch (\Throwable $exception) { | ||
$this->messageRecorder->resetRecordedMessages(); | ||
|
||
throw $exception; | ||
} | ||
|
||
$exceptions = array(); | ||
while (!empty($recordedMessages = $this->messageRecorder->getRecordedMessages())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reasoning of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to make sure nobody runs |
||
$this->messageRecorder->resetRecordedMessages(); | ||
// Assert: The message recorder is empty, all messages are in $recordedMessages | ||
|
||
foreach ($recordedMessages as $recordedMessage) { | ||
try { | ||
$this->messageBus->dispatch($recordedMessage); | ||
} catch (\Throwable $exception) { | ||
$exceptions[] = $exception; | ||
} | ||
} | ||
} | ||
|
||
if (!empty($exceptions)) { | ||
if (1 === \count($exceptions)) { | ||
throw $exceptions[0]; | ||
} | ||
throw new MessageHandlingException($exceptions); | ||
} | ||
|
||
return $returnData; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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\Messenger; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
interface RecordedMessageCollectionInterface | ||
{ | ||
/** | ||
* Fetch recorded messages. | ||
* | ||
* @return object[] | ||
*/ | ||
public function getRecordedMessages(): array; | ||
|
||
/** | ||
* Remove all recorded messages. | ||
*/ | ||
public function resetRecordedMessages(): void; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toArray
would be better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, Im not sure