@@ -696,6 +696,69 @@ 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 an HTTP context, you may not take care
705+ of services states that may leak services since PHP clean everything after
706+ sending the response.
707+
708+ Since worker run in a CLI context, you need to be careful about services state.
709+ You should avoid to put a state in a service to avoid leaking some information
710+ and/or memory from one message to another message.
711+
712+ Some symfony services leak by nature. For example the monolog fingers crossed
713+ handler. To avoid such situations, you can configure a transport to
714+ automatically reset the container between two messages:
715+
716+ .. configuration-block ::
717+
718+ .. code-block :: yaml
719+
720+ # config/packages/messenger.yaml
721+ framework :
722+ messenger :
723+ transports :
724+ async :
725+ dsn : ' %env(MESSENGER_TRANSPORT_DSN)%'
726+ reset_on_message : true
727+
728+ .. code-block :: xml
729+
730+ <!-- config/packages/messenger.xml -->
731+ <?xml version =" 1.0" encoding =" UTF-8" ?>
732+ <container xmlns =" http://symfony.com/schema/dic/services"
733+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
734+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
735+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
736+ https://symfony.com/schema/dic/services/services-1.0.xsd
737+ http://symfony.com/schema/dic/symfony
738+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
739+
740+ <framework : config >
741+ <framework : messenger >
742+ <framework : transport name =" async" dsn =" %env(MESSENGER_TRANSPORT_DSN)%" reset-on-message =" true" >
743+ </framework : transport >
744+ </framework : messenger >
745+ </framework : config >
746+ </container >
747+
748+ .. code-block :: php
749+
750+ // config/packages/messenger.php
751+ use Symfony\Config\FrameworkConfig;
752+
753+ return static function (FrameworkConfig $framework) {
754+ $messenger = $framework->messenger();
755+
756+ $messenger->transport('async')
757+ ->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
758+ ->resetOnMessage(true)
759+ ;
760+ };
761+
699762 .. _messenger-retries-failures :
700763
701764Retries & Failures
0 commit comments