Skip to content

Commit 8372b79

Browse files
authored
Catch symfony/messenger exceptions on message dispatch and convert it to job failure (#56)
1 parent 2b577ca commit 8372b79

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/DispatchMessageJobLauncher.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yokai\Batch\Bridge\Symfony\Messenger;
66

7+
use Symfony\Component\Messenger\Exception\ExceptionInterface;
78
use Symfony\Component\Messenger\MessageBusInterface;
89
use Yokai\Batch\BatchStatus;
910
use Yokai\Batch\Factory\JobExecutionFactory;
@@ -35,8 +36,17 @@ public function launch(string $name, array $configuration = []): JobExecution
3536
$jobExecution->setStatus(BatchStatus::PENDING);
3637
$this->jobExecutionStorage->store($jobExecution);
3738

38-
// dispatch message
39-
$this->messageBus->dispatch(new LaunchJobMessage($name, $configuration));
39+
try {
40+
// dispatch message
41+
$this->messageBus->dispatch(new LaunchJobMessage($name, $configuration));
42+
} catch (ExceptionInterface $exception) {
43+
// if a messenger exception occurs, it will be converted to job failure
44+
$jobExecution->setStatus(BatchStatus::FAILED);
45+
$jobExecution->addFailureException($exception);
46+
$this->jobExecutionStorage->store($jobExecution);
47+
48+
return $jobExecution;
49+
}
4050

4151
// re-fetch and return job execution from storage
4252
// if transport is synchronous, job execution may have been filled during execution

tests/DispatchMessageJobLauncherTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Prophecy\Argument;
99
use Prophecy\PhpUnit\ProphecyTrait;
1010
use Symfony\Component\Messenger\Envelope;
11+
use Symfony\Component\Messenger\Exception\TransportException;
1112
use Symfony\Component\Messenger\MessageBusInterface;
1213
use Yokai\Batch\BatchStatus;
1314
use Yokai\Batch\Bridge\Symfony\Messenger\DispatchMessageJobLauncher;
@@ -81,4 +82,30 @@ static function ($message): bool {
8182
self::assertSame('123456789', $jobExecutionFromStorage->getId());
8283
self::assertSame(BatchStatus::PENDING, $jobExecutionFromStorage->getStatus()->getValue());
8384
}
85+
86+
public function testLaunchAndMessengerFail(): void
87+
{
88+
$messageBus = $this->prophesize(MessageBusInterface::class);
89+
$messageBus->dispatch(Argument::any())
90+
->shouldBeCalled()
91+
->willThrow(new TransportException('This is a test'));
92+
93+
$jobLauncher = new DispatchMessageJobLauncher(
94+
new JobExecutionFactory(new UniqidJobExecutionIdGenerator()),
95+
$storage = new InMemoryJobExecutionStorage(),
96+
$messageBus->reveal()
97+
);
98+
99+
$jobExecutionFromLauncher = $jobLauncher->launch('testing');
100+
101+
[$jobExecutionFromStorage] = $storage->getExecutions();
102+
self::assertSame($jobExecutionFromLauncher, $jobExecutionFromStorage);
103+
104+
self::assertSame('testing', $jobExecutionFromStorage->getJobName());
105+
self::assertSame(BatchStatus::FAILED, $jobExecutionFromStorage->getStatus()->getValue());
106+
self::assertCount(1, $jobExecutionFromStorage->getFailures());
107+
$failure = $jobExecutionFromStorage->getFailures()[0];
108+
self::assertSame(TransportException::class, $failure->getClass());
109+
self::assertSame('This is a test', $failure->getMessage());
110+
}
84111
}

0 commit comments

Comments
 (0)