如何在包中安排Artisan命令? [英] How to schedule Artisan commands in a package?

查看:55
本文介绍了如何在包中安排Artisan命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含Artisan命令的软件包.我已经通过服务提供商向Artisan注册了这些命令,如下所示:

I have a package that contains Artisan commands. I’ve registered these commands with Artisan via my service provider like so:

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    // Register Amazon Artisan commands
    $this->commands([
        'App\Marketplace\Amazon\Console\PostProductData',
        'App\Marketplace\Amazon\Console\PostProductImages',
        'App\Marketplace\Amazon\Console\PostProductInventory',
        'App\Marketplace\Amazon\Console\PostProductPricing',
    ]);
}

但是,这些命令需要安排为每天运行.

However, these commands need to be scheduled to run daily.

我知道 app/Console/Kernel.php 中有 schedule()方法,您可以在其中注册命令及其频率,但是如何在而是我包裹的服务提供商?

I know in app/Console/Kernel.php there is the schedule() method where you can register commands and their frequency, but how can I schedule commands in my package’s service provider instead?

推荐答案

花了很多时间进行调试和阅读Laravel的源代码才能弄清楚这一点,但事实证明这很简单.诀窍是要等到应用程序启动后才调度命令,因为那是Laravel定义 Schedule 实例然后在内部调度命令的时候.希望这可以节省一些人的繁琐调试工作!

It took a lot of debugging and reading through Laravel's source to figure this out, but it turned out to be pretty simple. The trick is to wait until after the Application has booted to schedule the commands, since that is when Laravel defines the Schedule instance and then schedules commands internally. Hope this saves someone a few hours of painful debugging!

use Illuminate\Support\ServiceProvider;
use Illuminate\Console\Scheduling\Schedule;

class ScheduleServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->booted(function () {
            $schedule = $this->app->make(Schedule::class);
            $schedule->command('some:command')->everyMinute();
        });
    }

    public function register()
    {
    }
}

这篇关于如何在包中安排Artisan命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆