Laravel 5包计划任务 [英] Laravel 5 Package Scheduled Tasks

查看:81
本文介绍了Laravel 5包计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含一些计划任务的软件包-是否可以注册/发布它们而不影响已经设置了计划任务的基本应用程序?

I'm developing a package that has some scheduled tasks - is there way of registering / publishing them without affecting the base applications already set scheduled tasks?

我不想覆盖App/Console/Kernel.php,因为基本应用程序可能已经拥有了自己的预定任务等.

I don't want to overwrite the App/Console/Kernel.php as the base application may already have it's own scheduled tasks etc.

推荐答案

通过某些基本的面向对象编程的力量,您当然可以!

You certainly can, all through the power of some basic object-oriented programming!

让我们在您程序包的Console目录中创建一个Kernal类,我们将在其中扩展App\Console\Kernel.

Let's create a Kernal class within your package's Console directory where we will be extending App\Console\Kernel.

<?php
namespace Acme\Package\Console;

use App\Console\Kernel as ConsoleKernel;
use Illuminate\Console\Scheduling\Schedule;

class Kernel extends ConsoleKernel
{
    //
}

步骤2:添加schedule方法

由于我们正在扩展App Console内核,我们将要添加相关的schedule方法并调用其父类的实现.这样可以确保所有先前计划的任务都能完成.

Step 2: Add the schedule method

Since we are extending the App Console Kernel, we'll want to add the relevant schedule method and call the parent class' implementation of it. This will ensure that any previously scheduled tasks carry through.

<?php
namespace Acme\Package\Console;

use App\Console\Kernel as ConsoleKernel;
use Illuminate\Console\Scheduling\Schedule;

class Kernel extends ConsoleKernel
{
    /**
     * Define the package's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        parent::schedule($schedule);

        //
    }
}

第3步:添加您计划的任务

现在,您可以按常规添加自己的计划任务.

Step 3: Add your scheduled tasks

Now you may add your own scheduled tasks per normal.

$schedule->command('')->daily();

第4步:注册!

我们要将该类绑定到容器,并在包的服务提供者的register方法中将其make绑定:

Step 4: Register It!

We'll want to bind the class to the container, and make it within our package's service provider's register method:

$this->app->singleton('acme.package.console.kernel', function($app) {
    $dispatcher = $app->make(\Illuminate\Contracts\Events\Dispatcher::class);
    return new \Acme\Package\Console\Kernel($app, $dispatcher);
});

$this->app->make('acme.package.console.kernel');


这应该是全部!


That should be all that's required!

与此相关的一些事情:

  1. 首先要确保您的软件包具有这些捆绑的任务.开发人员不喜欢惊讶(特别是当它涉及任务自动在服务器上运行时).
  2. 关于第一点,并不是每个人都在他们的服务器上设置了所需的cronjob.他们需要先执行此操作,然后程序包的任务才能自动运行.
  3. 提供配置选项以禁用自动执行软件包的任务,并提供有关开发人员如何根据自己的需要进行注册的文档.
  1. Be up-front that your package has these bundled tasks. Developers don't like surprises (especially when it concerns tasks being automatically ran on their server).
  2. Along with the first point, not every one will have the required cronjob set up on their server. They'll need to do this before your package's tasks will run automatically.
  3. Provide a config option to disable the package's tasks from being auto registered, and documentation on how developers can register it themselves based on their own needs.

这篇关于Laravel 5包计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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