Send Webhook to realize cross application asynchronous callback


Send Webhook

In addition to mail sending and scheduled task scheduling, we can also send network requests to third-party services asynchronously through message queues, just like Ajax in JavaScript, to push messages across applications. A typical scenario is to send a Webhook to realize asynchronous callback notification.

We create a queue task class SendWebhook It is used to execute the sending of Webhook in handle Method, through the HTTP client Send a POST request containing event data to the given URL as a Webhook. After the URL corresponding application receives the request, it will perform subsequent processing:

Here, we also set the supermarket time for the request to be 5s. If no response is received from the other application after this time, the request is considered to have timed out.

Next, you can set the SendWebhook The task is pushed to the message queue for asynchronous processing. When pushing, the third-party service and event instance are passed in as parameters, so that the following can be used when processing the task:

Retry after request failure

Since it is an HTTP network request, the request may fail for various reasons, such as network disconnection, server downtime, the specified URL does not exist, or the other service is unavailable. Embodied in $response As a result, a timeout exception is thrown and a 404 or 500 response is received. We can retry after determining that the response failed:

Of course, if we SendWebhook The maximum number of attempts is set tries In case of request failure, Larravel message queue will automatically retry the task, but this manual retry method can more finely control the underlying logic of the retry, such as how long to delay the next retry after each failure. The configuration here is actually a skip retry, because each retry will attempts Value plus one:

The reason why the delay time is set so long is that the response time of HTTP requests is uncontrollable, and if one or two request failures may be caused by network jitter, but multiple request failures may be caused by service unavailability (network disconnection, server downtime, service collapse, etc.). At this time, frequent retries will waste system resources and increase system load.

Configuration task expiration

The above task push, asynchronous processing, and failure retry are all automatic. As we said above, if the third-party service is really unavailable, repeated retries are futile and will only increase the empty consumption of system resources pointlessly (each task execution will consume the CPU and memory resources of the current machine).

At this time, we can set the maximum number of task retries, or set a termination condition for failed retries, such as no more retries after one day, which can be defined for the task class retryUtil Method to achieve:

In this case, it is usually necessary to set the maximum number of attempts to unlimited retries:

Otherwise, it may not be triggered retryUtil In fact, you can set either of these two conditions. The purpose is to set termination conditions for failed retries to avoid unlimited retries.

Failed to process the task

Finally, if the queue task triggers the maximum number of attempts, or fails to execute before the expiration date, it will be marked as execution failure. You can define failed Methods to send email/SMS notifications to developers or operators after its implementation fails, and manually intervene to promote the solution of problems, such as solving program problems, or feedback to third-party service providers to solve problems:


give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: Automatically cancel abandoned orders

>>Next: Cancel meetings and automatic refund processing