为什么 php 工匠:调度运行命令不执行工匠命令?



我运行php artisan:schedule run命令,它显示消息说命令正在运行。但是,什么也没发生(命令触发的事件),它不起作用,它能是什么?

内核.php

<?php
namespace AppConsole;
use AppConsoleCommandsCheckPayments;
use AppConsoleCommandsCheckSubscriptions;
use AppConsoleCommandsDeleteOperationalLogs;
use AppConsoleCommandsGenerateInvoices;
use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
CheckPayments::class,
GenerateInvoices::class,
CheckSubscriptions::class,
DeleteOperationalLogs::class
];
protected function schedule(Schedule $schedule)
{
$schedule->command(CheckPayments::class, ['--force'])->everyMinute();
$schedule->command(GenerateInvoices::class, ['--force'])->everyMinute();
$schedule->command(CheckSubscriptions::class, ['--force'])->everyMinute();
$schedule->command(DeleteOperationalLogs::class, ['--force'])->everyMinute();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}

运行php artisan schedule后:

Running scheduled command: "C:xamppphpphp.exe" "artisan" payments:check --force > "NUL" 2>&1
Running scheduled command: "C:xamppphpphp.exe" "artisan" subscriptions:check --force > "NUL" 2>&1
Running scheduled command: "C:xamppphpphp.exe" "artisan" invoices:generate --force > "NUL" 2>&1
Running scheduled command: "C:xamppphpphp.exe" "artisan" logs:delete --force > "NUL" 2>&1

注意:如果我单独运行命令,它可以工作,例如:php 工匠付款:检查

要在调度程序中使用命令,可以使用它的签名或类名。AppConsoleCommands中的每个命令都具有以下内容:

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = "example:command";

一旦命令导入到AppConsoleKernel.php中,在protected $commands = [];数组中,它可以在schedule()函数中使用,但使用ExampleCommand::class是不正确的:

protected function schedule(Schedule $schedule){
$schedule->command("example:command --force")->everyMinute();
$schedule->command(ExampleCommand::class, ["--force"])->everyMinute();
...
}

这里的主要问题似乎是--force选项引发以下错误:

"--force"选项不存在

许多现有的 Laravel 命令都设置了--force标志,从文档中执行以下操作:

强制操作在生产中运行时运行。

许多工匠命令在运行命令时会提示输入,例如php artisan migrate,它询问

是否确实要在生产环境中运行此命令?

由于调度程序是非交互式的,因此--force标志将覆盖此提示为"是"。话虽如此,您需要自己定义和处理该选项:

protected $signature = "example:command {--force}";
public function handle(){
$force = $this->option("force");
if(env("APP_ENV", "production") == "production" && !$force){
if(!$this->ask("Are you sure you want to run this in production?")){
return false; // or dd();, etc.
}
} 
}

这是未经测试的,但如果在.env中设置了APP_ENV=production,并且$forcenull(如果未包含--force则为默认值),则它将提示确认,并在回答"否"时退出。

试试这个

php artisan schedule:work

最新更新