在循环周期内跳过运行时错误

时间:2016-09-21 06:57:41

标签: ruby

我创建了这个ruby脚本,它基本上根据每个服务器节点的配方从环境中搜索所有节点/服务器。问题是如果节点因运行时错误而停止并且它将根据 h [thread_id]<<<<<< “ReadTimeout Error”下一步

file = File.read("./#{srv}.json")
data_hash = JSON.parse(file)

h = {}
threads = []
service = data_hash.keys
service.each do |microservice|
threads << Thread.new do
thread_id = Thread.current.object_id.to_s(36)
 begin

 h[thread_id] = "#{microservice}"
 port = data_hash["#{microservice}"]['adport']
 h[thread_id] << "\nPort: #{port}\n"

 nodes = "knife search 'chef_environment:#{env} AND recipe:#{microservice}' -i 2>&1 | sed '1,2d'"
 node = %x[ #{nodes} ].split
 node.each do |n|
 h[thread_id] << "\n Node: #{n} \n"
 uri = URI("http://#{n}:#{port}/healthcheck?count=10")
 res = Net::HTTP.get_response(uri)
 status = Net::HTTP.get(uri)
  h[thread_id] << "#{res.code}"
  h[thread_id] << status
  h[thread_id] << res.message
end

rescue => e
  h[thread_id] << "ReadTimeout Error"
  next
end
end
end

threads.each do |thread|
  thread.join
end
ThreadsWait.all_waits(*threads)
h.values.join("\n")

如何才能跳过该节点,并在移动到下一个微服务之前获取此循环中其他找到的节点的结果。

0 个答案:

没有答案