How can I manage single service resource for multiple roles

I have 4 recipes and 4 chef-roles defined.1st recipe is (basic cookbook) default.rb and rest are written to integrate new features with basic cookbook.

At the end of basic cookcook execution, service resource gets executed which restarts the service.
now, for another extended recipes, is calling basic_cookbook first and then the code for new feature. After the new feature configuration we again need to restart the service.

So when, we are executing 2nd chef-role (basic cookbook+ new feature), service is getting restarted twice (after basic cookbook execution and after the new feature recipe execution)

we want service to be restarted only once at the end of execution of 2nd recipe. But we can’t remove the code for service restart from basic cookcook as when chef-role1 is called , we need restart.

How can I manage starting/stopping of service resource for multiple roles?

Delayed notifications are automatically de-duplicated and all run as a batch at the very end of the converge. This is generally how you handle server restart/reload signals.

So in my basic cookcook, default.rb recipe I should make service as delayed instead of strat, rt?

Existing code:

service “collectd” do
supports :status => true, :restart => true, :reload => true
action [ :enable, :start ]

Changed code will be liked this

service “collectd” do
supports :status => true, :restart => true, :reload => true
action [ :delayed]

Please confirm

No, it looks like this:

service “collectd” do
supports :status => true, :restart => true, :reload => true
action [ :enable, :start ], :delayed

Hmmm… The list ate my formatting. Hopefully it still gets the point across.

yes. I got your point. I tried it . but the problem with this approach is

if I execute multiple roles at once (assume a stack with middleware and application), then chef-client run is executing once per role,
So for 3 roles, 3 times chef-client run is happening internally so it is restarting the service thrice

Can I define the attributes in attribute file and call it in service
if node [attribute]= true
then start service or do nothing?

If I could do this, I am not abel to think how can I map the service attributes restart=>true in default.rb of attribute files

Pl guide

No, you want the notification to be marked as delayed. The docs for that are here: Common Resource Functionality

As Coderanger said before, when you use delayed notifications, they get uniq'd and then processed in a bunch at the conclusion of the main portion of the chef run.

I don’t want t handle it by notifiations. In earlier post I mentioned theproblem I am facing with notifications.

Can you please guide me how can I achieve it with attributes

If you have one server with three roles defined, they should be referenced in one run list. Doing a chef-client run three times for this means your node object is not going to be converged properly, nor will notifications be handled properly.

With all three roles/cookbooks within a single run list for this node, and one chef-client run the service should handle a delayed notification properly.