什么是设置Laravel事件队列的最佳方法? [英] What is the best way to set up Queues for Laravel Events?

查看:373
本文介绍了什么是设置Laravel事件队列的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到某些通知时会触发一个事件。我想排队事件,以便它们并不都是同时被触发,而是排队等待我收到它们,然后在上一个事件完成后被触发。我想知道最好的方法。



编辑:对于任何人来说,设置数据库队列驱动是非常简单和简单的。您运行php工匠队列:表并将驱动程序更改为数据库。我的问题是,由于某种原因,我的应用程序在我的.env文件中没有识别我的QUEUE_DRIVER设置。

解决方案

Laravel 5拥有处理排队工作的自己的方法,但是您仍然可以使用Laravel 4中提供的选项。我个人对于所有工作都很好奇,只是投掷一个空白的项目并运行了一对排队的工作与文档有一点帮助,所以这可能不是一个完整的答案,但我希望这可以帮助你的方式。



首先你会想要设置你的配置使用数据库队列驱动程序,这可以在 config / queue.php 中完成,或者对我而言要进入 .env 文件,请执行以下操作: QUEUE_DRIVER =数据库



然后,您要设置数据库表以容纳排队的作业,您可以通过运行一个工匠命令来执行此操作: php artisan queue:table 这将创建迁移您需要通过运行 php artisan migrate 创建表,然后您的DB中将显示您的作业表。



之后,您将要设置排队的作业,格式为命令。例如,我将设置一个将一些文本写入日志文件的作业。您可以使用artisan命令创建作业或命令,这是我创建的命令: php artisan make:command WriteToLog --queued 。这里是我添加一些代码以使其写入日志文件后,我的命令行看起来像...



应用程序/命令/ WriteToLog.php


 使用App\Commands\Command; 

使用Illuminate\Queue\SerializesModels;
使用Illuminate\Queue\InteractsWithQueue;
使用Illuminate\Contracts\Bus\SelfHandling;
使用Illuminate\Contracts\Queue\ShouldBeQueued;

class WriteToLog extends Command实现SelfHandling,ShouldBeQueued {

使用InteractsWithQueue,SerializesModels;

protected $ secs;

/ **
*创建一个新的命令实例。
*
* @return void
* /
public function __construct($ secs)
{
$ this-> secs = $ secs;
}

/ **
*执行命令。
*
* @return void
* /
public function handle()
{
\Log :: info('写入日志'。$ this-> secs);
}

}

创建命令后,要测试我在我的路线文件中写了一条路线...



app / Http / routes.php


  Route :: get('/',function(){
//有一段时间延迟工作
$ 5Secs = \ Carbon\Carbon :: now() - > addSeconds(5);
$ tenSecs = \Carbon\Carbon :: now() - > addSeconds(10);

//将作业添加到队列
Queue :: later($ 5Secs,new App\Commands\WriteToLog('5 secs'));

Queue :: later($ tenSecs ,new App\Commands\WriteToLog('10 secs'));

return'All done';
});

在我们点击路线之前,我们要监听任何作业以处理它们,只需运行 php artisan queue:listen 然后你可以去浏览器去路由,在我的浏览器中点击路由控制台显示



已处理:Illuminate\Queue\CallQueuedHandler @ call
已处理:Illuminate\Queue\CallQueuedHandler @ call

如果我查看我的日志文件,我看到以下内容:

  [2015-05-19 19:25:08] local.INFO:写入登录5秒
[2015-05-19 19:25: 10] local.INFO:写入登录10秒

不完全相隔5和10秒,希望你得到这个想法!



对我来说,这只是冰山一角,排队的工作在laravel中是非常强大的,我强烈建议您在这里查看文档: http://laravel.com/docs/5.0/queues ,这里: http://laravel.com/docs/5.0/bus



您还可以从排队的作业中触发事件或排队事件处理程序,请参阅此处了解更多详细信息: http://laravel.com/docs/5.0 / events#queued-event-handlers


I have an event that is fired when I receive certain notifications. I want to Queue the event so that they aren't all fired at the same time but are Queued up as I receive them and then fired after the previous event completes. I want to know the best way to do this.

Edit: Just for anyone in the future, setting up the database Queue driver is very straightforward and simple. You run the php artisan queue:table and change the driver to 'database'. My problem was that my app wasn't recognizing my QUEUE_DRIVER setting in my .env file for some reason.

解决方案

Laravel 5 has it's own way of dealing with queued jobs, but you can still use the options that were available in Laravel 4. I've personally been curious as to how it all works and just threw together a blank project and ran a couple of queued jobs with a little help from the docs so this may not be a full answer but I hope this helps you on your way.

First you will want to set your config to use the database queue driver, this can be done in config/queue.php or for me it was a matter of going to the .env file and doing this: QUEUE_DRIVER=database.

Then you want to set up the database table to hold the queued jobs, you can do this by running an artisan command: php artisan queue:table this will create the migration so then you need to create the table by running php artisan migrate and then you'll have your jobs table in your DB.

Following that, you'll want to set up a queued job which come in the form of Commands. For example I'll set up a job that writes some text to the log file. You can create jobs or commands using an artisan command, here's what I did to create a command: php artisan make:command WriteToLog --queued. And here's what my command class looks like after adding a little code to get it to write to the log file...

app/Commands/WriteToLog.php

use App\Commands\Command;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldBeQueued;

class WriteToLog extends Command implements SelfHandling, ShouldBeQueued {

    use InteractsWithQueue, SerializesModels;

    protected $secs;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct($secs)
    {
        $this->secs = $secs;
    }

    /**
     * Execute the command.
     *
     * @return void
     */
    public function handle()
    {
        \Log::info('Writing to the log in ' . $this->secs);
    }

}

After creating a command, to test it out I wrote a route in my routes file ...

app/Http/routes.php

Route::get('/', function(){
    // some time to delay the job
    $fiveSecs = \Carbon\Carbon::now()->addSeconds(5);
    $tenSecs  = \Carbon\Carbon::now()->addSeconds(10);

    // adds job to queue
    Queue::later($fiveSecs, new App\Commands\WriteToLog('5 secs'));

    Queue::later($tenSecs, new App\Commands\WriteToLog('10 secs'));

    return 'All done';
});

Before we hit the route we want to listen for any jobs in order to process them, just run php artisan queue:listen then you can go to your browser to the route, after hitting the route in my browser the console shows

$ php artisan queue:listen
Processed: Illuminate\Queue\CallQueuedHandler@call
Processed: Illuminate\Queue\CallQueuedHandler@call

And if I check my log file I see the following:

[2015-05-19 19:25:08] local.INFO: Writing to the log in 5 secs  
[2015-05-19 19:25:10] local.INFO: Writing to the log in 10 secs  

Not exactly 5 and 10 seconds apart but hopefully you get the idea!

For me this is really just the tip of the iceberg and queued jobs are something very powerful in laravel, I highly recommend checking out the docs here: http://laravel.com/docs/5.0/queues and here: http://laravel.com/docs/5.0/bus

You can also fire events from your queued jobs or queue an event handler, see here for more details: http://laravel.com/docs/5.0/events#queued-event-handlers

这篇关于什么是设置Laravel事件队列的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆