Laravel 5 - Task schedule withoutOverlapping not working

前端 未结 4 2046
予麋鹿
予麋鹿 2021-02-20 05:33

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

$schedule->call(function() {
   // do something here..
})->everyMinute();
相关标签:
4条回答
  • 2021-02-20 05:50

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

    0 讨论(0)
  • 2021-02-20 05:55

    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.

    0 讨论(0)
  • 2021-02-20 06:01

    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

    0 讨论(0)
  • 2021-02-20 06:08
    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)
    
    0 讨论(0)
提交回复
热议问题