Laravel 5 - Task schedule withoutOverlapping not working

蹲街弑〆低调 提交于 2020-12-02 06:55:19

问题


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


回答1:


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.




回答2:


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()




回答3:


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




回答4:


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

For E.g:
$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 E.g: schedule-0bfdb7f0bc14b27d84c7d6f2a2528e85b0847fc6

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


来源:https://stackoverflow.com/questions/31064236/laravel-5-task-schedule-withoutoverlapping-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!