如何中断或停止ruby_block在“订阅者”中循环运行的原因

时间:2019-01-08 13:30:28

标签: ruby chef

您好,我正在尝试检查服务是否正在运行并且是否可访问。

所以我有一个“ ruby​​_block ”,它检查服务是否正在运行,如果不是,则“ 通知”以“ 执行”阻止启动它,一旦执行块启动了服务,我需要通过使用“ 订阅”调用“ ruby​​_block”来再次检查它是否正在运行 但是,当服务无法启动时,“ ruby​​_block ”将进入不可阻挡的循环。

下面是描述工作流程的食谱

ruby_block 'check_if_Service_running' do

  block do
    # ...some logic to check service

    # generates a return code 301 if successful or any other value if fails and 
    # assign it to an attribute check eg. value = return_code   
  end

  notifies :run, 'execute['start_service']', :immediately
  subscribes :run,'execute[Start_Service]', :immediately
end

execute 'Start_Service' do
  #...code to start the service


  action :nothing

  not_if { value == 301 }
end

因此,在这种情况下,即使服务在执行块之后仍无法启动,则ruby_block继续运行并通知“执行”块,依此类推

注意:服务成功启动后,它确实显示了预期的行为

但是有时服务无法启动会进入循环

请帮助我停止“ ruby​​_block”进入订阅的循环原因两次以上,并在服务启动时停止所有操作(循环)

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

改为使用自定义资源,您希望对事物的控制比DSL所能提供的更为明确。

答案 1 :(得分:0)

It looks as though you have a ruby_block resource that is both subscribing to and notifying the same execute resource, which is probably always the wrong thing to do.

Why are you using ruby_block and execute resources instead of just a service resource?

Your recipe looks more like you are thinking of Chef of something you tell to do things in a particular way. Instead, Chef recipes should be a description of a state you want to assert, and then let Chef figure out how to achieve that state. Tell Chef you want your service to be running, and worry about adding more detail if and only if that doesn't work. It is uncommon to need to provide that extra detail.