@@ -1897,6 +1897,136 @@ on a case-by-case basis via the :class:`Symfony\\Component\\Messenger\\Stamp\\Se
18971897 provides that control. See `SymfonyCasts' message serializer tutorial `_ for
18981898 details.
18991899
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 --dir=var/temp'));
1926+ $this->bus->dispatch(new RunCommandMessage('cache:clear'));
1927+ }
1928+ }
1929+
1930+ You can configure the behavior in the case of something going 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 handler will return a
1936+ :class: `Symfony\\ Component\\ Console\\ Messenger\\ RunCommandContext ` which
1937+ contains many useful information such as the exit code or the output of the
1938+ process. You can refer to the page dedicated on
1939+ :doc: `handler results </messenger/handler_results >` for more information.
1940+
1941+ .. versionadded :: 6.4
1942+
1943+ The :class: `Symfony\\ Component\\ Console\\ Messenger\\ RunCommandMessage `
1944+ and :class: `Symfony\\ Component\\ Console\\ Messenger\\ RunCommandContext `
1945+ classes were introduced in Symfony 6.4.
1946+
1947+ Trigger An External Process
1948+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1949+
1950+ Messenger comes with a handy helper to run external processes by
1951+ dispatching a message. This takes advantages of the
1952+ :doc: `Process component </components/process >`. By dispatching a
1953+ :class: `Symfony\\ Component\\ Process\\ Messenger\\ RunProcessMessage `, Messenger
1954+ will take care of creating a new process with the parameters you passed::
1955+
1956+ use Symfony\Component\Messenger\MessageBusInterface;
1957+ use Symfony\Component\Process\Messenger\RunProcessMessage;
1958+
1959+ class CleanUpService
1960+ {
1961+ public function __construct(private readonly MessageBusInterface $bus)
1962+ {
1963+ }
1964+
1965+ public function cleanUp(): void
1966+ {
1967+ $this->bus->dispatch(new RunProcessMessage(['rm', '-rf', 'var/log/temp/*'], cwd: '/my/custom/working-dir'));
1968+
1969+ // ...
1970+ }
1971+ }
1972+
1973+ Once handled, the handler will return a
1974+ :class: `Symfony\\ Component\\ Process\\ Messenger\\ RunProcessContext ` which
1975+ contains many useful information such as the exit code or the output of the
1976+ process. You can refer to the page dedicated on
1977+ :doc: `handler results </messenger/handler_results >` for more information.
1978+
1979+ .. versionadded :: 6.4
1980+
1981+ The :class: `Symfony\\ Component\\ Process\\ Messenger\\ RunProcessMessage `
1982+ and :class: `Symfony\\ Component\\ Process\\ Messenger\\ RunProcessContext `
1983+ classes were introduced in Symfony 6.4.
1984+
1985+ Pinging A Webservice
1986+ --------------------
1987+
1988+ Sometimes, you may need to regularly ping a webservice to get its status, e.g.
1989+ is it up or down. It is possible to do so by dispatching a
1990+ :class: `Symfony\\ Component\\ HttpClient\\ Messenger\\ PingWebhookMessage `::
1991+
1992+ use Symfony\Component\HttpClient\Messenger\RPingWebhookMessage;
1993+ use Symfony\Component\Messenger\MessageBusInterface;
1994+
1995+ class LivenessService
1996+ {
1997+ public function __construct(private readonly MessageBusInterface $bus)
1998+ {
1999+ }
2000+
2001+ public function ping(): void
2002+ {
2003+ // An HttpExceptionInterface is thrown on 3xx/4xx/5xx
2004+ $this->bus->dispatch(new PingWebhookMessage('GET', 'https://example.com/status');
2005+
2006+ // Ping, but does not throw on 3xx/4xx/5xx
2007+ $this->bus->dispatch(new PingWebhookMessage('GET', 'https://example.com/status', throw: false);
2008+
2009+ // Any valid HttpClientInterface option can be used
2010+ $this->bus->dispatch(new PingWebhookMessage('POST', 'https://example.com/status', [
2011+ 'headers' => [
2012+ 'Authorization' => 'Bearer ...'
2013+ ],
2014+ 'json' => [
2015+ 'data' => 'some-data',
2016+ ],
2017+ ]));
2018+ }
2019+ }
2020+
2021+ The handler will return a
2022+ :class: `Symfony\\ Contracts\\ HttpClient\\ ResponseInterface `, allowing you to
2023+ gather and process information returned by the HTTP request.
2024+
2025+ .. versionadded :: 6.4
2026+
2027+ The :class: `Symfony\\ Component\\ HttpClient\\ Messenger\\ PingWebhookMessage `
2028+ class was introduced in Symfony 6.4.
2029+
19002030Customizing Handlers
19012031--------------------
19022032
0 commit comments