首页 > 其他 > 详细

larave5.1l队列

时间:2015-11-25 10:06:05      阅读:251      评论:0      收藏:0      [点我收藏+]

官方文档http://laravel.com/docs/5.1/queues#dealing-with-failed-jobs

1、队列容器设置为数据库

  config/queue.php  

‘default‘ => env(‘QUEUE_DRIVER‘, ‘database‘),

2、建立队列和失败队列数据库

php artisan queue:table
php artisan queue:failed-table
php artisan migrate

3、创建队列SendReminderEmail 

php artisan make:job SendReminderEmail --queued
<?php

namespace App\Jobs;

use App\User;
use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
use Exception;

class SendReminderEmail extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }
    public function handle()
    {
       $user = $this->user;
        $url = route(‘confirmation‘, [‘token‘ => $user->registration_token]);
        Mail::send(‘emails/registration‘, compact(‘user‘, ‘url‘), function ($m) use ($user) {
            $m->to($user->email, $user->name)->subject(‘test!‘);
        });
       // throw new Exception;   //异常可使队列失败
    }
}

4、发送队列

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\User;
use App\Jobs\SendReminderEmail;

class JobsController extends Controller
{
   public function testSendJobs(){
       $user = User::findOrFail(1);
       $this->dispatch(new SendReminderEmail($user));

   }
}

5、开启队列监听

php artisan queue:listen database --tries=3  //监听数据库容器的队列,3次执行失败,则将队列放到失败队列数据库表

6、处理失败队列

php artisan queue:failed //列出失败队列
php artisan queue:retry 1  //将id=1的失败队列恢复到队列表

 

larave5.1l队列

原文:http://www.cnblogs.com/zenghansen/p/4993712.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!