Laravel 5 scheduled tasks

I am developing a package with some scheduled tasks - is there a way to register / publish them without affecting the underlying applications already defined by the scheduled tasks?

I do not want to overwrite App/Console/Kernel.php, since the base application may already have its own scheduled tasks, etc.

+5
source share
3 answers

Of course, you can, in the entirety of some basic object-oriented programming!

Step 1: Create Your Console Kernel Class

Let me create a Kernal class inside your Console directory for the package in which we will expand App\Console\Kernel.

<?php
namespace Acme\Package\Console;

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

class Kernel extends ConsoleKernel
{
    //
}

Step 2: add a method schedule

, . .

<?php
namespace Acme\Package\Console;

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

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

        //
    }
}

3:

.

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

4: !

make register:

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

, !

, :

  • , . ( , ).
  • , cronjob, . , .
  • , .
+10

, , , - , , .

, , , cron:

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

php artisan schedule:run , .

, - schedule:run, , , cron, - packageSchedule:run.

, , - . cron Kernel.php, , , , , , , , , , / .

, , , - laravel.

P.S. Kernel.php , , , , , .

-, , Kernel.php .

+1

At your package provider, do the following:

/** @var array list of commands to be registered in the service provider */
protected $moreCommands = [
    \My\Package\CommandOne::class,
    \My\Package\CommandTwo::class,
    \My\Package\CommandThree::class,
];

Then, in your service provider's boot () method, do the following:

$this->commands($this->moreCommands);

Very good question, by the way. If I searched Laravel API docs to find the answer, and when I found it, I implemented it in one of my own packages.

-1
source

All Articles