@@ -1897,6 +1897,130 @@ on a case-by-case basis via the :class:`Symfony\\Component\\Messenger\\Stamp\\Se
1897
1897
provides that control. See `SymfonyCasts' message serializer tutorial `_ for
1898
1898
details.
1899
1899
1900
+ Running Commands And External Processes
1901
+ ---------------------------------------
1902
+
1903
+ Trigger a Command
1904
+ ~~~~~~~~~~~~~~~~~
1905
+
1906
+ It is possible to trigger any command by dispatching a
1907
+ :class: `Symfony\\ Component\\ Console\\ Messenger\\ RunCommandMessage `. Symfony
1908
+ will take care of handling this message and execute the command passed
1909
+ to the message parameter::
1910
+
1911
+ use Symfony\Component\Console\Messenger\RunCommandMessage;
1912
+ use Symfony\Component\Messenger\MessageBusInterface;
1913
+
1914
+ class CleanUpService
1915
+ {
1916
+ public function __construct(private readonly MessageBusInterface $bus)
1917
+ {
1918
+ }
1919
+
1920
+ public function cleanUp(): void
1921
+ {
1922
+ // Long task with some caching...
1923
+
1924
+ // Once finished, dispatch some clean up commands
1925
+ $this->bus->dispatch(new RunCommandMessage('app:my-cache:clean-up'));
1926
+ $this->bus->dispatch(new RunCommandMessage('cache:clear'));
1927
+ }
1928
+ }
1929
+
1930
+ You can configure the behavior to adopt if something goes wrong during command
1931
+ execution. To do so, you can use the ``throwOnFailure `` and ``catchExceptions ``
1932
+ parameters when creating your instance of
1933
+ :class: `Symfony\\ Component\\ Console\\ Messenger\\ RunCommandMessage `.
1934
+
1935
+ Once handled, the message will contain many useful information such as the exit
1936
+ code or the output of the command. You can refer to the page dedicated on
1937
+ :doc: `handler results </messenger/handler_results >` for more information.
1938
+
1939
+ .. versionadded :: 6.4
1940
+
1941
+ The :class: `Symfony\\ Component\\ Console\\ Messenger\\ RunCommandMessage `
1942
+ class was introduced in Symfony 6.4.
1943
+
1944
+ Trigger An External Process
1945
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1946
+
1947
+ Messenger comes with a handy helper to run external processes by
1948
+ dispatching a message. This takes advantages of the
1949
+ :doc: `Process component </components/process >`. By dispatching a
1950
+ :class: `Symfony\\ Component\\ Process\\ Messenger\\ RunProcessMessage `, Messenger
1951
+ will take care of creating a new process with the parameters you passed::
1952
+
1953
+ use Symfony\Component\Messenger\MessageBusInterface;
1954
+ use Symfony\Component\Process\Messenger\RunProcessMessage;
1955
+
1956
+ class CleanUpService
1957
+ {
1958
+ public function __construct(private readonly MessageBusInterface $bus)
1959
+ {
1960
+ }
1961
+
1962
+ public function cleanUp(): void
1963
+ {
1964
+ $this->bus->dispatch(new RunProcessMessage(['rm', '-rf', 'var/log/temp/*'], cwd: '/my/custom/working-dir'));
1965
+
1966
+ // ...
1967
+ }
1968
+ }
1969
+
1970
+ Once handled, the message will contain many useful information such as the exit
1971
+ code or the output of the process. You can refer to the page dedicated on
1972
+ :doc: `handler results </messenger/handler_results >` for more information.
1973
+
1974
+ .. versionadded :: 6.4
1975
+
1976
+ The :class: `Symfony\\ Component\\ Process\\ Messenger\\ RunProcessMessage `
1977
+ class was introduced in Symfony 6.4.
1978
+
1979
+ Pinging A Webservice
1980
+ --------------------
1981
+
1982
+ Sometimes, you may need to regularly ping a webservice to get its status, e.g.
1983
+ is it up or down. It is possible to do so by dispatching a
1984
+ :class: `Symfony\\ Component\\ HttpClient\\ Messenger\\ PingWebhookMessage `::
1985
+
1986
+ use Symfony\Component\HttpClient\Messenger\RPingWebhookMessage;
1987
+ use Symfony\Component\Messenger\MessageBusInterface;
1988
+
1989
+ class LivenessService
1990
+ {
1991
+ public function __construct(private readonly MessageBusInterface $bus)
1992
+ {
1993
+ }
1994
+
1995
+ public function ping(): void
1996
+ {
1997
+ // An HttpExceptionInterface is thrown on 3xx/4xx/5xx
1998
+ $this->bus->dispatch(new PingWebhookMessage('GET', 'https://example.com/status');
1999
+
2000
+ // Ping, but does not throw on 3xx/4xx/5xx
2001
+ $this->bus->dispatch(new PingWebhookMessage('GET', 'https://example.com/status', throw: false);
2002
+
2003
+ // Any valid HttpClientInterface option can be used
2004
+ $this->bus->dispatch(new PingWebhookMessage('POST', 'https://example.com/status', [
2005
+ 'headers' => [
2006
+ 'Authorization' => 'Bearer ...'
2007
+ ],
2008
+ 'json' => [
2009
+ 'data' => 'some-data',
2010
+ ],
2011
+ ]);
2012
+ }
2013
+ }
2014
+
2015
+ The handler will return a
2016
+ :class: `Symfony\\ Contracts\\ HttpClient\\ ResponseInterface `, allowing you to
2017
+ gather and process information returned by the HTTP request.
2018
+
2019
+ .. versionadded :: 6.4
2020
+
2021
+ The :class: `Symfony\\ Component\\ HttpClient\\ Messenger\\ PingWebhookMessage `
2022
+ class was introduced in Symfony 6.4.
2023
+
1900
2024
Customizing Handlers
1901
2025
--------------------
1902
2026
0 commit comments