当前位置:网站首页>Sending webhook of message queue to realize cross application asynchronous callback
Sending webhook of message queue to realize cross application asynchronous callback
2022-06-22 01:16:00 【weixin_ two billion forty-seven million six hundred and seventy】
send out Webhook
Besides mail sending and scheduled task scheduling , We can also asynchronously initiate network requests to third-party services through message queues , It's like JavaScript Medium Ajax like that , Push messages across applications . A typical scenario is sending Webhook Implement asynchronous callback notification .
We create a queue task class SendWebhook Used to perform Webhook Sending work of , stay handle In the method , adopt Laravel Self contained HTTP client Send... Containing event data POST Request to given URL As Webhook,URL After the corresponding application receives the request , And then carry out follow-up treatment :

here , We also set the requested supermarket time to be 5s, No response from the other party's application is received after this time , The request is considered to have timed out .
Next , In the controller method SendWebhook The task is pushed to the message queue for asynchronous processing , Third party services and event instances are passed in as parameters during push , In order to use... When processing tasks :

Try again after the request fails
Since it is HTTP Network request , Then the request may fail for various reasons , For example, the network is disconnected 、 The other server goes down 、 Appoint URL non-existent 、 Or the other party's service is not available . Embodied in $response As a result, a timeout exception is thrown 、 obtain 404 perhaps 500 Respond to , We can retry after determining that the response has failed :

Of course , If we SendWebhook The maximum number of attempts is set tries, In the event of a request failure ,Laravel Message queuing will automatically retry this task , However, through this manual retry method, the underlying logic of retry can be more finely controlled , For example, how long to delay the next retry after each failure , The configuration here is actually a skip retry , Because each retry will attempts Add the values of a :

The reason why the delay time is set so long , because HTTP The response time of the request is uncontrollable , And if oneortwo requests fail, it may be network jitter , However, if the request fails many times, it is likely that the service is no longer available ( The network is disconnected 、 Server down 、 Service crash, etc ), This is the time , Frequent retries will waste system resources , Increase system load .
Configure task expiration
Push the above tasks 、 Both asynchronous processing and failure retry are automatic , As we said above , If the third-party service is really unavailable , It is futile to try again and again , It will only increase the empty consumption of system resources in a meaningless way ( Each time a task is executed, the current machine's CPU And memory resources ).
This is the time , We can set the maximum number of task retries , Or set a termination condition for failed retries , For example, don't try again after a day , This can be done by defining retryUtil Method to implement :

In this case , It is usually necessary to set the maximum number of attempts to infinite retry :

Otherwise, it may not trigger retryUtil The conditions are , In fact, one of these two can be set arbitrarily , The purpose is to set termination conditions for failed retries , Avoid infinite retries .
Failed to process task
Last , If the queue task triggers the maximum number of attempts , Or it has not been successfully executed before the expiration date , Will be marked as execution failure , You can define for the task class failed Method , In order to send email to developers or operators after it fails to execute / SMS notification , Human intervention , Push for problem solving , Such as solving program problems , Or feed back to the third-party service provider to solve the problem :

边栏推荐
猜你喜欢

Pre add post add exploration and function call exploration

Pytorch learning 10: statistical operations

Error 4 opening dom ASM/Self in 0x8283c00

Pytorch learning 08: splicing and splitting

pytorch学习04:Tensor的创建

Pytorch learning 07:broadcast broadcast - automatic extension

Pytorch learning 03: tensor data types and some operations

【DailyFresh】课程记录3--商品搜索相关

企业可通过4个方法提高数据库安全

pytorch学习07:Broadcast广播——自动扩展
随机推荐
Sparkrdd case: calculate total score
pytorch学习06:Tensor维度变换
编译原理-递归下降子程序法
03 fastjson resolving circular references
Sqlite3数据库的timestamp类型的使用注意事项
Yolov3 3D semantic point cloud paper reading
FLowable运行时事务相关的表和表结构
【DailyFresh】发送激活邮件遇到的问题
pytorch学习11:where 和 gather
LVGL的一些介绍和移植
3分钟,带你玩转聊天机器人自动化【顶级模板】
4273. linked list consolidation
Graphical understanding of the article "text classification of Sina News Based on tensorflow+rnn"
[其他] 浅析ELF中的GOT与PLT
SQL语句——数据更新、修改、删除
SSO and oauth2 solutions
[dailyfresh] course record 3 -- product search related
今日内容
SQL statement - permission management
IDEA 提示 Duplicated code fragment (15 lines long)