Skip to content

Commit

Permalink
Fail job on segmentation fault (#6)
Browse files Browse the repository at this point in the history
* let a worker fail a job when a segmentation fault is detected (abnormal child exit)
* added missing log level
* formatting
  • Loading branch information
050media authored and danhunsaker committed Dec 2, 2018
1 parent 0a6330f commit 0e1f21e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/Resque/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,10 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)

// Wait until the child process finishes before continuing
pcntl_wait($status);
$exitStatus = pcntl_wexitstatus($status);
if($exitStatus !== 0) {

if (pcntl_wifexited($status) !== true) {
$job->fail(new Resque_Job_DirtyExitException('Job exited abnormally'));
} elseif (($exitStatus = pcntl_wexitstatus($status)) !== 0) {
$job->fail(new Resque_Job_DirtyExitException(
'Job exited with exit code ' . $exitStatus
));
Expand All @@ -229,7 +231,7 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
if (in_array($job->getStatus(), array(Resque_Job_Status::STATUS_WAITING, Resque_Job_Status::STATUS_RUNNING)))
{
$job->updateStatus(Resque_Job_Status::STATUS_COMPLETE);
$this->logger->log('done ' . $job);
$this->logger->log(Psr\Log\LogLevel::INFO, 'done ' . $job);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/Resque/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,15 @@ public function testBlockingListPop()

$this->assertEquals(2, $i);
}

public function testWorkerFailsSegmentationFaultJob()
{
Resque::enqueue('jobs', 'Test_Infinite_Recursion_Job');

$worker = new Resque_Worker('jobs');
$worker->setLogger(new Resque_Log());
$worker->work(0);

$this->assertEquals(1, Resque_Stat::get('failed'));
}
}
8 changes: 8 additions & 0 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,12 @@ public function tearDown()
{
self::$called = true;
}
}

class Test_Infinite_Recursion_Job
{
public function perform()
{
$this->perform();
}
}

0 comments on commit 0e1f21e

Please sign in to comment.