Home >PHP Framework >Laravel >Introduction to the usage of queues in laravel framework (with code)
This article brings you an introduction to the usage of queues in the laravel framework (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
In actual project development, we often encounter situations that require lightweight queues, such as sending text messages, sending emails, etc. These tasks are not enough to use heavyweight message queues such as kafka and RabbitMQ, but And it does require functions such as asynchronous, retry, and concurrency control. Generally speaking, we often use Redis, Beanstalk, and Amazon SQS to implement related functions. Laravel provides a unified API for different background queue services. This article will introduce the most widely used redis queue.
Before explaining laravel’s queue service, we must first talk about the queue service based on redis. First of all, redis is designed for caching, but due to some of its own characteristics, it can be used for message queues
redis It is easy to implement message queue features such as FIFO (first in, first out). You only need a list object to fetch data from the beginning and stuff data from the tail.
Related commands: (1) Left-side input and right-side output: lpush/rpop; (2) Right-side input and left-side output: rpush/lpop.
This simple message queue is easy to implement.
Some task scenarios do not require the task to be executed immediately, but need to be delayed; some tasks are very important and need to be retried when the task fails. These functions cannot be accomplished solely by relying on lists. At this time, an ordered collection of redis is needed.
Redis ordered set is similar to Redis set, which is a collection that does not contain the same string. The difference between them is that each member of the ordered set is associated with a score, which is used to rank the members of the ordered set from the lowest score to the highest score.
There is no relationship between the ordered set and the delayed task. However, you can set the score of the ordered set to the time when the delayed task is started, and then poll the ordered set to retrieve the expired tasks. Come out for processing, thus realizing the function of delaying tasks.
For important tasks that need to be retried, before the task is executed, the task will be put into an ordered collection and the longest execution time of the task will be set. If the task is successfully executed, the task will be deleted from the ordered collection. If the task is not completed within the specified time, the tasks in the ordered set will be put back into the queue.
Related commands:
(1) ZADD Adds one or more members to an ordered set, or updates its score if it already exists.
(2) ZRANGEBYSCORE Returns an ordered set of member ranges by score.
(3) ZREMRANGEBYRANK Removes all members from an ordered set within the given index.
The queue service task scheduling process is as follows:
Laravel’s queue service consists of two There are two process controls, one is the producer and the other is the consumer. These two processes manipulate three redis queues, one of which is List, responsible for immediate tasks, and two Zsets, responsible for delayed tasks and pending tasks.
The producer is responsible for pushing tasks to redis. If it is an immediate task, it will be pushed to queue:default by default; if it is a delayed task, it will be pushed to queue:default:delayed.
The consumer polls two queues, continuously takes out tasks from the queue, first puts the tasks into queue:default:reserved, and then executes related tasks. If the task is executed successfully, the task in queue:default:reserved will be deleted, otherwise it will be put back into the queue:default:delayed queue.
Task distribution process:
Task processor operation:
'redis' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => 'default', 'retry_after' => 90, ],
Configure in config/queue.php
Generally speaking, the default redis configuration As above, connection is the connection name of redis in the database; queue is the queue name in redis. It is worth noting that if you are using a redis cluster, you need to use the key hash tag, which is {default}; when the task runs beyond retry_after After this time, the task will be put back into the queue.
The structure of the task class is very simple. Generally speaking, it only contains a handle method that the queue uses to call this task.
If you want the task to be pushed to the queue rather than executed synchronously, you need to implement the IlluminateContractsQueueShouldQueue interface.
If you want to push tasks to a specific connection, such as redis or sqs, you need to set the conneciton variable.
If you want to push the task to a specific queue, you can set the queue variable.
如果想要让任务延迟推送,那么需要设置 delay 变量。
如果想要设置任务至多重试的次数,可以使用 tries 变量;
如果想要设置任务可以运行的最大秒数,那么可以使用 timeout 参数。
如果想要手动访问队列,可以使用 trait : IlluminateQueueInteractsWithQueue。
任务的分发
分发服务
写好任务类后,就能通过 dispatch 辅助函数来分发它了。唯一需要传递给 dispatch 的参数是这个任务类的实例:
class PodcastController extends Controller { public function store(Request $request) { // 创建播客... ProcessPodcast::dispatch($podcast); } }
如果想延迟执行一个队列中的任务,可以用任务实例的 delay 方法。
ProcessPodcast::dispatch($podcast) ->delay(Carbon::now()->addMinutes(10));
通过推送任务到不同的队列,可以给队列任务分类,甚至可以控制给不同的队列分配多少任务。要指定队列的话,就调用任务实例的 onQueue 方法:
ProcessPodcast::dispatch($podcast)->onQueue('processing');
如果使用了多个队列连接,可以将任务推到指定连接。要指定连接的话,可以在分发任务的时候使用 onConnection 方法:
ProcessPodcast::dispatch($podcast)->onConnection('redis ');
The above is the detailed content of Introduction to the usage of queues in laravel framework (with code). For more information, please follow other related articles on the PHP Chinese website!