Skip to content

Commit

Permalink
Merge pull request #17 from uafrica/paused-exception
Browse files Browse the repository at this point in the history
Paused exception
  • Loading branch information
dakota committed Mar 13, 2018
2 parents c5d5b24 + 8630b29 commit 42aabcb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/DelayedJob/JobManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use DelayedJobs\DelayedJob\Exception\EnqueueException;
use DelayedJobs\DelayedJob\Exception\JobExecuteException;
use DelayedJobs\Exception\NonRetryableException;
use DelayedJobs\Exception\PausedException;
use DelayedJobs\Result\Failed;
use DelayedJobs\Result\Pause;
use DelayedJobs\Result\ResultInterface;
use DelayedJobs\Result\Success;
use DelayedJobs\Traits\DebugLoggerTrait;
Expand Down Expand Up @@ -313,6 +315,8 @@ protected function _buildResultObject(Job $job, $result): ResultInterface
return $result;
} elseif ($result instanceof \DateTimeInterface) {
return (new Success($job, "Reoccur at {$result}"))->willRecur($result);
} elseif ($result instanceof PausedException) {
return new Pause($job);
} elseif ($result instanceof \Error || $result instanceof NonRetryableException) {
return (new Failed($job, $result->getMessage()))->willRetry(false)
->setException($result);
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/PausedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace DelayedJobs\Exception;

/**
* Class PausedException
*/
class PausedException extends \Exception
{

}
1 change: 1 addition & 0 deletions src/Model/Entity/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ class Worker extends Entity
const SHUTDOWN_LOOP_EXIT = 'loop exited';
const SHUTDOWN_NO_WORKER = 'no worker';
const SHUTDOWN_WRONG_PID = 'wrong pid';
const SHUTDOWN_ERROR = 'an error occured';
}
29 changes: 22 additions & 7 deletions src/Shell/WorkerShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WorkerShell extends AppShell
const MAXFAIL = 5;
const SUICIDE_EXIT_CODE = 100;
const NO_WORKER_EXIT_CODE = 110;
const WORKER_ERROR_EXIT_CODE = 120;
const HEARTBEAT_TIME = 30;

public $modelClass = 'DelayedJobs.DelayedJobs';
Expand Down Expand Up @@ -318,8 +319,7 @@ public function afterExecute(Event $event, ResultInterface $result, $duration)
Shell::VERBOSE
);
if ($result->getException()) {
$this->out($result->getException()
->getTraceAsString(), 1, Shell::VERBOSE);
$this->out($result->getException()->getTraceAsString(), 1, Shell::VERBOSE);
}
} elseif ($result instanceof Pause) {
$this->out(sprintf('<info> - Execution paused</info> :: <info>%s</info>', $result->getMessage()), 1, Shell::VERBOSE);
Expand All @@ -342,8 +342,17 @@ public function afterExecute(Event $event, ResultInterface $result, $duration)
}
}

public function afterCompleted()
/**
* @param \Cake\Event\Event $event
* @param \DelayedJobs\Result\ResultInterface $result
* @return void
*/
public function afterCompleted(Event $event, ResultInterface $result)
{
if ($this->param('stop-on-failure') && $result instanceof Failed) {
$this->stopHammerTime(Worker::SHUTDOWN_ERROR, self::WORKER_ERROR_EXIT_CODE);
}

$this->_timeOfLastJob = microtime(true);
$this->_checkSuicideStatus();

Expand All @@ -360,10 +369,16 @@ public function afterCompleted()
public function getOptionParser(): ConsoleOptionParser
{
$options = parent::getOptionParser();
$options->addSubcommand('worker', [
'help' => 'Executes a job',
'parser' => $this->Worker->getOptionParser(),
]);
$options
->addOption('stop-on-failure', [
'short' => 's',
'help' => 'The worker will immediately stop on any failure',
'boolean' => true,
])
->addSubcommand('worker', [
'help' => 'Executes a job',
'parser' => $this->Worker->getOptionParser(),
]);

return $options;
}
Expand Down

0 comments on commit 42aabcb

Please sign in to comment.