我有一个名为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函数。然而,问题是,它调用运行函数每次我使用:
php artisan migrate
有人知道为什么会发生这种情况吗?
提前感谢!
EDIT:所以我找到了它调用调度函数的地方,
vendor'laravel'framework'src'Illuminate'Foundation'Console'Kernel.php
这调用 defineconsolesschedule ()函数,该函数又运行$ 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');
}
}
希望这对某人有所帮助!