re-run jobs in laravel scheduler

1.1k views Asked by At

I want to re-run jobs in laravel Scheduler in case of failure , assume the following :
Kernal.php

protected function schedule(Schedule $schedule)
{
    $this->generateData($schedule);

}

protected function generateData($schedule){

   $schedule->command('My Command')
   ->monthly()
   ->after(function ($schedule){
     $command = DB::table('commands')
     ->where("name","My Command")
     ->orderBy('id', 'desc')
     ->first();
     if(!$command->succeeded){
       echo "task not finished";
       $this->generateData($schedule);
     }
     else{
       echo "task finished";
       return;
     }
   });
 }

this command sometimes fails, on after function I check if the command fails or not, then I try to re-execute it again, but this didn't work and I got the following error :
[ErrorException] Missing argument 1 for App\Console\Kernel::App\Console{closure}()

any suggestions ?

1

There are 1 answers

0
Erik Dohmen On

You're setting the $schedule the wrong way in the anonymous function, use the 'use' statement like this:

protected function generateData($schedule){

  $schedule->command('My Command')
  ->monthly()
  ->after(function() use ($schedule){
    $command = DB::table('commands')
    ->where("name","My Command")
    ->orderBy('id', 'desc')
    ->first();
    if(!$command->succeeded){
      echo "task not finished";
      $this->generateData($schedule);
    }
    else{
      echo "task finished";
      return;
    }
  });
}