Skip to content

Commit 76281c1

Browse files
author
Pooya Parsa
committed
queue tests
1 parent 65fd254 commit 76281c1

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Jenssegers/Mongodb/Queue/MongoQueue.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,21 @@ protected function getNextAvailableJobAndReserve($queue)
7979
*/
8080
protected function releaseJobsThatHaveBeenReservedTooLong($queue)
8181
{
82-
$expired = Carbon::now()->subSeconds($this->expire)->getTimestamp();
82+
$expiration = Carbon::now()->subSeconds($this->expire)->getTimestamp();
83+
$now = time();
8384

8485
$reserved = $this->database->collection($this->table)
8586
->where('queue', $this->getQueue($queue))
86-
->where('reserved', 1)
87-
->where('reserved_at', '<=', $expired)->get();
87+
->where(function ($query) use ($expiration, $now) {
88+
// Check for available jobs
89+
$query->where(function ($query) use ($now) {
90+
$query->whereNull('reserved_at');
91+
$query->where('available_at', '<=', $now);
92+
});
93+
94+
// Check for jobs that are reserved but have expired
95+
$query->orWhere('reserved_at', '<=', $expiration);
96+
})->get();
8897

8998
foreach ($reserved as $job) {
9099
$attempts = $job['attempts'] + 1;

tests/QueueTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testQueueJobExpired()
3838

3939
// Expect an attempted older job in the queue
4040
$job = Queue::pop('test');
41-
$this->assertEquals(2, $job->getDatabaseJob()->attempts);
41+
$this->assertEquals(1, $job->getDatabaseJob()->attempts);
4242
$this->assertGreaterThan($expiry, $job->getDatabaseJob()->reserved_at);
4343

4444
$job->delete();

0 commit comments

Comments
 (0)