Skip to content

Commit

Permalink
tweak a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 4, 2016
1 parent b85d3a9 commit 2382dc3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 38 deletions.
32 changes: 4 additions & 28 deletions src/Illuminate/Queue/Jobs/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ abstract class Job
*/
protected $instance;

/**
* The command instance.
*
* @var mixed
*/
protected $command;

/**
* The IoC container instance.
*
Expand Down Expand Up @@ -241,41 +234,24 @@ public function payload()
return json_decode($this->getRawBody(), true);
}

/**
* The underlying command.
*
* @return mixed
*/
public function getCommand()
{
if ($this->command) {
return $this->command;
}

$payload = $this->payload();

return $this->command = isset($payload['data']['command'])
? unserialize($payload['data']['command']) : null;
}

/**
* The number of times to attempt a job.
*
* @return int
* @return int|null
*/
public function retries()
{
return $this->getCommand() ? $this->getCommand()->retries : null;
return array_get($this->payload(), 'retries');
}

/**
* The number of seconds the job can run.
*
* @return int
* @return int|null
*/
public function timeout()
{
return $this->getCommand() ? $this->getCommand()->timeout : null;
return array_get($this->payload(), 'timeout');
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ protected function createPayload($job, $data = '', $queue = null)
if (is_object($job)) {
$payload = json_encode([
'job' => 'Illuminate\Queue\CallQueuedHandler@call',
'retries' => isset($job->retries) ? $job->retries : null,
'timeout' => isset($job->timeout) ? $job->timeout : null,
'data' => [
'commandName' => get_class($job),
'command' => serialize(clone $job),
Expand Down
22 changes: 12 additions & 10 deletions src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function registerTimeoutHandler($job, WorkerOptions $options)
return;
}

$timeout = $job && $job->timeout() !== null ? $job->timeout() : $options->timeout;
$timeout = $job && ! is_null($job->timeout()) ? $job->timeout() : $options->timeout;

pcntl_async_signals(true);

Expand Down Expand Up @@ -281,7 +281,7 @@ protected function handleJobException($connectionName, $job, WorkerOptions $opti
*/
protected function markJobAsFailedIfAlreadyExceedsMaxAttempts($connectionName, $job, $maxTries)
{
$maxTries = $job->retries() !== null ? $job->retries() : $maxTries;
$maxTries = ! is_null($job->retries()) ? $job->retries() : $maxTries;

if ($maxTries === 0 || $job->attempts() <= $maxTries) {
return;
Expand All @@ -308,7 +308,7 @@ protected function markJobAsFailedIfAlreadyExceedsMaxAttempts($connectionName, $
protected function markJobAsFailedIfHasExceededMaxAttempts(
$connectionName, $job, $maxTries, $e
) {
$maxTries = $job->retries() !== null ? $job->retries() : $maxTries;
$maxTries = ! is_null($job->retries()) ? $job->retries() : $maxTries;

if ($maxTries === 0 || $job->attempts() < $maxTries) {
return;
Expand All @@ -331,14 +331,16 @@ protected function failJob($connectionName, $job, $e)
return;
}

// If the job has failed, we will delete it, call the "failed" method and then call
// an event indicating the job has failed so it can be logged if needed. This is
// to allow every developer to better keep monitor of their failed queue jobs.
$job->delete();

$job->failed($e);
try {
// If the job has failed, we will delete it, call the "failed" method and then call
// an event indicating the job has failed so it can be logged if needed. This is
// to allow every developer to better keep monitor of their failed queue jobs.
$job->delete();

$this->raiseFailedJobEvent($connectionName, $job, $e);
$job->failed($e);
} finally {
$this->raiseFailedJobEvent($connectionName, $job, $e);
}
}

/**
Expand Down

0 comments on commit 2382dc3

Please sign in to comment.