所以我有一个名为iCron的界面
namespace App\Console\CronScripts; interface iCron{ public static function run($args); }
我还有一个使用这个名为UpdateStuff的类
class UpdateStuff implements iCron{ public static function run($args = NULL){ //I do api calls here to update my records echo "Begin Updating Stuff"; } }
所以在内核中我有:
use App\Console\CronScripts\UpdateStuff; class Kernel extends ConsoleKernel{ protected $commands = []; protected function schedule(Schedule $schedule){ $schedule->call(UpdateStuff::run(NULL))->dailyAt('23:00'); } }
据说我想在每天晚上11点调用UpdateStuff的run函数.但问题是,每次我使用它时都会调用run函数:
PHP artisan migrate
任何人都有任何想法为什么会这样?
提前致谢!
vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.PHP
这将调用defineConsoleSchedule()函数,该函数反过来运行$this-> schedule($schedule);然后由于某种原因,UpdateStuff :: run($args)正在执行,即使它不是11PM
解决方法
我想到了!因此对于任何困惑的人来说,cron调度程序需要一个Closure或一个指向没有参数的静态函数的字符串.这是我想出的:
class Kernel extends ConsoleKernel{ protected $commands = []; protected function schedule(Schedule $schedule){ //This calls the run function,but with no parameters $schedule->call("App\Console\CronScripts\UpdateStuff::run")->dailyAt('23:00'); //If you need parameters you can use something like this $schedule->call(function(){ App\Console\CronScripts\UpdateStuff::run(['key' => 'value']); })->dailyAt('23:00'); } }
希望这有助于某人!