Notifies vs. Subscribes with Chef

Jul 22, 2016
I ran into this interesting scenario recently. At uShip, we have Chef code that writes a Consul config file for a service, let's say RabbitMQ, and when the template is updated, Consul needs to be reloaded. The current code looks something like the following:

template "#{consul_config_directory}/rabbit_config.json" do

execute 'consul reload' do
  action :nothing
  subscribes :run, "template[#{consul_config_directory}/rabbit_config.json"

When a developer needed to add a new service, Mongo, it was not clear that another subscribes statement also needed to be added. This meant that Consul did not pick up the config properly.

This got me thinking about when to use notifies versus subscribes in Chef code. They basically do the same thing but in this case, the intention would have been a bit clearer if the template notified Consul to reload instead of the other way around. Just something to consider the next time you're trying to decide which type of notification you want