Skip to content

Commit

Permalink
refactoring changes
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jul 2, 2016
1 parent 87f3e5d commit cffcd34
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/Illuminate/Queue/DatabaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,45 @@ protected function getNextAvailableJob($queue)
->lockForUpdate()
->where('queue', $this->getQueue($queue))
->where(function ($query) {
// Where not reserved.
$query->where(function ($query) {
$query->where('reserved', 0);
$query->where('available_at', '<=', $this->getTime());
});
// Or where reserved and reservation has expired.
$query->orWhere(function ($query) {
$query->where('reserved', 1);
$query->where('reserved_at', '<=', Carbon::now()->subSeconds($this->expire)->getTimestamp());
});
$this->isAvailable($query);
$this->isReservedButExpired($query);
})
->orderBy('id', 'asc')
->first();

return $job ? (object) $job : null;
}

/**
* Modify the query to check for available jobs.
*
* @param \Illuminate\Database\Query\Builder $query
* @return void
*/
protected function isAvailable($query)
{
$query->where(function ($query) {
$query->where('reserved', 0);
$query->where('available_at', '<=', $this->getTime());
});
}

/**
* Modify the query to check for jobs that are reserved but have expired.
*
* @param \Illuminate\Database\Query\Builder $query
* @return void
*/
protected function isReservedButExpired($query)
{
$expiration = Carbon::now()->subSeconds($this->expire)->getTimestamp();

$query->orWhere(function ($query) use ($expiration) {
$query->where('reserved', 1);
$query->where('reserved_at', '<=', $expiration);
});
}

/**
* Mark the given job ID as reserved.
*
Expand All @@ -212,8 +234,8 @@ protected function getNextAvailableJob($queue)
protected function markJobAsReserved($job)
{
$job->reserved = 1;
$job->attempts = $job->attempts + 1;
$job->reserved_at = $this->getTime();
$job->attempts = ++$job->attempts;

$this->database->table($this->table)->where('id', $job->id)->update([
'reserved' => $job->reserved,
Expand Down

0 comments on commit cffcd34

Please sign in to comment.