简体   繁体   English

厨师食谱-如何向:: File.exists添加超时? 在ruby_block中

[英]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? 如何添加timeout ,以便在部署出错(文件永远不存在)的情况下,我的配方可以在(例如)30秒后继续(失败)?

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. 因此,您可能正在尝试解决XY问题。 As the tomcat may not start before the end of the run anyway. 由于tomcat可能在运行结束之前无法启动。

TL;DR: you're trying to code a state change instead of describing a resulting state, this goes against Configuration Management philosophy. TL; DR:您正在尝试编写状态更改代码,而不是描述结果状态,这与Configuration Management的原理背道而驰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM