为什么 php artisan: schedule 运行 命令不执行 artisan 命令?

Why the php artisan: schedule run command does not execute the artisan commands?

I 运行 php artisan: schedule 运行 命令,它显示消息说命令是 运行ning。但是,没有任何反应(命令触发的事件),它不起作用,这会是什么?

Kernel.php

<?php

    namespace App\Console;

    use App\Console\Commands\CheckPayments;
    use App\Console\Commands\CheckSubscriptions;
    use App\Console\Commands\DeleteOperationalLogs;
    use App\Console\Commands\GenerateInvoices;
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel 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:\xampp\php\php.exe" "artisan" payments:check --force > "NUL" 2>&1
    Running scheduled command: "C:\xampp\php\php.exe" "artisan" subscriptions:check --force > "NUL" 2>&1
    Running scheduled command: "C:\xampp\php\php.exe" "artisan" invoices:generate --force > "NUL" 2>&1
    Running scheduled command: "C:\xampp\php\php.exe" "artisan" logs:delete --force > "NUL" 2>&1

注意:如果我 运行 命令 分开 它有效,例如: php artisan payments : 检查

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

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

一旦命令被导入App\Console\Kernel.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" option does not exist

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

Force the operation to run when in production.

许多 artisan 命令在您 运行 一个要求

的命令时提示输入,例如 php artisan migrate

Are you sure you want to run this command in production?

由于调度程序是 non-interactive,--force 标志会将此提示覆盖为 "Yes"。综上所述,您需要自己定义和处理选项:

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.
    }
  } 
}

这是未经测试的,但如果 APP_ENV=production 设置在 .env 中,并且 $forcenull(如果不包括 --force,则默认) , 然后它会提示确认,如果回答 "No" 则退出。