Skip to content

Commit bbcc618

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

File tree

1 file changed

+94
-4
lines changed

1 file changed

+94
-4
lines changed

messenger.rst

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,22 +462,29 @@ 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

477+
**Stopping Workers That Encounter Errors**
478+
If a worker dependency like your database server is down, or timeout is reached,
479+
you can try to add :ref:`reconnect logic <middleware-doctrine>`, or just quit
480+
the worker if it receives too many errors with the ``--failure-limit`` option of
481+
the ``messenger:consume`` command.
482+
476483
**Restart Workers on Deploy**
477484
Each time you deploy, you'll need to restart all your worker processes so
478485
that they see the newly deployed code. To do this, run ``messenger:stop-workers``
479486
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
487+
it's currently handling and shut down gracefully. Then, the process manager will create
481488
new worker processes. The command uses the :ref:`app <cache-configuration-with-frameworkbundle>`
482489
cache internally - so make sure this is configured to use an adapter you like.
483490

@@ -633,6 +640,86 @@ config and start your workers:
633640
634641
See the `Supervisor docs`_ for more details.
635642

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

638725
Retries & Failures
@@ -1541,6 +1628,8 @@ middleware and *only* include your own:
15411628
If a middleware service is abstract, a different instance of the service will
15421629
be created per bus.
15431630

1631+
.. _middleware-doctrine:
1632+
15441633
Middleware for Doctrine
15451634
~~~~~~~~~~~~~~~~~~~~~~~
15461635

@@ -1663,4 +1752,5 @@ Learn more
16631752
.. _`Enqueue's transport`: https://github.com/sroze/messenger-enqueue-transport
16641753
.. _`streams`: https://redis.io/topics/streams-intro
16651754
.. _`Supervisor docs`: http://supervisord.org/
1755+
.. _`systemd docs`: https://www.freedesktop.org/wiki/Software/systemd/
16661756
.. _`SymfonyCasts' message serializer tutorial`: https://symfonycasts.com/screencast/messenger/transport-serializer

0 commit comments

Comments
 (0)