Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel non-overlapping scheduled job not executing

I have a Laravel Scheduled job which is defined in Kernel.php like so

$schedule->call('\App\Http\Controllers\ScheduleController@processQueuedMessages')
    ->everyFiveMinutes()
    ->name('process_queued_messages')
    ->withoutOverlapping();

During development, my job threw an exception due to a syntax error. I corrected the error and tried executing it again; but for some reason it won't.

I tried artisan down and then artisan up. I also tried restarting the server instance. But nothing would help. The job just didn't get executed (there was no exception either).

I realize that the problem is due to ->withoutOverlapping(). Somehow, Laravel scheduler is thinking that the job is already running and hence is not executing it again.

like image 939
linuxartisan Avatar asked Nov 28 '22 13:11

linuxartisan


2 Answers

I found the solution by looking at the vendor code.

Illuminate\Console\Scheduling\CallbackEvent.php

It creates a file in local storage with the name schedule-*.

public function withoutOverlapping()
{
    if ( ! isset($this->description))
    {
        throw new LogicException(
            "A scheduled event name is required to prevent overlapping. Use the 'name' method before 'withoutOverlapping'."
        );
    }

    return $this->skip(function()
        {
            return file_exists($this->mutexPath());
        });
}

protected function mutexPath()
{
    return storage_path().'/framework/schedule-'.md5($this->description);
}

Deleting the file schedule-* at storage/framework resolved the issue.

like image 58
linuxartisan Avatar answered Dec 04 '22 03:12

linuxartisan


To anyone reading this, deleting the schedule files yourself, is not the right way to go. You need to specify - the lock time - based on which, withoutOverlapping prevents further tasks from running.

As cited in Laravel - Task Scheduling

If needed, you may specify how many minutes must pass before the "without overlapping" lock expires. By default, the lock will expire after 24 hours:

Your problem originates due to the fact that withoutOverlapping applies a default lock for 24 hours. So you had to wait for 24 hours before similar tasks are accepted. Simply adjust the lock time based on your needs by doing:

$schedule->command('emails:send')->withoutOverlapping(10); // where 10 refers to minutes

like image 34
nmargaritis Avatar answered Dec 04 '22 05:12

nmargaritis