Class: Chef::EventDispatch::Dispatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/chef/event_dispatch/dispatcher.rb

Overview

== EventDispatch::Dispatcher The Dispatcher handles receiving event data from the sources (Chef::Client, Resources and Providers, etc.) and publishing the data to the registered subscribers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#action_collection_registration, #attribute_changed, #attribute_file_load_failed, #attribute_file_loaded, #attribute_load_complete, #attribute_load_start, #compliance_input_enabled, #compliance_input_loaded, #compliance_load_complete, #compliance_load_start, #compliance_profile_enabled, #compliance_profile_loaded, #compliance_waiver_enabled, #compliance_waiver_loaded, #converge_complete, #converge_failed, #converge_start, #cookbook_clean_complete, #cookbook_clean_start, #cookbook_compilation_complete, #cookbook_compilation_start, #cookbook_gem_failed, #cookbook_gem_finished, #cookbook_gem_installing, #cookbook_gem_start, #cookbook_gem_using, #cookbook_resolution_complete, #cookbook_resolution_failed, #cookbook_resolution_start, #cookbook_sync_complete, #cookbook_sync_failed, #cookbook_sync_start, #definition_file_load_failed, #definition_file_loaded, #definition_load_complete, #definition_load_start, #handler_executed, #handlers_completed, #handlers_start, #inputs_load_complete, #inputs_load_start, #key_migration_status, #library_file_load_failed, #library_file_loaded, #library_load_complete, #library_load_start, #lwrp_file_load_failed, #lwrp_file_loaded, #lwrp_load_complete, #lwrp_load_start, #msg, #node_load_completed, #node_load_failed, #node_load_start, #node_load_success, #ohai_completed, #ohai_plugin_file_load_failed, #ohai_plugin_file_loaded, #ohai_plugin_load_complete, #ohai_plugin_load_start, #policyfile_loaded, #profiles_load_complete, #profiles_load_start, #provider_requirement_failed, #recipe_file_load_failed, #recipe_file_loaded, #recipe_load_complete, #recipe_load_start, #recipe_not_found, #registration_completed, #registration_failed, #registration_start, #removed_cookbook_file, #resource_action_start, #resource_after_state_loaded, #resource_bypassed, #resource_completed, #resource_current_state_load_bypassed, #resource_current_state_loaded, #resource_failed, #resource_failed_retriable, #resource_skipped, #resource_up_to_date, #resource_update_applied, #resource_update_progress, #resource_updated, #run_completed, #run_failed, #run_list_expand_failed, #run_list_expanded, #run_start, #run_started, #skipping_registration, #stream_closed, #stream_opened, #stream_output, #synchronized_cookbook, #updated_cookbook_file, #waivers_load_complete, #waivers_load_start, #whyrun_assumption

Constructor Details

#initialize(*subscribers) ⇒ Dispatcher

Returns a new instance of Dispatcher.



14
15
16
# File 'lib/chef/event_dispatch/dispatcher.rb', line 14

def initialize(*subscribers)
  @subscribers = subscribers
end

Instance Attribute Details

#subscribersObject (readonly)

Returns the value of attribute subscribers.



12
13
14
# File 'lib/chef/event_dispatch/dispatcher.rb', line 12

def subscribers
  @subscribers
end

Instance Method Details

#call_subscribers(method_name, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef/event_dispatch/dispatcher.rb', line 66

def call_subscribers(method_name, *args)
  @in_call = true
  subscribers.each do |s|
    # Skip new/unsupported event names
    next unless s.respond_to?(method_name)

    mth = s.method(method_name)
    # Trim arguments to match what the subscriber expects to allow
    # adding new arguments without breaking compat.
    if mth.arity < args.size && mth.arity >= 0
      mth.call(*args.take(mth.arity))
    else
      mth.call(*args)
    end
  end
ensure
  @in_call = false
end

#deprecation(message, location = ) ⇒ Object

Special case deprecation, since it needs to know its caller



50
51
52
# File 'lib/chef/event_dispatch/dispatcher.rb', line 50

def deprecation(message, location = caller(2..2)[0])
  enqueue(:deprecation, message, location)
end

#enqueue(method_name, *args) ⇒ Object



36
37
38
39
# File 'lib/chef/event_dispatch/dispatcher.rb', line 36

def enqueue(method_name, *args)
  event_list << [ method_name, *args ]
  process_events_until_done unless @in_call
end

#event_listObject

Since the cookbook synchronizer will call this object from threads, we have to deal with concurrent access to this object. Since we don't want threads to handle events from other threads, we just use thread local storage.



23
24
25
# File 'lib/chef/event_dispatch/dispatcher.rb', line 23

def event_list
  Thread.current[:chef_client_event_list] ||= []
end

#formatter?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check to see if we are dispatching to a formatter

Returns:

  • (Boolean)


56
57
58
# File 'lib/chef/event_dispatch/dispatcher.rb', line 56

def formatter?
  subscribers.any? { |s| s.respond_to?(:is_formatter?) && s.is_formatter? }
end

#register(subscriber) ⇒ Object

Add a new subscriber to the list of registered subscribers



28
29
30
# File 'lib/chef/event_dispatch/dispatcher.rb', line 28

def register(subscriber)
  subscribers << subscriber
end

#unregister(subscriber) ⇒ Object



32
33
34
# File 'lib/chef/event_dispatch/dispatcher.rb', line 32

def unregister(subscriber)
  subscribers.reject! { |x| x == subscriber }
end