所属分类:php教程
Laravel5.5之事件监听、任务调度、队列
流程:
php artisan make:event UserLogin
LoginController.php
/** * The user has been authenticated. * * @param \Illuminate\Http\Request $request * @param mixed $user * @return mixed */ protected function authenticated(Request $request, $user) { event(new UserLogin($user)); }
php artisan make:listener EmailAdminUserLogin --event=UserLogin
//应用程序的事件监听器映射 class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'App\Events\UserLogin' => [ 'App\Listeners\UserLogin\EmailAdminUserLogin', 'App\Listeners\UserLogin\TraceUser', 'App\Listeners\UserLogin\AddUserLoginCounter', ], 'App\Events\UserLogout' => [ 'App\Listeners\UserLogout\EmailAdminUserLogout', 'App\Listeners\UserLogout\TraceUser', ], ]; /** * Register any events for your application. * * @return void */ public function boot() { parent::boot(); Event::listen('event.*', function ($eventName, array $data) { // }); } }
生成事件 & 监听器:php artisan event:generate
protected function schedule(Schedule $schedule) { $schedule->call(function (){ \Log::info('我是call方法实现的定时任务'); })->everyMinute(); }
执行:php artisan schedule:run
生成命令:php artisan make:command SayHello
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class SayHello extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'message:hi'; /** * 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() { //书写处理逻辑 \Log::info('早上好,用户'); } }
Kernel.php
protected function schedule(Schedule $schedule) { $schedule->command('message:hi') ->everyMinute(); }
执行:php artisan schedule:run
QUEUE_DRIVER=database
如:数据库驱动
php artisan queue:table php artisan migrate
生成任务类:
php artisan make:job SendReminderEmail
class SendReminderEmail implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $user; /** * Create a new job instance. * * @param User $user */ public function __construct(User $user) { $this->user = $user; } /** * Execute the job. * * @return void */ public function handle() { \Log::info('send reminder email to user' . $this->user->email); } }
你写好任务类后,就能通过 dispatch
辅助函数来分发它了。唯一需要传递给 dispatch
的参数是这个任务类的实例:
利用模型工厂生成30个用户:
public function store(Request $request) { $users = User::where('id','>',24)->get(); foreach ($users as $user){ $this->dispatch(new SendReminderEmail($user)); } return 'Done'; }
Route::get('/job', 'UserController@store');
数据库表jobs
生成5个队列任务:
php artisan queue:work
Tips:要注意,一旦 queue:work
命令开始,它将一直运行,直到你手动停止或者你关闭控制台
处理单一任务:你可以使用 --once
选项来指定仅对队列中的单一任务进行处理
php artisan queue:work --once
拓展:使用 Beanstalkd
管理队列,Supervisor
则是用来监听队列的任务,并在队列存在任务的情况下自动帮我们去执行,免去手动敲 php artisan
的命令,保证自己的队列可以正确执行
《相关推荐:最新的五个Laravel视频教程》
以上就是解析Laravel5.5之事件监听、任务调度、队列的详细内容,更多请关注zzsucai.com其它相关文章!