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

僤鯓⒐⒋嵵緔 提交于 2019-12-11 09:02:26

问题


I run the php artisan: schedule run command and it shows the messages saying that the commands are running. However, nothing happens (the events the commands trigger), it did not work, what can it be?

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');
        }
    }

After run 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

Note: if I run the commands separately it works, for example: php artisan payments: check


回答1:


To use a Command in your scheduler, you can use it's signature, or it's Classname. Each Command in App\Console\Commands has the following:

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

Once the Command is imported into App\Console\Kernel.php, in the protected $commands = []; array, it can be used in the schedule() function, but using ExampleCommand::class isn't correct:

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

The main issue here seems to be with the --force option throwing the following error:

"--force" option does not exist

Many of the existing Laravel commands have the --force flag set, which, from the documentation does the following:

Force the operation to run when in production.

Many artisan commands prompt for input when you run a command, like php artisan migrate, that asks

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

Since the scheduler is non-interactive, the --force flag will override this prompt to "Yes". That all being said, you need to define and handle the option yourself:

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

This is untested, but if APP_ENV=production is set in .env, and $force is null (default if --force isn't included), then it will prompt for confirmation, and exit if "No" is answered.



来源:https://stackoverflow.com/questions/55498576/why-the-php-artisan-schedule-run-command-does-not-execute-the-artisan-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!