The first thing I would check is that the user running chef-client has permissions to start/restart the service (usually not the problem).
Next I would check that there are no other recipes running that are counteracting the logic of this recipe (sometimes the problem, but not often).
What I really think is causing your issue is the way chef handles it's queue of what it needs to execute via the shell. Multiple and somewhat conflicting calls to the same service can lead to unexpected behavior(as you have already seen). By default, all 'shell' calls are handled as the last part of the convergence phase in a chef-client run. Moreover, chef doesn't guarantee any particular order of execution, so things can frequently happen out of order and may produce undesired behavior depending on the software of the service you are manipulating. Usually bucking that with the technique below is all you need.
The quick and dirty answer to your question is to add a :timer argument to your notify calls.
DOC: http://docs.opscode.com/resource_common.html#notifications-timers
here's a suggested update to your example code above:
package "pgbouncer"
service "pgbouncer" do
supports :start => true, :stop => true, :restart => true, :reload => true, :status => true
action [:enable, :start]
end
cookbook_file "/etc/default/pgbouncer" do
source "pgbouncer/pgbouncer"
owner "root"
group "root"
mode 0644
end
cookbook_file "/etc/pgbouncer/userlist.txt" do
source "pgbouncer/userlist.txt"
owner "postgres"
group "postgres"
mode 0640
notifies :restart, "service[pgbouncer]", :immediately
end
template "/etc/pgbouncer/pgbouncer.ini" do
source "pgbouncer/pgbouncer.ini"
owner "postgres"
group "postgres"
mode 0640
variables :postgres_host => node[:postgres_host]
notifies :restart, "service[pgbouncer]", :immediately
end
This isn't the most efficient way to go as it can cause your daemon to execute too many redundant actions (up to 3 'start like' calls in one run: start,restart,restart). There is another, more OOP friendly way to do this in chef by leveraging a Definition (DOC: http://docs.opscode.com/essentials_cookbook_definitions.html). This would essentially be a custom wrapper for the pgbouncer service resource you define to reduce the inefficiencies of executing redundant calls while ensuring they are executed effectively, but I'll leave it to you to decide what's best for your use case.
Not sure if this is still an open question to you, but just for reference: There are at least two rbenv
cookbooks, fnichol's and RiotGames' (which is linked from the community site).
Only Fletcher Nichol's rbenv
cookbook has a user
recipe.
Best Answer
Most probably you have some errors in your
/etc/init.d/myservice
script. When you say that yourservice supportsstatus
command, chef will runservice myservice status
and check the exitcode. If it's 0, it assumes that the service is running. Your script should exit with nonzero code.