在 Laravel 6 中的模块(nWidart/laravel-modules:7.3)上定义调度任务
1、生成一个新模块: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、默认情况下,模块类不会自动加载。 你可以使用 psr-4 自动加载你的模块。编辑 composer.json。如图1
"autoload": { "psr-4": { "App\\": "app/", "Modules\\": "Modules/" } },
3、提示:不要忘记之后运行 composer dump-autoload。如图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 manifest generated successfully. Generated optimized autoload files containing 4360 classes
4、为指定模块生成给定的控制台命令。
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、编辑 CreatePostCommand.php,主要修改 $name 属性与 handle 方法
<?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.'], ]; } /** * Get the console command options. * * @return array */ protected function getOptions() { return [ ['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null], ]; } }
6、注册命令,使用服务提供者类中可用的名为 commands 的 laravel 方法注册该命令。
/** * Register the service provider. * * @return void */ public function register() { $this->app->register(RouteServiceProvider::class); $this->commands([ \Modules\Blog\Console\CreatePostCommand::class, ]); }
7、要查看所有可用的 Artisan 命令的列表,可以使用 list 命令:确认 blog:create-post 已存在。如图3
8、执行:php artisan blog:create-post 时报错:Not enough arguments (missing: “example”).
PS E:\wwwroot\laravel6-modules-demo> php artisan blog:create-post Not enough arguments (missing: "example").
9、编辑 CreatePostCommand.php ,删除掉方法 getArguments
10、再次执行:php artisan blog:create-post,不再报错。查看执行结果,有输出日志文件:Modules-Blog-Console-CreatePostCommand-handle-1672371310.181-805184791.txt 。如图4
11、执行:php artisan schedule:run,响应:没有计划的命令准备好运行。
PS E:\wwwroot\laravel6-modules-demo> php artisan schedule:run No scheduled commands are ready to run.
12、参考任务调度 – Artisan 命令调度,在 App\Console\Kernel 类的 schedule 方法中定义新的调度任务
/** * Define the application's 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(); // 每分钟执行一次任务 }
13、执行 schedule:run 命令 ,执行成功,且成功生成对应的日志文件。如图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、但是,既然 command blog:create-post 是在模块中编写的,那么调度此 command 的 schedule 也应该在对应的模块中编写才是更为合适的实现。
15、参考:https://stackoverflow.com/questions/30456737/how-to-schedule-artisan-commands-in-a-package ,两种方案皆是可行的。还原第 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')); /* $this->app->booted(function () { $schedule = $this->app->make(Schedule::class); $schedule->command('blog:create-post')->everyMinute(); }); */ // 在 Laravel 6.10 及以上版本中: $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
近期评论