The problem:
I have a job in Laravel, based on the condition that an API is reachable it should either run or be released a day later.
The condition to check if the API is reachable works perfectly. The problem, however, occurs when the job is released again. I defined it as $this->release($dayInSeconds);
where $dayInSeconds = 86400;
. So, according to my understanding, the Job should be released to queue again, after 86400 seconds (a day).
The docs defines this behaviour here: Manually releasing a job, and this (old) answer also confirms that I understand the release()
method correctly. Laravel 4.2 queues what does $job->release() do?.
However, when I call $this->release($dayInSeconds)
the job is released again, ranging with a delay of 6 minutes to 4 hours. (We get notifications in a dedicated Teams channel when this happens). However, this should only happen after a day, not after 6 minutes or 4 hours.
The question:
Why is my Job not being released after a day, even though I think I have the correct understanding of the release()
method? Am I missing something or somehow still understanding the release()
method wrong?
Useful information:
- Laravel version: 8
- Queue driver: database
Useful code snippets:
The Job:
class SendOrderTo<REDACTED> implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, ConsoleBaseMethodsTrait;
private int $dayInSeconds = 86400;
public $tries = 5;
public $timeout = 60;
public $backoff = 300;
public $order;
public function __construct (Order $order)
{
$this->order = $order;
}
public function handle ()
{
if (!$this->isApiReachable()) {
// Re-schedule the job for a day later
$this->release($this->dayInSeconds);
// Notify on Teams Alert channel.
$orderId = $this->order->id;
$orderHostName = $this->order->host->name ?? NULL;
TeamsTrait::notifyOnTeams('<REDACTED> Job ' . $orderHostName . ' order ' . $orderId . ' has been re-scheduled.',
'Due to an outage in the <REDACTED> Service this job has been delayed by a day.');
}
// Other logic in the handle() that is not relevant for the question.
}
public function failed (Exception $e)
{
// Just some logging, also not relevant.
}
private function isApiReachable () : bool
{
$data = getServicesAvailabilityFile();
return $data->services->api ?? false;
}
Clarifications:
I used REDACTED in some spaces, this means I am unable to publicly show this name, should not impact the question.
$data
in the isApiReachable()
method is a JSON file, looks something like this, it returns either true
or false
:
{"services":{"api":true,"other":true,}}