Dan H.
Dan H.

Reputation: 23

Restarting Service Only If Specific Attribute Changes

Suppose you have 2 pairs of attributes with 1 corresponding template, and each pair of attributes is for a different service. How do you restart each service separately, rather than both services being restarted when only 1 of the 2 pairs of attributes changes. Thanks!

#recipe
template "/etc/security/limits.conf" do
  source 'limits.conf.erb'
  mode '0644'
  notifies :restart, 'service[nginx]' #need code to restart separately
  notifies :restart, 'service[memcached]' #same as above
end

#attributes
default['nginx']['www-data']['soft'] = 32000
default['nginx']['www-data']['hard'] = 32000
default['memcache']['soft'] = 32000
default['memcache']['hard'] = 32000

#template
www-data soft nofile <%= node['nginx']['www-data']['soft'] %>
www-data hard nofile <%= node['nginx']['www-data']['hard'] %>
memcache hard nofile <%= node['memcache']['hard'] %>
memcache soft nofile <%= node['memcache']['soft'] %>

Upvotes: 2

Views: 745

Answers (1)

display name
display name

Reputation: 4185

I would suggest you to try adding a middle-man ruby block to manage the service. You need to replace the if and elsif statement below to the ones that you use to check which service to start. --

template '/etc/security/limits.conf' do
  source 'limits.conf.erb'
  mode '0644'
  notifies :run, 'ruby_block[start_right_service]', :immediately
end

ruby_block 'start_right_service' do
  action :nothing
  block do
    if [# nginx attributes changed]
       self.notifies :restart,'service[nginx]',:immediately
    elsif [# memcachedattributes changed]
       self.notifies :restart,'service[memcached]',:immediately
    else
       self.notifies :restart,'service[nginx]',:immediately
       self.notifies :restart,'service[memcached]',:immediately
    end    
  end
end

Upvotes: 2

Related Questions