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.php
Configuring
...'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:
- Artisan queue: work
- WorkerCommand: fire ()
- Worker: pop ()
- Worker: getNextJob ()
- RedisQueue: pop ()
- 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.