diff --git a/messenger.rst b/messenger.rst index ae6f8a455b8..bc06ac6f03e 100644 --- a/messenger.rst +++ b/messenger.rst @@ -696,6 +696,69 @@ of the desired grace period in seconds) in order to perform a graceful shutdown: [program:x] stopwaitsecs=20 + +Stateless Worker +~~~~~~~~~~~~~~~~ + +PHP was designed to be stateless: everything is lost after processing an HTTP +request. When you run your application in an HTTP context, you may not take care +of services states that may leak services since PHP clean everything after +sending the response. + +Since worker run in a CLI context, you need to be careful about services state. +You should avoid to put a state in a service to avoid leaking some information +and/or memory from one message to another message. + +Some symfony services leak by nature. For example the monolog fingers crossed +handler. To avoid such situations, you can configure a transport to +automatically reset the container between two messages: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/messenger.yaml + framework: + messenger: + transports: + async: + dsn: '%env(MESSENGER_TRANSPORT_DSN)%' + reset_on_message: true + + .. code-block:: xml + + + + + + + + + + + + + + .. code-block:: php + + // config/packages/messenger.php + use Symfony\Config\FrameworkConfig; + + return static function (FrameworkConfig $framework) { + $messenger = $framework->messenger(); + + $messenger->transport('async') + ->dsn('%env(MESSENGER_TRANSPORT_DSN)%') + ->resetOnMessage(true) + ; + }; + .. _messenger-retries-failures: Retries & Failures