PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 1, 2022

[FIXED] Laravel 5 Package Scheduled Tasks

 February 01, 2022     laravel, laravel-5     No comments   

Issue

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?

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


Solution

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

Step 1: Create Your Package's "Kernel" Console Class

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
{
    //
}

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

        //
    }
}

Step 3: Add your scheduled tasks

Now you may add your own scheduled tasks per normal.

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

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!

Some things to take into consideration with this though:

  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.


Answered By - kai_desu
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing