当前位置:网站首页>Message queue batch processing refund order through queue
Message queue batch processing refund order through queue
2022-06-29 06:55:00 【weixin_ two billion forty-seven million six hundred and seventy】
In the last tutorial, we saw the benefits of splitting a single time-consuming Queue task into multiple subtasks :

But it also introduces new problems , If you use Redis As a queue driver , There will be multiple executions Redis Command push Queue task problem , And every time you do it Redis Command to establish a network connection ,IO Costly .
Batch push
So , We can go through Laravel Message queuing provides The batch Function to push multiple tasks at once to improve queue performance .
Take the refund business as an example , We can pass before each Function to traverse all participants , Then push... In turn in the closure RefundAttendee The queue task operation is adjusted to pass map The function returns all valid RefundAttendee Queue task instances are combined into one jobs aggregate , And then through Bus::batch($jobs)->dispatch() Batch push $jobs All tasks in the collection are executed to the message queue :

In order to make RefundAttendee The task class supports all the functions of batch processing , Can be introduced in this class Batchable Trait, So that the batch instance to which the current task belongs can be obtained later , And cancel batch processing :

If you use a database as a queue driver , A new data table needs to be introduced job_batches:

You can refer to Laravel Queue documents Learn more about Queue task batching .
Cancel batch
You can get the corresponding batch instance after executing the batch push , And then take it. ID Persist to conference Model instance refunds_batch Field :

If you want to cancel the batch operation in the middle , You can use the batch stored above ID Get the corresponding batch instance , Then execute... On this instance cancel Method to cancel it :

however , It should be noted that , This will only set the batch records stored in the data table to canceled, You cannot recall a task that has been pushed to a queue .
To plug this loophole , Before executing a queue task, you need to determine whether the batch it belongs to is valid , If the batch has been canceled , Then the task will not be performed :

Processing batch failed
The default configuration is that if any task fails to execute in the batch, the whole batch will be canceled , This means that if batch processing 1000 Refunds , But it goes to 300 I made a mistake when I was , Then the rest will not be executed because the batch has been canceled .
To force batch processing to continue even if a task fails , This can be done by... At the time of distribution allowFailures() Method to set :

thus , Even if a task fails , Batch processing will also continue , Then the failed task ID Store in job_batches surface .
You can retry a single batch of failed tasks with the following command :

You can also retry all failed tasks in the specified batch at once :

边栏推荐
- Exclusive download. Alibaba cloud native brings 10+ technical experts to bring new possibilities of cloud native and cloud future
- 配置Flutter开发环境
- 力扣今日题-324. 摆动排序 II
- Fault: administrator account cannot be selected for login
- package.json的所有配置项及其用法,你都熟悉么
- Introduction to Ceres Quartet
- Easy to understand TCP four waves (multi picture explanation)
- Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- multi axis mirroring function
- Qt 自定义位操作类
- Qt QFrame详解
猜你喜欢

Exclusive download. Alibaba cloud native brings 10+ technical experts to bring new possibilities of cloud native and cloud future

解析学习幼儿机器人教育的浪潮

用机器人教育创造新一代生产和服务工具

Qt QLineEdit详解

Qt QFrame详解

Easy to understand TCP four waves (multi picture explanation)

开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事

Qt 串口编程

Analysis on the wave of learning robot education for children

Linux Installation redis
随机推荐
二叉树的迭代法前序遍历的两种方法
Antd work item memo w3.0
Li Kou daily question - day 30 -1281 Difference of sum of bit product of integer
Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- length and angle measurement function
数据库-同义词
'only_ full_ group_ The influence of by'sql mode on group by and its treatment
2022.02.14 - 239. A single element in an ordered array
Client and server working modes of JVM
Fault: display Storport driver out of date in component health
Failure: unable to log in to "taxpayer equity platform"
Why are keys unordered in golang map
Li Kou today's question -324 Swing sort II
把多个ROC曲线画在一张图上
Baidu applet automatically submits search
Object detection - VIDEO reasoning using yolov6
关于 localStorage 的一些高阶用法
Illegal forward reference and enums
Introduction to Ceres Quartet
[c language] [sword finger offer article] - print linked list from end to end
Aging design guide for applets