如何让 Ruby 自动多次重试命令?

Den*_*nis 1 ruby git

目前我们的应用程序运行有时会失败,因为我们的 Gitserver 非常不可靠。

begin
  repository = Git.clone(remote_repo,deploy_script_root, { })
...
rescue Exception => e
  log.error e
  raise "Could not export deploy scripts: #{e.message}"
end
Run Code Online (Sandbox Code Playgroud)

错误:无法连接到 gitserver:80;正在访问http://gitserver 时进行操作

我想知道如何让 Ruby 多次重试此命令,以免中断我们的运行。

ush*_*sha 5

用 retry

begin
  retries ||= 0
  repository = Git.clone(remote_repo,deploy_script_root, { })
...
rescue Exception => e
  log.error e
  retries += 1
  retry if (retries <= 5)
  raise "Could not export deploy scripts: #{e.message}"
end
Run Code Online (Sandbox Code Playgroud)