8000 [Messenger] document reset_on_message transport option · symfony/symfony-docs@487ef2c · GitHub
[go: up one dir, main page]

Skip to content

Commit 487ef2c

Browse files
committed
[Messenger] document reset_on_message transport option
1 parent 35f06c3 commit 487ef2c

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

messenger.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,68 @@ of the desired grace period in seconds) in order to perform a graceful shutdown:
696696
[program:x]
697697
stopwaitsecs=20
698698
699+
700+
Stateless worker
701+
~~~~~~~~~~~~~~~~
702+
703+
PHP was designed to be stateless: everything is lost after processing an HTTP
704+
request. When you run your application in a HTTP context, you don't have to
705+
clean your services since PHP clean everything after sending the request.
706+
707+
Since worker run in a CLI context, you need to be careful about services state.
708+
You should avoid to put a state in a service to avoid leaking some information
709+
from one message to another message.
710+
711+
Some symfony services leak by nature. For example the monolog fingers cross
712+
handler. To avoid such situations, you can configure a transport to
713+
automatically reset the container between two messages:
714+
715+
.. configuration-block::
716+
717+
.. code-block:: yaml
718+
719+
# config/packages/messenger.yaml
720+
framework:
721+
messenger:
722+
transports:
723+
async:
724+
dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
725+
reset_on_message: true
726+
727+
.. code-block:: xml
728+
729+
<!-- config/packages/messenger.xml -->
730+
<?xml version="1.0" encoding="UTF-8" ?>
731+
<container xmlns="http://symfony.com/schema/dic/services"
732+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
733+
xmlns:framework="http://symfony.com/schema/dic/symfony"
734+
xsi:schemaLocation="http://symfony.com/schema/dic/services
735+
https://symfony.com/schema/dic/services/services-1.0.xsd
736+
http://symfony.com/schema/dic/symfony
737+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
738+
739+
<framework:config>
740+
<framework:messenger>
741+
<framework:transport name="async" dsn="%env(MESSENGER_TRANSPORT_DSN)%" reset-on-message="true">
742+
</framework:transport>
743+
</framework:messenger>
744+
</framework:config>
745+
</container>
746+
747+
.. code-block:: php
748+
749+
// config/packages/messenger.php
750+
use Symfony\Config\FrameworkConfig;
751+
752+
return static function (FrameworkConfig $framework) {
753+
$messenger = $framework->messenger();
754+
755+
$messenger->transport('async')
756+
->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
757+
->resetOnMessage(true)
758+
;
759+
};
760+
699761
.. _messenger-retries-failures:
700762

701763
Retries & Failures

0 commit comments

Comments
 (0)
0