@@ -462,22 +462,29 @@ Deploying to Production
462
462
463
463
On production, there are a few important things to think about:
464
464
465
- **Use Supervisor to keep your worker(s) running **
465
+ **Use a process manager like Supervisor or systemd to keep your worker(s) running **
466
466
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 >`.
468
469
469
470
**Don't Let Workers Run Forever **
470
471
Some services (like Doctrine's EntityManager) will consume more memory
471
472
over time. So, instead of allowing your worker to run forever, use a flag
472
473
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
474
475
are also other options like ``--memory-limit=128M `` and ``--time-limit=3600 ``.
475
476
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
+
476
483
**Restart Workers on Deploy **
477
484
Each time you deploy, you'll need to restart all your worker processes so
478
485
that they see the newly deployed code. To do this, run ``messenger:stop-workers ``
479
486
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
481
488
new worker processes. The command uses the :ref: `app <cache-configuration-with-frameworkbundle >`
482
489
cache internally - so make sure this is configured to use an adapter you like.
483
490
@@ -633,6 +640,86 @@ config and start your workers:
633
640
634
641
See the `Supervisor docs `_ for more details.
635
642
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
+
636
723
.. _messenger-retries-failures :
637
724
638
725
Retries & Failures
@@ -1541,6 +1628,8 @@ middleware and *only* include your own:
1541
1628
If a middleware service is abstract, a different instance of the service will
1542
1629
be created per bus.
1543
1630
1631
+ .. _middleware-doctrine :
1632
+
1544
1633
Middleware for Doctrine
1545
1634
~~~~~~~~~~~~~~~~~~~~~~~
1546
1635
@@ -1663,4 +1752,5 @@ Learn more
1663
1752
.. _`Enqueue's transport` : https://github.com/sroze/messenger-enqueue-transport
1664
1753
.. _`streams` : https://redis.io/topics/streams-intro
1665
1754
.. _`Supervisor docs` : http://supervisord.org/
1755
+ .. _`systemd docs` : https://www.freedesktop.org/wiki/Software/systemd/
1666
1756
.. _`SymfonyCasts' message serializer tutorial` : https://symfonycasts.com/screencast/messenger/transport-serializer
0 commit comments