Chef - Custom Resource - How to notify only if the content of the ressource updates a file?

144 views Asked by At

I'm not sure if I've understood the concept of notifying other resources based on a custom resource correctly. I have a resource:

get_api_calls 'www.myapi.com' do
  request '<myrequest>'
  target_path '/<my>/<target>/<filepath>
  notifies :reload, 'service[apache]', :delayed
end

This demo resource is ficitonal but shows what I want to do. Under the hood it queries the API, downloads the necessary file and stores it into my_target_filepath. Everytime I execute the Chef code from above, it reloads Apache. That makes sense because I'm telling the DSL to reload the resource everytime it's executed. But I don't want this behavior. I want to norify to reload Apache if a file has been downloaded and updated from the API only.

I mean the file resource acts the same in the end. It updates or creates a file under the hood and notifies only another resource instead of notifying it everytime it gets executed without changing something.

How is it possible to implement that behavior for my resource?

2

There are 2 answers

0
Pegasus1985 On

I've used Ruby blocks for the whole API interaction. Removing the blocks and run_context from file ressource solved the issue.

0
Mohamed Elgazar On

One way to achieve this would be to use a conditional in your get_api_calls resource that only notify the apache service if the file has been updated:

get_api_calls 'www.myapi.com' do
  request '<myrequest>'
  target_path '/<my>/<target>/<filepath>
  notifies :reload, 'service[apache]', :delayed if file_updated?
end