summaryrefslogtreecommitdiff
path: root/lib/chef/resource_collection.rb
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2016-04-04 10:45:47 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2016-04-04 11:04:03 -0700
commit0ca27b6f30ccd327505bd3a44bd319fb3eba956b (patch)
tree4adc9d499d5d487b124811d42edd73bb097fc799 /lib/chef/resource_collection.rb
parent6689faacd533e3ab5c3fd26ae5137fd0e881d0a3 (diff)
downloadchef-0ca27b6f30ccd327505bd3a44bd319fb3eba956b.tar.gz
Make notifications recursive.lcg/notify-scopes
This is similar to poise's approach but has a few differences. Similarly to poise, the base behavior of notifications and find() and lookup() on the resource collection is changed to be 'recursive' and to search in outer contexts for resources and will return them by default. There are find_local() and lookup_local() methods added to allow for bypassing the recursion and making sure to throw exceptions if the current run_context does not have any matching resources. The CHEF-3694 resource cloning code has been modified to call the lookup_local() API and not to be recursive because we believe that nobody in their right mind would want that behavior (and resource cloning should eventually be removed). So the behavior of resource cloning should remain unchanged. The behavior of delayed notifications to resources outside of the current run_context is slightly different than what Poise has been implementing. The delayed notification will run in the run_context of the resource that is being notified. I think Poise tends to bubble up to the nextmost wrapping resource context (as opposed to Poise's subcontext_block or notifying_block contexts). This code I think is conceptually simpler to reason about, and I think it gets the use case right where if you're notifying a service resource in the outermost run_context from within multiple wrapping resources that it correctly bubbles out to the outermost run context and will notify with all the other delayed notifications at the end of the chef client run. Another useful feature of the delayed notification behavior is that if we do implement notifying_block or subcontext_block that each block can get its own delayed notification run and any resources that are inside of that block can run in the delayed notification phase of that block (while still being able to notify resources outside of the block and having those delayed notifications run in the receiving resources run_context). This will let us implement an often-requested feature for having "notifications delayed to the end of a block/recipe" instead of having to do all notifications absolutely immediately or delayed to the end of the chef run. This code also cleans up the object model a little bit. All of the state about notification collection is now hanging off of the run_context -- the delayed_actions have been moved from the Chef::Runner to the Chef::RunContext. Hanging it off of the Chef::Runner would have been very difficult to 'target' from other run_context's without adding a pointer back from the RunContext to the Runner and that feels like the wrong object model. The RunContext is now responsible for all of its notification state, while the Runner is responsible for wiring up the notifications across different run_contexts. Note that it will not be possible to send a notification to a run_context which has already been converged. That seems to make sense to me and the search API on the resource collection does not support returning resources from run_contexts that are children, only parents (and we don't actually hold onto pointers to child run_contexts and they may be garbage collected).
Diffstat (limited to 'lib/chef/resource_collection.rb')
-rw-r--r--lib/chef/resource_collection.rb50
1 files changed, 46 insertions, 4 deletions
diff --git a/lib/chef/resource_collection.rb b/lib/chef/resource_collection.rb
index 6c5b4289d8..1429c25d7f 100644
--- a/lib/chef/resource_collection.rb
+++ b/lib/chef/resource_collection.rb
@@ -33,9 +33,12 @@ class Chef
extend Forwardable
attr_reader :resource_set, :resource_list
- private :resource_set, :resource_list
+ attr_accessor :run_context
- def initialize
+ protected :resource_set, :resource_list
+
+ def initialize(run_context = nil)
+ @run_context = run_context
@resource_set = ResourceSet.new
@resource_list = ResourceList.new
end
@@ -79,11 +82,50 @@ class Chef
resource_list_methods = Enumerable.instance_methods +
[:iterator, :all_resources, :[], :each, :execute_each_resource, :each_index, :empty?] -
- [:find] # find needs to run on the set
- resource_set_methods = [:lookup, :find, :resources, :keys, :validate_lookup_spec!]
+ [:find] # find overridden below
+ resource_set_methods = [:resources, :keys, :validate_lookup_spec!]
def_delegators :resource_list, *resource_list_methods
def_delegators :resource_set, *resource_set_methods
+ def lookup_local(key)
+ resource_set.lookup(key)
+ end
+
+ def find_local(*args)
+ resource_set.find(*args)
+ end
+
+ def lookup(key)
+ if run_context.nil?
+ lookup_local(key)
+ else
+ lookup_recursive(run_context, key)
+ end
+ end
+
+ def find(*args)
+ if run_context.nil?
+ find_local(*args)
+ else
+ find_recursive(run_context, *args)
+ end
+ end
+
+ private
+
+ def lookup_recursive(rc, key)
+ rc.resource_collection.resource_set.lookup(key)
+ rescue Chef::Exceptions::ResourceNotFound
+ raise if rc.parent_run_context.nil?
+ lookup_recursive(rc.parent_run_context, key)
+ end
+
+ def find_recursive(rc, *args)
+ rc.resource_collection.resource_set.find(*args)
+ rescue Chef::Exceptions::ResourceNotFound
+ raise if rc.parent_run_context.nil?
+ find_recursive(rc.parent_run_context, *args)
+ end
end
end