Laravel-modules: How do I schedule a command on each Module?

Created on 6 Jul 2019  路  2Comments  路  Source: nWidart/laravel-modules

I created my Command and registered at the service provider $command variable from my module, but how I schedule it? Normally I will put it at the Kernel.php file inside Console folder. But how to do it if I want a different schedule for each module? Something like Kernel.php inside of each module but I don't really know how to do that.

Most helpful comment

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()
    {
    }
}

Then in module.json register your schedule service provider

{
    "name": "Module",
    "alias": "module",
    "description": "",
    "keywords": [],
    "active": 1,
    "order": 0,
    "providers": [
        "Module\\Providers\\ResourceServiceProvider",
        "Module\\Providers\\EventServiceProvider",
        "Module\\Providers\\ScheduleServiceProvider"
    ],
    "aliases": {},
    "files": [],
    "requires": []
}

All 2 comments

I haven't done this before, but you can schedule each command individually so you can do it that way.
If it's a fixed schedule par module you can develop something to handle this automatically

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()
    {
    }
}

Then in module.json register your schedule service provider

{
    "name": "Module",
    "alias": "module",
    "description": "",
    "keywords": [],
    "active": 1,
    "order": 0,
    "providers": [
        "Module\\Providers\\ResourceServiceProvider",
        "Module\\Providers\\EventServiceProvider",
        "Module\\Providers\\ScheduleServiceProvider"
    ],
    "aliases": {},
    "files": [],
    "requires": []
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  4Comments

sandeepk2304 picture sandeepk2304  路  3Comments

morningmemo picture morningmemo  路  3Comments

alexandretaz picture alexandretaz  路  3Comments

guirociozanini picture guirociozanini  路  4Comments