Guard and Guard Interpreter for ruby_block

Hi,

https://docs.chef.io/resource_ruby_block.html

Is it just me or am I correct in assuming that ruby_block does NOT support guard/guard interpreter?

I am trying to execute 2 resources (one a execute resource and another a ruby_block) and i want both these resources to NOT RUN if the PID is already up and running…

execute 'Start Weblogic Node Manager' do
  guard_interpreter :bash
  user node['weblogic']['user']
  group node['weblogic']['group']
  cwd node['weblogic']['user_project_bin']

  #This PID runs in the foreground by default, so need to use nohup & to run as a background process
  command "nohup ./startNodeManager.sh > chef_start_nodemanager.log 2>&1 &"

  # grep exits with 1 when no match is found. This resource is executed when not_if returns false i.e exit code is != 0 from bash script
  # So, this resource is run if grep doesn't find a match
  not_if 'ps -ef | grep weblogic.NodeManager | grep -v grep'
end


#Only run the below block if the SAME guard as above is false.....can that be done??   
ruby_block "Wait until Node Manager has started" do
  block do
     until File.readlines("#{node['weblogic']['domainhome']}/bin/chef_start_nodemanager.log").grep(/Plain socket listener started on port 5556, host localhost/).size > 0
         Chef::Log.info('sleeping 3 seconds until grep condition true')
         sleep 3
     end
  end
end

However, looking at the ruby_block documentation, there is no mention of a guard_interpreter/guard, so just checking if its supported.

Cheers

Hi there – the guard functionality should included in all built-in resources. Are you actually seeing a problem? It should just work.

FYI https://docs.chef.io/resource.html#common-functionality

Thank you. I think I got confused because I was expecting a Guards section within https://docs.chef.io/resource_ruby_block.html similar to how https://docs.chef.io/resource_ruby.html does.

A little off topic, but worth noting the | grep -v grep is easy to avoid.
Try with not_if 'ps -ef | grep "[w]eblogic.NodeManager"'
Using the brackets tell grep to match the same as you did but its own command line won't match as there will be brackets inside it.

Please don’t start services like this. Check out poise-service for an easy way to convert this into a real service management system.

1 Like