Skip to content

Job Queue: Throw orphan job exception when child job cleanup fails. #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion pkg/job-queue/JobRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ public function runUnique($ownerId, $name, callable $runCallback)
try {
$result = call_user_func($runCallback, $jobRunner, $childJob);
} catch (\Throwable $e) {
$this->jobProcessor->failChildJob($childJob);
try {
$this->jobProcessor->failChildJob($childJob);
} catch (\Throwable $t) {
throw new OrphanJobException(sprintf(
'Job cleanup failed. ID: "%s" Name: "%s"',
$childJob->getId(),
$childJob->getName()
), 0, $e);
}

throw $e;
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/job-queue/OrphanJobException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Enqueue\JobQueue;

class OrphanJobException extends \Exception
{
}
34 changes: 34 additions & 0 deletions pkg/job-queue/Tests/JobRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Enqueue\JobQueue\Job;
use Enqueue\JobQueue\JobProcessor;
use Enqueue\JobQueue\JobRunner;
use Enqueue\JobQueue\OrphanJobException;

class JobRunnerTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -195,6 +196,39 @@ public function testRunUniqueShouldFailJobIfCallbackThrowsException()
});
}

public function testRunUniqueShouldThrowOrphanJobExceptionIfChildCleanupFails()
{
$root = new Job();
$child = new Job();

$jobProcessor = $this->createJobProcessorMock();
$jobProcessor
->expects($this->once())
->method('findOrCreateRootJob')
->will($this->returnValue($root))
;
$jobProcessor
->expects($this->once())
->method('findOrCreateChildJob')
->will($this->returnValue($child))
;
$jobProcessor
->expects($this->never())
->method('successChildJob')
;
$jobProcessor
->expects($this->once())
->method('failChildJob')
->willThrowException(new \Exception())
;

$jobRunner = new JobRunner($jobProcessor);
$this->expectException(OrphanJobException::class);
$jobRunner->runUnique('owner-id', 'job-name', function () {
throw new \Exception();
});
}

public function testRunUniqueShouldNotSuccessJobIfJobIsAlreadyStopped()
{
$root = new Job();
Expand Down