Schedule tasks from a module
To run a task from the Blog module on a schedule, override configureSchedules() in the module service provider. You need an enabled Blog module and a command to schedule (see Add Artisan commands to a module).
-
Uncomment
configureSchedules()inModules/Blog/app/Providers/BlogServiceProvider.phpand register your scheduled command. The generated provider already importsIlluminate\Console\Scheduling\Schedule.Modules/Blog/app/Providers/BlogServiceProvider.php protected function configureSchedules(Schedule $schedule): void{$schedule->command('blog:prune-old')->daily();}Use
protectedorpublicvisibility (notprivate) with theScheduleparameter. -
Check the result.
Terminal window php artisan schedule:list0 0 * * * php artisan blog:prune-old .......... Next Due: <time>
Schedule a command that takes arguments
Section titled “Schedule a command that takes arguments”Pass command arguments as an array:
$schedule->command('module:prune', ['Blog'])->daily();When the schedule is built
Section titled “When the schedule is built”configureSchedules() skips web requests. It runs only when Laravel resolves Schedule (such as during schedule:run or schedule:list), after every service provider has booted. Disabling the module removes its scheduled tasks.
Related
Section titled “Related”- Module service provider: methods:
registerCommandSchedules()andconfigureSchedules(). - Add Artisan commands to a module
- Prune module models