在 laravel 调度程序中重新运行作业

Posted

技术标签:

【中文标题】在 laravel 调度程序中重新运行作业【英文标题】:re-run jobs in laravel scheduler 【发布时间】:2018-02-24 16:56:47 【问题描述】:

如果失败,我想,假设如下: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;
     
   );
 

这个命令有时会失败,在函数之后我检查命令是否失败,然后我尝试再次重新执行它,但这不起作用,我收到以下错误: [ErrorException] App\Console\Kernel::App\Consoleclosure() 缺少参数 1

有什么建议吗?

【问题讨论】:

【参考方案1】:

您在匿名函数中以错误的方式设置 $schedule,请像这样使用“使用”语句:

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

【讨论】:

以上是关于在 laravel 调度程序中重新运行作业的主要内容,如果未能解决你的问题,请参考以下文章