douyi6922 2015-05-19 16:25
浏览 69
已采纳

为Laravel Events设置队列的最佳方法是什么?

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.

  • 写回答

2条回答 默认 最新

  • doubinchou4219 2015-05-19 19:31
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要