Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 - Task schedule withoutOverlapping not working

I try to run schedule on Laravel 5. Its work fine when I run this:

$schedule->call(function() {
   // do something here..
})->everyMinute();

But when I add withoutOverlapping(), the scheduler never run the task:

$schedule->call(function () {
   // do something here..
})->everyMinute()->name('job_name')->withoutOverlapping();

*these schedule code is written at /app/Console/Kernel.php

like image 398
Hussaini Zulkifli Avatar asked Jun 26 '15 03:06

Hussaini Zulkifli


4 Answers

The order is important, but it was never mentioned.

Try this

$schedule->call(function () {
   // do something here..
})->name('job_name')->withoutOverlapping()->everyMinute();

This is how it worked for me:

(1) call -> (2) name -> (3) withoutOverlapping -> (4) dailyAt -> (5) onOneServer

when you mess around with the order you can get errors like

A scheduled event name is required to prevent overlapping. Use the name method before 'withoutOverlapping'.

or

Call to undefined method Illuminate\Console\Scheduling\Schedule::name()

like image 190
Yevgeniy Afanasyev Avatar answered Nov 11 '22 08:11

Yevgeniy Afanasyev


Delete ->everyMinute() when using ->withoutOverlapping() it will still run every minute but without overlapping.

UPDATE

Since Laravel v. 5.5+ you can specify on how many minutes must pass before the "without overlapping" lock expires.

eg. ->withoutOverlapping(10) can be used to unlock the "overlapping" when 10 minutes will pass.

like image 17
Vizjerei Avatar answered Nov 11 '22 07:11

Vizjerei


withoutOverlapping method creates a mutex file when a command is executed and deletes the mutex file when it execution is finished.

Scheduler check if mutex file is present for any command it doesn't allow to execute it again.

In your case the mutex file is not deleted and it prevents the command to run again.

You can clear the laravel cache to make it work again using php artisan cache:clear

like image 4
Waqas Saeed Avatar answered Nov 11 '22 06:11

Waqas Saeed


The cron withoutOverlapping() is now working. Let's understand how it works For example:

$schedule->command('command')
                    ->hourly()
                    ->withoutOverlapping();

The withoutOverlapping means when cron runs then it will generate a lock file in storage/framework/ directory and once it's completed then it will delete the lock file. Now next time onwards, it will check weather the lock file is there or not. If lock file is there that means the previous cron is not completed and it will not allow the cron to overlap the previous one.

In this scenario, the lock file is there into the storage/framework/ directory so that the cron is not working

Lock file looks like: schedule-random_string For example: schedule-0bfdb7f0bc14b27d84c7d6f2a2528e85b0847fc6

To Fix: Remove or rename the lock file (storage/framework/schedule-0bfdb7f0bc14b27d84c7d6f2a2528e85b0847fc6)

like image 1
Umang Soni Avatar answered Nov 11 '22 06:11

Umang Soni