I'm looking for an explanation on how chef's restart mechanism works behind the scenes. I can't find any documentation for it online, and I got stuck trying to trace the code (triggers are spooky action-at-a-distance).
Let's look specifically at nginx
, and assume we're using a chef server and not chef-solo
(I don't know if that makes a difference).
I have this (for example) in a recipe:
template '/etc/nginx/nginx.conf' do
source 'nginx.cfg.erb'
owner "root"
group "root"
mode 00755
notifies :reload, "service[nginx]", :delayed
end
The notifies :reload
bit means it triggers a reload, and the :delayed
means that it will happen at the end of the chef-client
run. How does that work behind the scenes? I'm having trouble following the execution thread.
Somewhere chef-client
has to run service nginx reload
or something along those lines. Where and how is that defined?
notifies
sends a notification for another Chef resource to do something.
In your example, it's telling the resource service[nginx]
to :reload
. service[nginx]
is a service
resource whose name
is nginx
.
For this to work, service[nginx]
must have been declared in your node's run_list
at some point. Otherwise, Chef will throw an error. Usually this is either done manually in a recipe by the user, or via a dependency (say, the application
, or the nginx
cookbooks).
Exactly how Chef runs the reload
command depends on how the service[nginx]
resource was declared, but it usually depends on the underlying operating system (that's one of the beauties of using a tool such as this -- it abstracts many lower level details away from you and allows you to use the same code across multiple platforms).
In the Syntax section of the service
documentation you find this:
Chef::Provider::Service::Init
, Chef::Provider::Service::Init::Debian
, Chef::Provider::Service::Upstart
, Chef::Provider::Service::Init::Freebsd
, Chef::Provider::Service::Init::Gentoo
, Chef::Provider::Service::Init::Redhat
, Chef::Provider::Service::Solaris
, Chef::Provider::Service::Windows
, or Chef::Provider::Service::Macosx
. The chef-client will detect the platform at the start of the run based on data collected by Ohai. After the platform is identified, the chef-client will determine the correct providerThe functionality is described in the chef doco.
If the configuration file's contents change this will trigger a reload of the nginx service. The "delayed" setting means the reload action occurs at the end of the chef run. The idea is that there could be several configuration files changed by a chef run and you want a single reload at the end rather than for each changed file (which is the "immediately" option).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With