当前位置:网站首页>Cancellation meeting and automatic refund processing of message queue
Cancellation meeting and automatic refund processing of message queue
2022-06-22 01:16:00 【weixin_ two billion forty-seven million six hundred and seventy】
2020 year , Many industry exchange meetings have been cancelled due to the COVID-19 , This involves dealing with refunds , The ideal scenario is when we click the Cancel button in the system management interface , The refund of all paid users will be automatically processed in batch in the background , Then send the notification of meeting cancellation and refund success to the corresponding user .
Write a task class
For such time-consuming operations , It is obvious that message queuing can be used for asynchronous processing . We create a corresponding meeting cancellation task class CancelConference, stay handle In the method , We are each Method to process refunds for each participant , And send an email notification :

Processing queue task timed out
Inside this , If a third-party payment service is used , The refund needs to involve a network request , Sending mail is also a time-consuming operation , and Laravel The default timeout for a task class is 60s, Maybe not enough , You can specify --timeout Extend it :

Here we set it to 300 minute , That is to say 5 Hours , The reason why the setting is so long , Because CancelConference All participants will be refunded and notified during one execution .
Of course , and tries、--backoff Same as other parameters , You can also set the timeout through the properties of the task class :

This is recommended , Because only this task needs to set a long timeout , Other tasks don't take that long .
Avoid repeated execution of queue tasks
When getting tasks from the queue , The task is marked as reserved, In this way, it can not be acquired by other processors , When the task is completed , Either remove from the queue , Or it is pushed to the queue again to try again .
However, if the queue processor process crashes during the execution of the queue task, the task execution is interrupted , Will always be marked as reserved, Will no longer be executed .
To avoid this problem ,Laravel by reserved The status sets the timeout , The default is 90s, Can be in queue.php Configure this value in the configuration file :

Because we are CancelConference The task class has set the timeout to 5 Hours , One problem this can cause is 90s after ,reserved State failure , Other processors can get this task class to execute , Repeated execution of the same task occurs .
To solve this problem , Need to ensure retry_after The value of is greater than the timeout of all task classes :

Make sure you have enough processors
If a task needs to be performed 5 Hours , If there is only one queue processor process , Other queue tasks will be blocked , To do this, you need to start multiple processor processes , But it is also possible that all processor processes are executing these time consuming processes at the same time 5 Hours of work , One solution is to push these tasks to separate queues :

And allocate enough processor processes to it :

Use separate connections to handle time-consuming tasks
Press and hold the gourd to lift the ladle , Only 1 Queue tasks take 5 Hours , With all the other tasks reversed The state requires 5 It takes more than hours to expire , It's not the right thing to do , To solve this problem , You can set up separate connections for this queue :

Then specify the connection when pushing very time-consuming tasks to the queue :

Or set the connection in the task class through the attribute value :

Finally, we can specify the queue connection when starting the processor process :
php artisan queue:work database-cancelationsA queue processor process that specifies a connection can only consume queue tasks in that connection .
Split a single task into multiple subtasks
our CancelConference Task classes may need to run for up to 5 Hours to complete all refunds and email , During this period, the queue processor process can only handle this task , If an exception occurs during execution , And start from scratch , And skip refunds that have already been processed , There are uncontrollable and high risks .
So , We can split this time-consuming single task distribution into multiple subtask distributions , It's not hard to realize , It is the implementation code that processes all refunds and notifications in the original task :

Adjust to one task to process only one user's refund and notification , We build a new one RefundAttendee Task class to handle this work , Incoming from outside RefundAttendee No longer Conference example , It is Attendee The instance :

thus , We can revert to using a single queue connection , There is no need to configure those extra timeout fields :

here , We are actually working in a queue (CancelConference) Add another queue task (RefundAttendee) Push to message queue for processing .
边栏推荐
- [environment stepping on pit] error occurs when uploading files using fastdfs test
- Special survey of moving average strategy
- 高德地图--根据地理位置获取经纬度
- eVC4编的程序不能在emulator上运行
- MySQL 8.0 新特性梳理汇总
- 03 FastJson 解决循环引用
- yolov3 3D 语义点云paper阅读
- MSVC与GCC的差异(收集中)
- pytorch学习10:统计运算
- Pytorch learning 02: handwritten digit recognition
猜你喜欢

Go Technology Daily (June 20, 2022) -- go: simple optimization notes

Pytorch learning 09: basic matrix operations

SSO and oauth2 solutions

Summary of new MySQL 8.0 features

Simple sorting of RNN

pytorch学习12:自动求导
![[dailyfresh] problems encountered in sending activation email](/img/08/b292f6c98615e51e666b78f305e92c.png)
[dailyfresh] problems encountered in sending activation email

pytorch学习02:手写数字识别

Pytorch learning 05: indexing and slicing

Enterprises can improve database security in four ways
随机推荐
field.setAccessible(true);代码扫描有安全漏洞,解决方案
[Others] analysis of got and PLT in ELF
0x00007ffff3d3ecd0 in _ IO_ vfprintf_ internal (s=0x7ffff40b5620 <_IO_2_1_stdout_>
4274. 后缀表达式
3371. comfortable cow
Pytorch learning 06: tensor dimension transformation
Handwriting database connection pool
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
0x00007ffff3d3ecd0 in _IO_vfprintf_internal (s=0x7ffff40b5620 <_IO_2_1_stdout_>
判断系统CPU是否空闲
Pytorch learning 12: automatic derivation
三种文件句柄之间的转换
Li Kou daily question - day 24 -485 Maximum number of consecutive 1
香橙派orangepi4b上安装tensorflow与transformer
[dailyfresh] problems encountered in sending activation email
crf*.bdb文件过大问题处理
pytorch学习03:张量数据类型和一些操作
.NET中获得hInstance的几个方法
【DailyFresh】发送激活邮件遇到的问题
4274. suffix expression