To address the issue you're experiencing with the queue running order, you are correct in your observation that the jobs should be ordered by their available_at timestamp rather than their id. This ensures that delayed jobs are processed in the correct order, respecting their intended execution times.
Here's a revised version of the getNextAvailableJob method that orders the jobs by the available_at timestamp:
protected function getNextAvailableJob($queue)
{
$job = $this->database->table($this->table)
->lock($this->getLockForPopping())
->where('queue', $this->getQueue($queue))
->where(function ($query) {
$this->isAvailable($query);
$this->isReservedButExpired($query);
})
->orderBy('available_at', 'asc') // Change to order by available_at
->first();
return $job ? new DatabaseJobRecord((object) $job) : null;
}
Explanation:
-
Locking: The
lockmethod ensures that the job is locked for processing to prevent race conditions. -
Queue Filtering: The
where('queue', $this->getQueue($queue))ensures that only jobs from the specified queue are considered. -
Availability Check: The
isAvailableandisReservedButExpiredmethods filter out jobs that are not available for processing. -
Ordering: The critical change is the
orderBy('available_at', 'asc')line, which orders the jobs by theiravailable_attimestamp in ascending order. This ensures that jobs are processed in the order they become available, respecting any delays.
Redis Consideration:
For Redis, the queue implementation might differ, but the principle remains the same. You need to ensure that jobs are ordered by their availability time. If you're using Laravel's Redis queue driver, you might need to customize the Redis queue implementation similarly to how you did for the database queue.
Conclusion:
Your proposed change is correct and should ensure that jobs are processed in the correct order, respecting their available_at timestamps. This change should be applied to any queue driver that does not already respect the available_at timestamp to ensure consistent behavior across different queue implementations.