Class: Chef::EventDispatch::Serf

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Serf

Returns a new instance of Serf.



23
24
25
26
27
28
# File 'lib/chef/event_dispatch/serf.rb', line 23

def initialize(options = {})
  @subscribe = options.delete(:subscribe) || []
  @publish_all = options.delete(:publish_all) || false
  @ignore_failure = options.delete(:ignore_failure)
  @conn_opts = options.dup
end

Instance Method Details

#converge_completeObject



35
36
37
# File 'lib/chef/event_dispatch/serf.rb', line 35

def converge_complete
  serf_event(__method__.to_s, payload_to_json)
end

#payload_to_json(data = {}) ⇒ Object



46
47
48
49
50
# File 'lib/chef/event_dispatch/serf.rb', line 46

def payload_to_json(data = {})
  Chef::JSONCompat.to_json(
    data.merge(node: Chef::Config[:node_name])
  )
end

#resource_updated(res, action) ⇒ Object



39
40
41
42
43
44
# File 'lib/chef/event_dispatch/serf.rb', line 39

def resource_updated(res, action)
  if @publish_all or @subscribe.include?(res.to_s) or res.publish
    payload = payload_to_json(resource: res.to_s, action: action.to_s)
    serf_event(__method__.to_s, payload)
  end
end

#run_start(version) ⇒ Object



30
31
32
33
# File 'lib/chef/event_dispatch/serf.rb', line 30

def run_start(version)
  payload = payload_to_json(version: version)
  serf_event(__method__.to_s, payload)
end

#serf_event(*args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chef/event_dispatch/serf.rb', line 52

def serf_event(*args)
  begin
    Serfx.connect(@conn_opts) do |conn|
      conn.event(*args)
    end
  rescue Errno::ECONNREFUSED => e
    if @ignore_failure
      Chef::Log.warn("Failed to publish event in serf: #{e.message}")
    else
      raise e
    end
  end
end