@@ -687,6 +687,72 @@ of the desired grace period in seconds) in order to perform a graceful shutdown:
687
687
[program:x]
688
688
stopwaitsecs =20
689
689
690
+ Stateless Worker
691
+ ~~~~~~~~~~~~~~~~
692
+
693
+ PHP is designed to be stateless, there are no shared resources across different
694
+ requests. In HTTP context PHP cleans everything after sending the response, so
695
+ you can decide to not take care of services that may leak memory.
696
+
697
+ On the other hand, workers usually run in long-running CLI processes, which don't
698
+ finish after processing a message. That's why you need to be careful about services
699
+ state to not leak information and/or memory from one message to another message.
700
+
701
+ However, certain Symfony services, such as the Monolog
702
+ :ref: `fingers crossed handler <logging-handler-fingers_crossed >`, leak by design.
703
+ In those cases, use the ``reset_on_message `` transport option to automatically
704
+ reset the service container between two messages:
705
+
706
+ .. configuration-block ::
707
+
708
+ .. code-block :: yaml
709
+
710
+ # config/packages/messenger.yaml
711
+ framework :
712
+ messenger :
713
+ transports :
714
+ async :
715
+ dsn : ' %env(MESSENGER_TRANSPORT_DSN)%'
716
+ reset_on_message : true
717
+
718
+ .. code-block :: xml
719
+
720
+ <!-- config/packages/messenger.xml -->
721
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
722
+ <container xmlns =" http://symfony.com/schema/dic/services"
723
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
724
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
725
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
726
+ https://symfony.com/schema/dic/services/services-1.0.xsd
727
+ http://symfony.com/schema/dic/symfony
728
+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
729
+
730
+ <framework : config >
731
+ <framework : messenger >
732
+ <framework : transport name =" async" dsn =" %env(MESSENGER_TRANSPORT_DSN)%" reset-on-message =" true" >
733
+ </framework : transport >
734
+ </framework : messenger >
735
+ </framework : config >
736
+ </container >
737
+
738
+ .. code-block :: php
739
+
740
+ // config/packages/messenger.php
741
+ use Symfony\Config\FrameworkConfig;
742
+
743
+ return static function (FrameworkConfig $framework) {
744
+ $messenger = $framework->messenger();
745
+
746
+ $messenger->transport('async')
747
+ ->dsn('%env(MESSENGER_TRANSPORT_DSN)%')
748
+ ->resetOnMessage(true)
749
+ ;
750
+ };
751
+
752
+ .. versionadded :: 5.4
753
+
754
+ The ``reset_on_message `` option was introduced in Symfony 5.4.
755
+
690
756
.. _messenger-retries-failures :
691
757
692
758
Retries & Failures
0 commit comments