From dbdea62f057ec256c3b7f0c56f79f2d63171fab5 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Sun, 12 Aug 2018 12:54:29 -0600 Subject: [PATCH] Throw orphan job exception when child job cleanup fails. --- pkg/job-queue/JobRunner.php | 11 ++++++++- pkg/job-queue/OrphanJobException.php | 7 ++++++ pkg/job-queue/Tests/JobRunnerTest.php | 34 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 pkg/job-queue/OrphanJobException.php diff --git a/pkg/job-queue/JobRunner.php b/pkg/job-queue/JobRunner.php index 17a51b110..afd445c67 100644 --- a/pkg/job-queue/JobRunner.php +++ b/pkg/job-queue/JobRunner.php @@ -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; } diff --git a/pkg/job-queue/OrphanJobException.php b/pkg/job-queue/OrphanJobException.php new file mode 100644 index 000000000..37eac5ca7 --- /dev/null +++ b/pkg/job-queue/OrphanJobException.php @@ -0,0 +1,7 @@ +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();