vikingsteve vikingsteve - 1 month ago 10
Ruby Question

chef recipe - how to add a timeout to ::File.exists? in ruby_block

Consider this code:

ruby_block 'wait for tomcat' do
block do
true until ::File.exists?('/usr/share/tomcat/webapps/system/WEB-INF')
end
end


How can I add a
timeout
, so that in the case that the deployment went wrong (and the file will never exist), my recipe can continue (and fail) after (say) 30 seconds?

Answer

Just using ruby (untested, I may have forgot something there):

ruby_block 'wait for tomcat' do
  block do
    iter=0
    until ::File.exists?('/usr/share/tomcat/webapps/system/WEB-INF') || iter > 5 do
      sleep 6
      iter++
    end
    raise "Timeout waiting for tomcat startup" unless iter <= 5
  end
end

But this kind of construct usually means you're falling into the converge vs compile problem. and thus you're probably trying to solve an XY problem. As the tomcat may not start before the end of the run anyway.

TL;DR: you're trying to code a state change instead of describing a resulting state, this goes against Configuration Management philosophy.