Skip to content

Commit 8d045ff

Browse files
committed
Messenger process managers
Add information about mysql timeouts, supervisor FATAL, systemd user services. Add docs for #13617
1 parent 5514b60 commit 8d045ff

File tree

1 file changed

+93
-4
lines changed

1 file changed

+93
-4
lines changed

messenger.rst

+93-4
Original file line numberDiff line numberDiff line change
@@ -462,22 +462,23 @@ Deploying to Production
462462

463463
On production, there are a few important things to think about:
464464

465-
**Use Supervisor to keep your worker(s) running**
465+
**Use a process manager like Supervisor or systemd to keep your worker(s) running**
466466
You'll want one or more "workers" running at all times. To do that, use a
467-
process control system like :ref:`Supervisor <messenger-supervisor>`.
467+
process control system like :ref:`Supervisor <messenger-supervisor>`
468+
or :ref:`systemd <messenger-systemd>`.
468469

469470
**Don't Let Workers Run Forever**
470471
Some services (like Doctrine's EntityManager) will consume more memory
471472
over time. So, instead of allowing your worker to run forever, use a flag
472473
like ``messenger:consume --limit=10`` to tell your worker to only handle 10
473-
messages before exiting (then Supervisor will create a new process). There
474+
messages before exiting (then the process manager will create a new process). There
474475
are also other options like ``--memory-limit=128M`` and ``--time-limit=3600``.
475476

476477
**Restart Workers on Deploy**
477478
Each time you deploy, you'll need to restart all your worker processes so
478479
that they see the newly deployed code. To do this, run ``messenger:stop-workers``
479480
on deploy. This will signal to each worker that it should finish the message
480-
it's currently handling and shut down gracefully. Then, Supervisor will create
481+
it's currently handling and shut down gracefully. Then, the process manager will create
481482
new worker processes. The command uses the :ref:`app <cache-configuration-with-frameworkbundle>`
482483
cache internally - so make sure this is configured to use an adapter you like.
483484

@@ -633,6 +634,91 @@ config and start your workers:
633634
634635
See the `Supervisor docs`_ for more details.
635636

637+
If a worker dependency like your database server is down, or timeout is reached,
638+
you can try to add :ref:`reconnect logic <middleware-doctrine>`, or just quit
639+
the worker if it receives too many errors with the ``--failure-limit`` option of
640+
the ``messenger:consume`` command.
641+
642+
It is possible to end up in a situation where the supervisor job gets into a
643+
FATAL (too many start retries) state when trying to restart when something is
644+
not yet available. You can prevent this by wrapping the Symfony script with a
645+
shell script and sleep when the script fails:
646+
647+
.. code-block:: ini
648+
649+
;/etc/supervisor/conf.d/messenger-worker.conf
650+
[program:messenger-consume]
651+
command=sh -c "/php /path/to/your/app/bin/console messenger:consume async --time-limit=3600 || (sleep 30 && false)"
652+
...
653+
654+
.. _messenger-systemd:
655+
656+
Systemd Configuration
657+
~~~~~~~~~~~~~~~~~~~~~
658+
659+
While Supervisor is a great tool, it has the disadvantage that you need system
660+
access to run it. Systemd has become the standard on most Linux distributions,
661+
and has a good alternative called user services.
662+
663+
Systemd user service configuration files typically live in a ``~/.config/systemd/user``
664+
directory. For example, you can create a new ``messenger-worker.service`` file. Or a
665+
``[email protected]`` file if you want more instances running at the same time:
666+
667+
.. code-block:: ini
668+
669+
[Unit]
670+
Description=Symfony messenger-consume %i
671+
672+
[Service]
673+
ExecStart=php /path/to/your/app/bin/console messenger:consume async --time-limit=3600
674+
Restart=always
675+
RestartSec=30
676+
677+
[Journal]
678+
Storage=persistent
679+
680+
[Install]
681+
WantedBy=default.target
682+
683+
Now, tell systemd to enable and start one worker:
684+
685+
.. code-block:: terminal
686+
687+
$ systemctl --user enable [email protected]
688+
689+
$ systemctl --user start [email protected]
690+
691+
Then enable and start another 19:
692+
693+
.. code-block:: terminal
694+
695+
$ systemctl --user enable messenger-worker@{2..20}.service
696+
697+
$ systemctl --user start messenger-worker@{2..20}.service
698+
699+
If you would change your service config file, reload the daemon:
700+
701+
.. code-block:: terminal
702+
703+
$ systemctl --user daemon-reload
704+
705+
Restart all your consumers:
706+
707+
.. code-block:: terminal
708+
709+
$ systemctl --user restart messenger-consume@*.service
710+
711+
Logs are managed by journald and can be worked with using the journalctl
712+
command, but you do need elevated privileges (sudo) for that:
713+
714+
.. code-block:: terminal
715+
716+
$ sudo journalctl -f [email protected]
717+
718+
$ sudo journalctl -f _UID=$UID
719+
720+
See the `systemd docs`_ for more details.
721+
636722
.. _messenger-retries-failures:
637723

638724
Retries & Failures
@@ -1541,6 +1627,8 @@ middleware and *only* include your own:
15411627
If a middleware service is abstract, a different instance of the service will
15421628
be created per bus.
15431629

1630+
.. _middleware-doctrine:
1631+
15441632
Middleware for Doctrine
15451633
~~~~~~~~~~~~~~~~~~~~~~~
15461634

@@ -1663,4 +1751,5 @@ Learn more
16631751
.. _`Enqueue's transport`: https://github.com/sroze/messenger-enqueue-transport
16641752
.. _`streams`: https://redis.io/topics/streams-intro
16651753
.. _`Supervisor docs`: http://supervisord.org/
1754+
.. _`systemd docs`: https://www.freedesktop.org/wiki/Software/systemd/
16661755
.. _`SymfonyCasts' message serializer tutorial`: https://symfonycasts.com/screencast/messenger/transport-serializer

0 commit comments

Comments
 (0)