diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2019-03-11 11:49:31 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2019-03-11 11:49:31 -0700 |
commit | 66015ba654469f4dacfd78d40b02aafee52bbf1b (patch) | |
tree | b00d0de111d18980f446b006ac63ef599eea8108 /lib/chef/event_dispatch | |
parent | 4037976199b728d4bdc18fd428e8d40a84c97e2b (diff) | |
download | chef-66015ba654469f4dacfd78d40b02aafee52bbf1b.tar.gz |
Extract Action Collection from Data Collector
See the PR for details on this change.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib/chef/event_dispatch')
-rw-r--r-- | lib/chef/event_dispatch/base.rb | 19 | ||||
-rw-r--r-- | lib/chef/event_dispatch/dispatcher.rb | 54 |
2 files changed, 58 insertions, 15 deletions
diff --git a/lib/chef/event_dispatch/base.rb b/lib/chef/event_dispatch/base.rb index 0886d63152..3b0b70c9b9 100644 --- a/lib/chef/event_dispatch/base.rb +++ b/lib/chef/event_dispatch/base.rb @@ -29,7 +29,7 @@ class Chef class Base # Called at the very start of a Chef Run - def run_start(version) + def run_start(version, run_status) end def run_started(run_status) @@ -44,6 +44,7 @@ class Chef end # Called right after ohai runs. + # NOTE: the node object here is always nil because of when it is called def ohai_completed(node) end @@ -73,6 +74,10 @@ class Chef # TODO: def node_run_list_overridden(*args) + # Called once the node is loaded by the policy builder + def node_load_success(node) + end + # Failed to load node data from the server def node_load_failed(node_name, exception, config) end @@ -163,6 +168,10 @@ class Chef ## TODO: add callbacks for overall cookbook eval start and complete. + # Called immediately after creating the run_context and before any cookbook compilation happens + def cookbook_compilation_start(run_context) + end + # Called when library file loading starts def library_load_start(file_count) end @@ -263,10 +272,18 @@ class Chef def recipe_load_complete end + # This is called after all cookbook compilation phases are completed. + def cookbook_compilation_complete(run_context) + end + # Called before convergence starts def converge_start(run_context) end + # Callback hook for handlers to register their interest in the action_collection + def action_collection_registration(action_collection) + end + # Called when the converge phase is finished. def converge_complete end diff --git a/lib/chef/event_dispatch/dispatcher.rb b/lib/chef/event_dispatch/dispatcher.rb index 69419a393b..d69a7c76e8 100644 --- a/lib/chef/event_dispatch/dispatcher.rb +++ b/lib/chef/event_dispatch/dispatcher.rb @@ -10,19 +10,44 @@ class Chef class Dispatcher < Base attr_reader :subscribers + attr_reader :event_list def initialize(*subscribers) @subscribers = subscribers + @event_list = [] end # Add a new subscriber to the list of registered subscribers def register(subscriber) - @subscribers << subscriber + subscribers << subscriber + end + + def unregister(subscriber) + subscribers.reject! { |x| x == subscriber } + end + + def enqueue(method_name, *args) + event_list << [ method_name, *args ] + process_events_until_done unless @in_call + end + + (Base.instance_methods - Object.instance_methods).each do |method_name| + class_eval <<-EOM + def #{method_name}(*args) + enqueue(#{method_name.inspect}, *args) + end + EOM + end + + # Special case deprecation, since it needs to know its caller + def deprecation(message, location = caller(2..2)[0]) + enqueue(:deprecation, message, location) end # Check to see if we are dispatching to a formatter + # @api private def formatter? - @subscribers.any? { |s| s.respond_to?(:is_formatter?) && s.is_formatter? } + subscribers.any? { |s| s.respond_to?(:is_formatter?) && s.is_formatter? } end #### @@ -30,9 +55,11 @@ class Chef # define the forwarding in one go: # + # @api private def call_subscribers(method_name, *args) - @subscribers.each do |s| - # Skip new/unsupported event names. + @in_call = true + subscribers.each do |s| + # Skip new/unsupported event names next if !s.respond_to?(method_name) mth = s.method(method_name) # Trim arguments to match what the subscriber expects to allow @@ -43,20 +70,19 @@ class Chef mth.call(*args) end end + ensure + @in_call = false end - (Base.instance_methods - Object.instance_methods).each do |method_name| - class_eval <<-EOM - def #{method_name}(*args) - call_subscribers(#{method_name.inspect}, *args) - end - EOM - end + private - # Special case deprecation, since it needs to know its caller - def deprecation(message, location = caller(2..2)[0]) - call_subscribers(:deprecation, message, location) + # events are allowed to enqueue chained events, so pop them off until + # empty, rather than iterating over the list. + # + def process_events_until_done + call_subscribers(*event_list.shift) until event_list.empty? end + end end end |