0

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?

Pegasus1985
  • 126
  • 4
  • 11
  • We'd need to see your get_api_calls resource definition, but I guess it's not using `converge_if_changed` nor proper guard around `converge_by` and so it is always marked updated, hence triggering the notification at each call. There's too much possibilities about what you're doing in load_current_resource to do that, so without code it's not answerable. – Tensibai Oct 31 '22 at 15:49
  • @Tensibai, you're right. Initially I used for every ruby code execution a Ruby block to avoid to get it executed during compile phase and to get it only executed at the correct time within the recipe. I was wrong, the API interaction work almost the same without using Ruby blocks. I removed the Ruby blocks as well as removed the run_context directive within my file attribute execution and it worked! – Pegasus1985 Nov 12 '22 at 08:20

2 Answers2

0

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

Pegasus1985
  • 126
  • 4
  • 11
-1

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
Mohamed Elgazar
  • 778
  • 4
  • 9