mangeshchef
mangeshchef

Reputation: 13

How to break or stop ruby_block running in loop cause of "subscribes"

Hi have a recipe where I am trying to check if a service is running and accessible.

So I have a "ruby_block" which checks if service is running if not it "notifies" to "execute" block to start it, once execute block starts the service I need to again check if it running by calling "ruby_block" using "subscribes" But when service fails to start "ruby_block" goes in unstoppable loop.

Below is recipe which describes the workflow

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

So in this case when Services fails to start even after execute block, ruby_block keeps on running and notify "execute" block and so on

note: when service starts successfully it does show expected behavior

But some times when service does not start it goes in loop

Please help me here to stop "ruby_block" being going in loop cause of subscribes for more than 2 times and stop everything (loop) if service felt to start

Any help will be appreciated!

Upvotes: 1

Views: 290

Answers (2)

David M
David M

Reputation: 4376

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.

Upvotes: 0

coderanger
coderanger

Reputation: 54211

Make a custom resource instead, you want more explicit control over things than the DSL will give you.

Upvotes: 1

Related Questions