Define scheduling tasks on modules (nwidart/laravel-modules:7.3) in Laravel 6
1. Generate a new module: blog.
PS E:\wwwroot\Laravel6-modules-demo> php artisan module:make blog created : e:\wwwroot\Laravel6-modules-demo\modules/blog/module.json created : e:\wwwroot\laravel6-modules-demo\modules/blog/routes/web.php created : e:\wwwroot\Laravel6-modules-demo\modules/blog/routes/api.php created : e:\wwwroot\laravel6-modules-demo\modules/blog/resources/views/index.blade.php created : e:\wwwroot\laravel6-modules-demo\modules/blog/resources/views/layouts/master.blade.php created : e:\wwwroot\Laravel6-modules-demo\modules/blog/config/config.php created : e:\wwwroot\laravel6-modules-demo\modules/blog/composer.json created : e:\wwwroot\laravel6-modules-demo\modules/blog/resources/assets/js/app.js created : e:\wwwroot\laravel6-modules-demo\modules/blog/resources/assets/sass/app.scss created : e:\wwwroot\laravel6-modules-demo\modules/blog/webpack.mix.js created : e:\wwwroot\laravel6-modules-demo\modules/blog/package.json created : e:/wwwroot/laravel6-modules-demo/modules/blog/database/seeders/blogDatabaseSeeder.php created : e:/wwwroot/laravel6-modules-demo/modules/blog/providers/blogserviceprovider.php created : e:/wwwroot/laravel6-modules-demo/modules/blog/providers/routeserviceprovider.php created : e:/wwwroot/laravel6-modules-demo/modules/blog/http/controllers/blogcontroller.php module[Blog]Created successfully.
2. By default, the module class will not load automatically. You can load your modules automatically using PSR-4. Edit composer.json. as shown in Figure 1
"autoload": {
"PSR-4": {
"app\": "app/",
"modules\": "modules/"
}
},
3. Tip: Don’t forget to run composer dump-autoload. as shown in Figure 2
PS E:\wwwroot\Laravel6-modules-demo> composer dump-autoload GENERATING OPTIMIZED AUTOLOAD FILES > Illuminate\Foundation\ComposerScripts::PostAutoLoadDump > @php artisan package:discover --ansi Discovered Package: Facade/Ignition Discovered Package: FideLoper/Proxy Discovered Package: Laravel/Tinker Discovered Package: Nesbot/Carbon Discovered Package: NUNOMADURO/COLLISION Discovered Package: Nwidart/Laravel-Modules Package manifested successfully. GENERATED OPTIMIZED AUTOLOAD FILES CONTAINING 4360 CLASSES
4. Generate a given console command for the specified module.
PS E:\wwwroot\Laravel6-modules-demo> php artisan module:make-command createPostCommand blog created : e:/wwwroot/laravel6-modules-demo/modules/blog/console/createpostcommand.php
5. Edit CreatePostCommand.php, mainly modify the $name attribute and the handle method
<?php
namespace modules\blog\console;
use illuminate\console\command;
use symfony\component\console\input\inputOption;
use symfony\component\console\input\inputargument;
Class CreatePostCommand Extends command
{
/**
* The console command name.
;
* @var string
*/
protected $name =blog:create-post;
/**
* The console command description.
;
* @var string
*/
protected $description =command description.;
/**
* Create a new command instance.
;
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
;
* @return mixed
*/
public function handle()
{
file_put_contents(storage_path() ./logs/modules-blog-console-createPostCommand-handle-. microtime(true) .-. mt_rand() ..txt, print_r([], true), file_append | lock_ex);
}
/**
* Get the console command arguments.
;
* @return array
*/
protected function getArguments()
{
return[
[Example, inputArgument::required,an example argument.]#ATFP_CLOSE_Translate_span#,
];
}
/**
* Get the console command options.
;
* @return array
*/
protected function getOptions()
{
return[
[Example, null, inputOption::value_optional,an example option., null] #atfp_close_translate_span#,
];
}
}
6. Register the command to register the command using the Laravel method called Commands available in the service provider class.
/**
* Register the Service Provider.
;
* @return void
*/
public function register()
{
$this->app->register(RouteServiceProvider::Class);
$this->commands([
\modules\blog\Console\CreatePostCommand::Class,
]#atfp_close_translate_span#);
}
7. To view the list of all available Artisan commands, you can use the list command: confirm that blog:create-post exists. as shown in Figure 3
8. Execute: php artisan blog:create-post When an error is reported: not enough arguments (missing: “example”).
PS E:\wwwroot\Laravel6-modules-demo> php artisan blog:create-post not enough arguments (missing: "example").
9. Edit CreatePostCommand.php and delete the method getArguments
10. Execute again: php artisan blog:create-post, no more errors. To view the execution result, there is an output log file: modules-blog-console-createpostcommand-handle-1672371310.181-805184791.txt . as shown in Figure 4
11. Execution: php artisan schedule:run, response: no scheduled command is ready to run.
PS E:\wwwroot\Laravel6-modules-demo> php artisan schedule:run No scheduled commands are ready to run.
12. Refer to task scheduling – ARTISAN command scheduling, define new scheduling tasks in the Schedule method of the App\Console\Kernel class
/**
* define the applicationS command schedule.
;
* @param \illuminate\console\scheduling\schedule $schedule
* @return void
*/
protected function schedule(schedule $schedule)
{
// $Schedule->Command(Inspire)
// ->hourly();
$Schedule->Command(blog:create-post)->EveryMinute(); // Perform a task every minute
}
13. Execute the schedule:run command, the execution is successful, and the corresponding log file is successfully generated. as shown in Figure 5
PS E:\wwwroot\Laravel6-modules-demo> php artisan schedule:run running scheduled command: "C:\php-7.4.27\php.exe" "artisan" blog:create-post > "nul" 2>∓1
14. However, since command blog:create-post is written in the module, the schedule for scheduling this command should also be written in the corresponding module to be a more suitable implementation.
15. Reference: https://stackoverflow.com/questions/30456737/how-to-schedule-artisan-commands-in-a-package , both options are feasible. Restore the implementation of step 12.
/**
* Boot the Application Events.
;
* @return void
*/
public function boot()
{
$this->registerTranslations();
$this->registerConfig();
$this->registerViews();
$this->registerFactories();
$this->loadmigrationsFrom(module_path($this->moduleName,database/migrations));
tiveting
$this->app->booted(function () {
$schedule = $this->app->make(schedule::class);
$Schedule->Command(blog:create-post)->everyMinute();
});
*/
// In Laravel 6.10 and above:
$this->CallAfterResolving(schedule::class, function (schedule $schedule) {
$Schedule->Command(blog:create-post)->everyMinute();
});
}
PS E:\wwwroot\Laravel6-modules-demo> php artisan schedule:run running scheduled command: "C:\php-7.4.27\php.exe" "artisan" blog:create-post > "nul" 2>∓1 PS E:\wwwroot\Laravel6-modules-demo> php artisan schedule:run running scheduled command: "C:\php-7.4.27\php.exe" "artisan" blog:create-post > "nul" 2>∓1




