兵兵有你

人品好,气质差.丢了工作就回家...

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

以laravel5.5为例子:

1.配置队列:composer require "predis/predis:~1.0"

a.在ENV中配置:QUEUE_DRIVER=redis

b.配置表,失败时队列的失败信息内容会存到此表里。命令:php artisan queue:failed-table

c.生成记录失败的数据表:php aritsan migrate

2.生成一个任务类出来:php artisan make:job TranslateSlug

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use App\Models\Topic;
use App\Handlers\SlugTranslateHandler;

class TranslateSlug implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $topic;

    public function __construct(Topic $topic)
    {
        // 队列任务构造器中接收了 Eloquent 模型,将会只序列化模型的 ID
        $this->topic = $topic;
    }

    public function handle()
    {
        // 请求百度 API 接口进行翻译
        $slug = app(SlugTranslateHandler::class)->translate($this->topic->title);

        // 为了避免模型监控器死循环调用,我们使用 DB 类直接对数据库进行操作
        \DB::table('topics')->where('id', $this->topic->id)->update(['slug' => $slug]);
    }
}

  

该类实现了 Illuminate\Contracts\Queue\ShouldQueue 接口,该接口表明 Laravel 应该将该任务添加到后台的任务队列中,而不是同步执行。

引入了 SerializesModels trait,Eloquent 模型会被优雅的序列化和反序列化。队列任务构造器中接收了 Eloquent 模型,将会只序列化模型的 ID。这样子在任务执行时,队列系统会从数据库中自动的根据 ID 检索出模型实例。这样可以避免序列化完整的模型可能在队列中出现的问题。

handle 方法会在队列任务执行时被调用。值得注意的是,我们可以在任务的 handle 方法中可以使用类型提示来进行依赖的注入。Laravel 的服务容器会自动的将这些依赖注入进去,与控制器方法类似。

还有一点需要注意,我们将会在模型监控器中分发任务,任务中要避免使用 Eloquent 模型接口调用,如:create()update()save() 等操作。否则会陷入调用死循环 —— 模型监控器分发任务,任务触发模型监控器,模型监控器再次分发任务,任务再次触发模型监控器.... 死循环。在这种情况下,使用 DB 类直接对数据库进行操作即可。

 

3.任务分发:

<?php

namespace App\Observers;

use App\Models\Topic;
use App\Jobs\TranslateSlug;

// creating, created, updating, updated, saving,
// saved,  deleting, deleted, restoring, restored

class TopicObserver
{
    public function saving(Topic $topic)
    {
        // XSS 过滤
        $topic->body = clean($topic->body, 'user_topic_body');

        // 生成话题摘录
        $topic->excerpt = make_excerpt($topic->body);

        // 如 slug 字段无内容,即使用翻译器对 title 进行翻译
        if ( ! $topic->slug) {

            // 推送任务到队列
            dispatch(new TranslateSlug($topic));
        }
    }
}

4.开始测试,监听 php artisan queue:listen

5.让队列工作起来 php artisan queue:work

6.让对队工作起来,如果设置了队列名字时,如 PrintInvoiceTask::dispatch($keys)->onQueue("print_invoice");

则执行 php artisan queue:work --queue=print_invoice

nohup php artisan queue:work --daemon --queue=print_invoice --timeout=3600 > storage/logs/queue.log 2>&1 &

提示:1 --daemon 表示队列任务完成了也不要退出。 要用queue:work,用listen是不行的,会自己挂掉 
 
posted on 2018-03-15 13:53  greatbing  阅读(3372)  评论(1编辑  收藏  举报