Skip to content

Commit 0e1f21e

Browse files
050mediadanhunsaker
050media
authored andcommitted
Fail job on segmentation fault (#6)
* let a worker fail a job when a segmentation fault is detected (abnormal child exit) * added missing log level * formatting
1 parent 0a6330f commit 0e1f21e

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

lib/Resque/Worker.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,10 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
218218

219219
// Wait until the child process finishes before continuing
220220
pcntl_wait($status);
221-
$exitStatus = pcntl_wexitstatus($status);
222-
if($exitStatus !== 0) {
221+
222+
if (pcntl_wifexited($status) !== true) {
223+
$job->fail(new Resque_Job_DirtyExitException('Job exited abnormally'));
224+
} elseif (($exitStatus = pcntl_wexitstatus($status)) !== 0) {
223225
$job->fail(new Resque_Job_DirtyExitException(
224226
'Job exited with exit code ' . $exitStatus
225227
));
@@ -229,7 +231,7 @@ public function work($interval = Resque::DEFAULT_INTERVAL, $blocking = false)
229231
if (in_array($job->getStatus(), array(Resque_Job_Status::STATUS_WAITING, Resque_Job_Status::STATUS_RUNNING)))
230232
{
231233
$job->updateStatus(Resque_Job_Status::STATUS_COMPLETE);
232-
$this->logger->log('done ' . $job);
234+
$this->logger->log(Psr\Log\LogLevel::INFO, 'done ' . $job);
233235
}
234236
}
235237
}

test/Resque/Tests/WorkerTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,15 @@ public function testBlockingListPop()
290290

291291
$this->assertEquals(2, $i);
292292
}
293+
294+
public function testWorkerFailsSegmentationFaultJob()
295+
{
296+
Resque::enqueue('jobs', 'Test_Infinite_Recursion_Job');
297+
298+
$worker = new Resque_Worker('jobs');
299+
$worker->setLogger(new Resque_Log());
300+
$worker->work(0);
301+
302+
$this->assertEquals(1, Resque_Stat::get('failed'));
303+
}
293304
}

test/bootstrap.php

+8
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,12 @@ public function tearDown()
145145
{
146146
self::$called = true;
147147
}
148+
}
149+
150+
class Test_Infinite_Recursion_Job
151+
{
152+
public function perform()
153+
{
154+
$this->perform();
155+
}
148156
}

0 commit comments

Comments
 (0)