10
$schedule->call(function () 
        {
            error_log("Line Schedule 1:Start");
            //Send Email
            error_log("Line Schedule 1:End");

        })->everyFiveMinutes()->name('event_name:1')->withoutOverlapping();


        $schedule->call(function () 
        {
          error_log("Line Schedule 2:Start");
           //Send Email
          error_log("Line Schedule 2:End");
        })->everyFiveMinutes()->name('event_name:2')->withoutOverlapping();
        $schedule->call(function () 
        {
          error_log("Line Schedule 3:Start");
              //Send Email
          error_log("Line Schedule 3:End");
        })->everyFiveMinutes()->name('event_name:3')->withoutOverlapping();

i run these schulders using command php artisan schedule:run and i am running many instances in parallel. and my logs file says that schulder 2 is starting second time even its previous instance has not completed it yet .

[01-Jan-2016 11:30:08 UTC] Line Schedule 1:Start
[01-Jan-2016 11:30:11 UTC] Line Schedule 2:Start
[01-Jan-2016 11:30:13 UTC] Line Schedule 3:Start
[01-Jan-2016 11:30:15 UTC] Line Schedule 1:End
[01-Jan-2016 11:30:15 UTC] Line Schedule 2:Start
[01-Jan-2016 11:30:17 UTC] Line Schedule 2:End
[01-Jan-2016 11:30:17 UTC] Line Schedule 3:Start
[01-Jan-2016 11:30:19 UTC] Line Schedule 3:End
[01-Jan-2016 11:30:21 UTC] Line Schedule 2:End
[01-Jan-2016 11:30:21 UTC] Line Schedule 3:Start
[01-Jan-2016 11:30:22 UTC] Line Schedule 3:End
[01-Jan-2016 11:30:25 UTC] Line Schedule 3:End
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
Awais Mushtaq
  • 633
  • 1
  • 10
  • 23
  • What do you want to do with `withoutOverlapping` method? – Ozan Kurt Jan 01 '16 at 11:07
  • i am using 3 long time taking schedulers lets say S1,S2 and S3 and my server goes down because of these schedulers .i do not want to run any scheduler lets say S1 if previous one(S1) is not completed. – Awais Mushtaq Jan 01 '16 at 11:16

2 Answers2

13

Just name your task with a call to name() and chain the methods that define when your task should be run.

$schedule->call(function () {
  //Some Code
})->everyFiveMinutes()
->name('some_name')
->withoutOverlapping();

For anonymous functions the name is required to prevent overlapping.

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • [01-Jan-2016 11:30:08 UTC] Line Schedule 1:Start [01-Jan-2016 11:30:11 UTC] Line Schedule 2:Start [01-Jan-2016 11:30:13 UTC] Line Schedule 3:Start [01-Jan-2016 11:30:15 UTC] Line Schedule 1:End [01-Jan-2016 11:30:15 UTC] Line Schedule 2:Start [01-Jan-2016 11:30:17 UTC] Line Schedule 2:End if you see my logs , Schedule 2 is staring second time even previously it is not completed yet.I do not see overlapping is working . i have logged in start and end of my each schedule . – Awais Mushtaq Jan 01 '16 at 11:37
  • i do not see overlapping working as my scheduler 2 is starting even its previous instance is not completed yet . i have commented log in above comment – Awais Mushtaq Jan 01 '16 at 11:43
  • How are you running them? By running "artisan schedule:run" twice in parallel? – jedrzej.kurylo Jan 01 '16 at 11:47
  • yes i am testing on windows bu running command "php artisan schedule:run" in parallel. i am running many instances to test . actually my server goes down because of scheduling – Awais Mushtaq Jan 01 '16 at 11:49
  • I just ran the above code, it works for me. When a task is running already, I'm getting "No scheduled commands are ready to run" when I run the second scheduler. Could you update the question with your current schedule? Please also run the scheduler twice and paste output you're getting from both calls – jedrzej.kurylo Jan 01 '16 at 11:50
  • Are you running multiple instances of "schedule:run" on the same server or different ones? – jedrzej.kurylo Jan 01 '16 at 12:02
  • i am running them on same server . i hope you have understood my problem . which is running of schedule 2 second time even its previous instance has not done yet . – Awais Mushtaq Jan 01 '16 at 12:05
  • Ok, I've found the issue. When you run "schedule:run", first your Kernel's "schedule()" method is run to define the schedule and then, based on that schedule tasks are run. When you run first instance of "schedule:run", it schedules S1/S2/S3 to be run and then starts executing S1 task. When you run second instance of "schedule:run", it sees that S1 is being run, so it schedules S2/S3 to be run. When first instance finishes executing S1, it executes S2 because it's been scheduled to run, even though the second instance is already executing that. I'll submit a pull reuqest to Laravel to fix it. – jedrzej.kurylo Jan 01 '16 at 12:18
  • and can you guide me how i can prevent my server from getting down ? i have some different logic in these 3 schedulers instead of sending email.all these tasks are quite time taking . – Awais Mushtaq Jan 01 '16 at 12:53
  • I can't really tell what's causing servers to fail without seing the code and what's going on on the servers when you run it. If the operations are heavy it might just server that is too weak to handle them. Or overlapping tasks cause some kind of locks that, I can't really tell. – jedrzej.kurylo Jan 01 '16 at 13:24
  • The pull request has been just merged to Laravel's 5.2 branch - https://github.com/laravel/framework/pull/11646 – jedrzej.kurylo Jan 01 '16 at 17:38
6

because these are different tasks, withoutOverlapping works when there is a task scheduled everyFiveMinutes for example, and takes more than 5 minutes to finish so it will not start another instance of the same task until the old one is finished,

abdelrahman khedr
  • 366
  • 1
  • 3
  • 11