diff --git a/src/DataObjects/QueuedJobDescriptor.php b/src/DataObjects/QueuedJobDescriptor.php index 9bca9d08..e4c73c5d 100644 --- a/src/DataObjects/QueuedJobDescriptor.php +++ b/src/DataObjects/QueuedJobDescriptor.php @@ -269,6 +269,18 @@ public function cleanupJob() } } + public function onBeforeWrite() + { + // if a job is marked as 'waiting' for a restart, we need to reset the + // worker it was assigned to, otherwise it'll never pick up and go again if + // it was paused from the UI, or a worker stopping to release memory / other reason! + if ($this->JobStatus == QueuedJob::STATUS_WAIT) { + $this->Worker = null; + } + + parent::onBeforeWrite(); + } + public function onBeforeDelete() { parent::onBeforeDelete(); diff --git a/tests/QueuedJobsTest.php b/tests/QueuedJobsTest.php index 6fb2f207..cee03b90 100644 --- a/tests/QueuedJobsTest.php +++ b/tests/QueuedJobsTest.php @@ -290,6 +290,54 @@ public function testNextJob() $this->assertNotEquals((int) $job->ID, (int) $next->ID); } + /** + * @throws ValidationException + */ + public function testNextResumedJob() + { + $svc = $this->getService(); + $list = $svc->getJobList(); + + foreach ($list as $job) { + $job->delete(); + } + + $list = $svc->getJobList(); + $this->assertCount(0, $list); + + $job = new TestQueuedJob(); + $id1 = $svc->queueJob($job); + + // okay, lets get the first one and initialise it, then make sure that a subsequent init attempt fails + $job = $svc->getNextPendingJob(); + + $this->assertEquals($id1, $job->ID); + $svc->testInit($job); + + // assign job locking properties + $job->Worker = 'test worker'; + $job->WorkerCount = (int) $job->WorkerCount + 1; + $job->Expiry = '2016-01-01 16:00:01'; + $job->write(); + + $next = $svc->getNextPendingJob(); + + $this->assertNull($next); + + // okay now pause the job and resume it - it _should_ be the next one + $job->pause(true); + + $next = $svc->getNextPendingJob(); + $this->assertNull($next); + + $job->resume(true); + + $next = $svc->getNextPendingJob(); + $this->assertNotNull($next); + + $this->assertEquals($job->ID, $next->ID); + } + /** * Verify that broken jobs are correctly verified for health and restarted as necessary *