Working Process of using Redis as a queue system in Laravel

Source: Internet
Author: User
Redis can be used to conveniently implement a task queue, but in Laravel, The Redis queue always has the problem of executing a task multiple times. The reason is that it writes the reserved duration, that is, if the task is not completed after one minute, the task will be put back into the queue. The following describes the simple use and execution principles of queues. Set

Redis can be used to conveniently implement a task queue, but in Laravel, The Redis queue always has the problem of executing a task multiple times. The reason is that it writes the reserved duration, that is, if the task is not completed after one minute, the task will be put back into the queue. The following describes the simple use and execution principles of queues. Set

Redis can be used to conveniently implement a task queue, but in Laravel, The Redis queue always has the problem of executing a task multiple times. The reason is that it writes the reserved duration, that is, if the task is not completed after one minute, the task will be put back into the queue. The following describes the simple use and execution principles of queues.

Set

It is very easy to set the queue to use Redis.app/config/queue.phpConfiguring

...'default' => 'redis',...'connections' => array(    ...    'redis' => array(        'driver' => 'redis',        'queue'  => 'waa',    ),),

You can.

Use

You do not need to configure multiple configurations during use. You only need to write the Queue class and its fire method, and leave the Queue as needed. For details, refer to here.

class SendEmail {    public function fire($job, $data)    {        //        $job->delete();    }}Queue::push('SendEmail@send', array('message' => $message));
Process

Laravel uses the artisan command to perform the team-out operation and then execute the task. The method is called as follows:

  1. Artisan queue: work
  2. WorkerCommand: fire ()
  3. Worker: pop ()
  4. Worker: getNextJob ()
  5. RedisQueue: pop ()
  6. Worker: process ()

Here is the problem I encountered.RedisQueue:pop()Method, there is such a sentence:

$this->redis->zadd($queue.':reserved', $this->getTime() + 60, $job);

Put the current task in another reserved queue. The timeout value is 60 s. That is to say, if the task is not deleted after 60 s, the task will be put into the queue again. Therefore, in actual use, the task may be executed multiple times. The solution is:

class SendEmail {    public function fire($job, $data)    {        $job->delete();        // job     }}

Delete the task before running it.

Original article address: the workflow of using Redis as the queue system in Laravel. Thank you for sharing it with me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.