[英]Laravel - Send custom parameter to scheduled tasks (commands)
Currently I have 4 different commands in "app/Console/Commands/", and I set them all up in app/Kernel.php, like this:目前我在“app/Console/Commands/”中有 4 个不同的命令,我将它们全部设置在 app/Kernel.php 中,如下所示:
$schedule->command('command:start-imports-group-a')->everyFiveMinutes()->runInBackground()->withoutOverlapping();
$schedule->command('command:start-imports-group-b')->everyFiveMinutes()->runInBackground()->withoutOverlapping();
$schedule->command('command:start-imports-group-c')->everyFiveMinutes()->runInBackground()->withoutOverlapping();
$schedule->command('command:start-imports-group-d')->everyFiveMinutes()->runInBackground()->withoutOverlapping();
In my command php files (eg StartImportsGroupA.php, StartImportsGroupB.php, etc.) they are all calling the same function, but I'm simply setting a variable in each to change the "group" value. In my command php files (eg StartImportsGroupA.php, StartImportsGroupB.php, etc.) they are all calling the same function, but I'm simply setting a variable in each to change the "group" value. For example:
例如:
StartImportsGroupA.php StartImportsGroupA.php
public function handle()
{
$group = 'a';
$this->startImports($group);
}
StartImportsGroupB.php StartImportsGroupB.php
public function handle()
{
$group = 'b';
$this->startImports($group);
}
...and so on. ...等等。
Is there an better way to do this?有没有更好的方法来做到这一点? Eg pass the "group" parameter to the command?
例如将“组”参数传递给命令? That way I won't need 4 different command files to simply change that one $group variable's value.
这样我就不需要 4 个不同的命令文件来简单地更改一个 $group 变量的值。
You can use second parameter as array of options for your command:您可以将第二个参数用作命令的选项数组:
$schedule->command(StartImportsGroupCommand::class, ['--group=a'])
->everyFiveMinutes()
->runInBackground()
->withoutOverlapping();
Do not forget to describe this option --group
in command code.不要忘记在命令代码中描述这个选项
--group
。
Thanks SpinyMan , although I had to do some more research for the remainder of the answer.谢谢SpinyMan ,尽管我不得不为其余的答案做更多的研究。 The full answer is this:
完整的答案是这样的:
Working Example:工作示例:
In app/Console/Commands/StartImports.php file:在 app/Console/Commands/StartImports.php 文件中:
protected $signature = 'command:start-imports {group}';
public function handle()
{
$group = $this->argument('group');
}
In app/Console/Kernel.php file:在 app/Console/Kernel.php 文件中:
$schedule->command('command:start-imports', ['a'])->everyFiveMinutes()->runInBackground()->withoutOverlapping();
$schedule->command('command:start-imports', ['b'])->everyFiveMinutes()->runInBackground()->withoutOverlapping();
$schedule->command('command:start-imports', ['c'])->everyFiveMinutes()->runInBackground()->withoutOverlapping();
$schedule->command('command:start-imports', ['d'])->everyFiveMinutes()->runInBackground()->withoutOverlapping();
I hope this helps anyone else who needs this!我希望这可以帮助其他需要这个的人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.