Skip to content

Commit 6a99722

Browse files
authored
Merge pull request #496 from garrettrayj/job-queue-failed-cleanup
Job Queue: Throw orphan job exception when child job cleanup fails.
2 parents dda4dcc + 3891bd2 commit 6a99722

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

pkg/job-queue/JobRunner.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ public function runUnique($ownerId, $name, callable $runCallback)
5151
try {
5252
$result = call_user_func($runCallback, $jobRunner, $childJob);
5353
} catch (\Throwable $e) {
54-
$this->jobProcessor->failChildJob($childJob);
54+
try {
55+
$this->jobProcessor->failChildJob($childJob);
56+
} catch (\Throwable $t) {
57+
throw new OrphanJobException(sprintf(
58+
'Job cleanup failed. ID: "%s" Name: "%s"',
59+
$childJob->getId(),
60+
$childJob->getName()
61+
), 0, $e);
62+
}
63+
5564
throw $e;
5665
}
5766

pkg/job-queue/OrphanJobException.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Enqueue\JobQueue;
4+
5+
class OrphanJobException extends \Exception
6+
{
7+
}

pkg/job-queue/Tests/JobRunnerTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Enqueue\JobQueue\Job;
66
use Enqueue\JobQueue\JobProcessor;
77
use Enqueue\JobQueue\JobRunner;
8+
use Enqueue\JobQueue\OrphanJobException;
89

910
class JobRunnerTest extends \PHPUnit\Framework\TestCase
1011
{
@@ -195,6 +196,39 @@ public function testRunUniqueShouldFailJobIfCallbackThrowsException()
195196
});
196197
}
197198

199+
public function testRunUniqueShouldThrowOrphanJobExceptionIfChildCleanupFails()
200+
{
201+
$root = new Job();
202+
$child = new Job();
203+
204+
$jobProcessor = $this->createJobProcessorMock();
205+
$jobProcessor
206+
->expects($this->once())
207+
->method('findOrCreateRootJob')
208+
->will($this->returnValue($root))
209+
;
210+
$jobProcessor
211+
->expects($this->once())
212+
->method('findOrCreateChildJob')
213+
->will($this->returnValue($child))
214+
;
215+
$jobProcessor
216+
->expects($this->never())
217+
->method('successChildJob')
218+
;
219+
$jobProcessor
220+
->expects($this->once())
221+
->method('failChildJob')
222+
->willThrowException(new \Exception())
223+
;
224+
225+
$jobRunner = new JobRunner($jobProcessor);
226+
$this->expectException(OrphanJobException::class);
227+
$jobRunner->runUnique('owner-id', 'job-name', function () {
228+
throw new \Exception();
229+
});
230+
}
231+
198232
public function testRunUniqueShouldNotSuccessJobIfJobIsAlreadyStopped()
199233
{
200234
$root = new Job();

0 commit comments

Comments
 (0)