converge_by does not respect sequential order #8616
In a resource (tested with custom resource and LWRP) the use of
12.18.31 to at least 15.0.300
Create a custom resource like below:
provides :test action :run do log '1' converge_by 'logging' do ::Chef::Log.info '2' log '3' end end
Then converge this in a recipe:
My original idea of the
I'm don't know either whether it is a good thing to use resources inside the
This is just how chef works with compile/converge mode.
The log resources are resources and they are always delayed into converge mode.
The converge_by block is a compile-time declaration. The same with the Log statement.
And no you should in general not be using converge_by. You should almost never be placing converge_by around resources. The purpose of that is NOT to log output if that is what you are using it for (which is the #1 mistake in using converge by). The converge_by statement also always sets the updated_by_last_action flag of the resource it is in, which means that it will cause notifications to fire if it is executed. That means that converge_by statements MUST always be wrapped with conditionals.
The purpose of converge_by is for when you are wrapping pure ruby code. It MUST look like:
If you want to use a chef resource (which you should, you should never roll your own file resource like in that example) then just use it and do not use converge_by.
@lamont-granquist Thanks a lot for the clarification on when to use converge_by vs built-in resources, or that I need to wrap them with conditionals, but I was already aware of that and this is not my issue here, if you told me that because of my example I just wanted to have a clean and simple one.
Maybe I wasn't clear enough; my problem is that the execution order of custom resource with mixed converge_by and built-in resources is not as straight forward as it should be, and/or completely undocumented.
This is a good example of non-documentation, how should I know that, but also could you clarify that point? Should I, or not? If I should in which case?
Here is another example, that I hope will help to clarify the situation:
resource '1 converge_by "a resource in a converge_by" do resource '2' end if should_be_updated? converge_by "a pure ruby code" do ::Chef::Log.warn "X" end if should_be_updated?
I personally was expecting that "at compile time" of the action evaluation:
Then at the "convergence time":
So in the case where my resources were log with
# kind of compile time WARN: X # kind of convergence time INFO: 1 INFO: 2
But I was wrong, the result is:
# kind of compile time WARN: X # kind of convergence time INFO: 2 INFO: 1
Seems like the "This is not a bug, this is a feature" answer to me. Would you @lamont-granquist really expect the "X 2 1" order?
So this is an issue tracker for the source code. This isn't a source code issue, it is strictly a documentation issue and should get opened against the chef-web-docs:
And the answer to this question:
Resources are not run when they are declared. The declaration of the resource builds an object which is then put on the resource collection list. Once the ruby code in the recipe or action is fully parsed then the resource collection is converged and the resources are actually run. So all ruby code runs before all resources converge.
The fact that "converge_by" runs at compile time is perhaps confusing, but the name of that method was written 8 years ago and that ship already sailed and isn't coming back. It is mostly intended for advanced users who already understand compile/converge mode.
And you're starting to get into "how do I chef?" questions which are not bug reports. These should be asked on slack, discourse or stackoverflow.