所以我有一个名为iCron的界面
namespace App\Console\CronScripts;
interface iCron{
public static function run($args);
}
Run Code Online (Sandbox Code Playgroud)
我也有一个使用这个的类叫做UpdateStuff
class UpdateStuff implements iCron{
public static function run($args = NULL){
//I do api calls here to update my records
echo "Begin Updating Stuff";
}
}
Run Code Online (Sandbox Code Playgroud)
所以在内核中,我有:
use App\Console\CronScripts\UpdateStuff;
class Kernel extends ConsoleKernel{
protected $commands = [];
protected function schedule(Schedule $schedule){
$schedule->call(UpdateStuff::run(NULL))->dailyAt('23:00');
}
}
Run Code Online (Sandbox Code Playgroud)
如我所说,我想每天晚上11点调用UpdateStuff的运行功能。但是问题是,每次使用时都会调用run函数:
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
任何人都知道为什么会这样吗?
提前致谢!
编辑:所以我发现它在哪里调用调度功能,
vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php
Run Code Online (Sandbox Code Playgroud)
这将调用defineConsoleSchedule()函数,该函数依次运行$ this-> schedule($ schedule);。然后由于某种原因,即使不是11 PM,UpdateStuff::run($ args)仍在执行
我想到了!因此,对于任何感到困惑的人来说,cron 调度程序需要一个闭包或一个指向不带参数的静态函数的字符串。这是我想出的:
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');
}
}
Run Code Online (Sandbox Code Playgroud)
希望这对某人有帮助!
| 归档时间: |
|
| 查看次数: |
536 次 |
| 最近记录: |