卷曲重试机制 [英] Curl retry mechanism
问题描述
我运行一个脚本来部署2个Web服务和一个前端应用程序。该脚本调用一种方法,以检查2个后端服务是否已启动并正在运行。如果是这样,则认为部署成功,否则恢复。
I have a script I run to deploy 2 web services and a front-end application. The script calls a method that checks to see if the 2 back-end services are up and running. If so, deem the deploy successful, otherwise revert.
这两项服务有时需要的时间比前端要长。目前,我打了个睡眠电话以延迟Web服务检查。
The 2 services sometimes take longer than the front-end to start up. Currently I put in a sleep call to delay the web service check. This allows them time to start up.
我想删除此睡眠并添加重试机制,以便在服务关闭时重复检查直到我再次尝试。
I want to remove this sleep and add in a retry mechanism so that if a service is down, just retry the check repeatedly until I get a response.
TP检查设备是否启动,我使用curl。我已经知道curl具有重试机制,但是我从未使用过。
Tp check if the device is up, I use curl. I've read that curl has a retry mechanism but I've never used it.
你们中的任何人以前解决过这个问题吗?我想了解解决问题时必须考虑的事项,例如我要重试,直到我从服务中获得HTTP 200吗?
Have any of you solved this problem before? I want to understand the things I must consider when solving it, e.g. do I retry until i get a HTTP 200 from my service?
有人建议我对此进行测试吗?我需要找到一个关闭的服务。
Anyone any suggestions how I'd test this? I'd need to find a service that was down.
编辑:我看到-retry只对瞬态错误做出反应'瞬态错误的意思是:超时,FTP 4xx响应代码或HTTP 5xx响应代码。我的服务可以返回404,因此curl的重试不是我的解决方案。
I see that -retry only reacts to transient errors 'Transient error means either: a timeout, an FTP 4xx response code or an HTTP 5xx response code'. My service can return a 404 therefore curl's retry is not my solution.
推荐答案
以下语句将重试5次或最多重试一次40秒,连接超时为5秒,并且没有指数补偿策略
The following statement will retry 5 times or a maximum of 40 seconds with a connection timeout of 5 seconds, and no exponential backoff policy
curl --connect-timeout 5 \
--max-time 10 \
--retry 5 \
--retry-delay 0 \
--retry-max-time 40 \
'http://your_url'
--max-time 10 (how long each retry will wait)
--retry 5 (it will retry 5 times)
--retry-delay 0 (an exponential backoff algorithm)
--retry-max-time (total time before it's considered failed)
请注意,还有一个-retry-connrefused
(自curl 7.52.0起),即使连接为拒绝和-重试所有错误
(自curl 7.71.0起),是重试的大锤。
Note that there is also a --retry-connrefused
(since curl 7.52.0) that retries even when the connection is refused and --retry-all-errors
(since curl 7.71.0) which "is the sledgehammer of retrying".
这篇关于卷曲重试机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!