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?
In your package service provider, do this:
/** @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 boot() method of the service provider do this:
$this->commands($this->moreCommands);
Very good question, btw. Had me searching through the Laravel API docs to find the answer, and when I found it I implemented it in one of my own packages.
You certainly can, all through the power of some basic object-oriented programming!
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
{
//
}
schedule
methodSince 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);
//
}
}
Now you may add your own scheduled tasks per normal.
$schedule->command('')->daily();
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!
Some things to take into consideration with this though: