码迷,mamicode.com
首页 > 其他好文 > 详细

larave5.1l队列

时间:2015-11-25 10:08:15      阅读:266      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!