From cffcd347901617b19e8eca05be55cda280e0d262 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 2 Jul 2016 14:23:33 -0500 Subject: [PATCH] refactoring changes --- src/Illuminate/Queue/DatabaseQueue.php | 44 +++++++++++++++++++------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Queue/DatabaseQueue.php b/src/Illuminate/Queue/DatabaseQueue.php index eb6cf8cd30c0..8ef03e5bc205 100644 --- a/src/Illuminate/Queue/DatabaseQueue.php +++ b/src/Illuminate/Queue/DatabaseQueue.php @@ -186,16 +186,8 @@ 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(); @@ -203,6 +195,36 @@ protected function getNextAvailableJob($queue) 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. * @@ -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,