Class: Diplomat::Event
- Inherits:
-
RestClient
- Object
- RestClient
- Diplomat::Event
- Includes:
- ApiOptions
- Defined in:
- lib/diplomat/event.rb
Overview
Methods for interacting with the Consul event API endpoint
Instance Method Summary collapse
-
#fire(name, value = nil, service = nil, node = nil, tag = nil, dc = nil) ⇒ nil
Send an event rubocop:disable Metrics/ParameterLists.
-
#get(name = nil, token = :last, not_found = :wait, found = :return) ⇒ hash
Get a specific event in the sequence matching name rubocop:disable MethodLength, CyclomaticComplexity, AbcSize.
-
#get_all(name = nil, not_found = :reject, found = :return) ⇒ Array[hash]
Get the list of events matching name rubocop:disable MethodLength, AbcSize.
Methods included from ApiOptions
#check_acl_token, #use_cas, #use_consistency, #valid_transaction_verbs, #valid_value_transactions
Methods inherited from RestClient
access_method?, #concat_url, #initialize, method_missing, respond_to?, respond_to_missing?, #use_named_parameter
Constructor Details
This class inherits a constructor from Diplomat::RestClient
Instance Method Details
#fire(name, value = nil, service = nil, node = nil, tag = nil, dc = nil) ⇒ nil
Send an event rubocop:disable Metrics/ParameterLists
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/diplomat/event.rb', line 17 def fire(name, value = nil, service = nil, node = nil, tag = nil, dc = nil) url = ["/v1/event/fire/#{name}"] url += check_acl_token url += use_named_parameter('service', service) if service url += use_named_parameter('node', node) if node url += use_named_parameter('tag', tag) if tag url += use_named_parameter('dc', dc) if dc @conn.put concat_url(url), value nil end |
#get(name = nil, token = :last, not_found = :wait, found = :return) ⇒ hash
Whereas the consul API for events returns all past events that match name, this method allows retrieval of individual events from that sequence. However, because consul’s API isn’t conducive to this, we can offer first, last, next (last + 1) events, or arbitrary events in the middle, though these can only be identified relative to the preceding event. However, this is ideal for iterating through the sequence of events (while being sure that none are missed).
Get a specific event in the sequence matching name rubocop:disable MethodLength, CyclomaticComplexity, AbcSize
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/diplomat/event.rb', line 116 def get(name = nil, token = :last, not_found = :wait, found = :return) url = ['/v1/event/list'] url += check_acl_token url += use_named_parameter('name', name) @raw = @conn.get concat_url url body = JSON.parse(@raw.body) # TODO: deal with unknown symbols, invalid indices (find_index will return nil) idx = case token when :first then 0 when :last then body.length - 1 when :next then body.length else body.find_index { |e| e['ID'] == token } + 1 end if idx == body.length case not_found when :reject raise Diplomat::EventNotFound, name when :return event_name = '' event_payload = '' event_token = :last when :wait @raw = wait_for_next_event(url) @raw = parse_body # If it's possible for two events to arrive at once, # this needs to #find again: event = @raw.last event_name = event['Name'] event_payload = Base64.decode64(event['Payload']) event_token = event['ID'] end else case found when :reject raise Diplomat::EventAlreadyExits, name when :return event = body[idx] event_name = event['Name'] event_payload = Base64.decode64(event['Payload']) event_token = event['ID'] end end { value: { name: event_name, payload: event_payload }, token: event_token } end |
#get_all(name = nil, not_found = :reject, found = :return) ⇒ Array[hash]
Events are sent via the gossip protocol; there is no guarantee of delivery success or order, but the local agent will store up to 256 events that do arrive. This method lists those events. It has the same semantics as Kv::get, except the value returned is a list i.e. the current value is all events up until now, the next value is the current list plus the next event to arrive. To get a specific event in the sequence, @see #get When trying to get a list of events matching a name, there are two possibilities:
-
The list doesn’t (yet) exist / is empty
-
The list exists / is non-empty
The combination of not_found and found behaviour gives maximum possible flexibility. For X: reject, R: return, W: wait
-
X X - meaningless; never return a value
-
X R - “normal” non-blocking get operation. Default
-
X W - get the next value only (must have a current value)
-
R X - meaningless; never return a meaningful value
-
R R - “safe” non-blocking, non-throwing get-or-default operation
-
R W - get the next value or a default
-
W X - get the first value only (must not have a current value)
-
W R - get the first or current value; always return something, but
block only when necessary
-
W W - get the first or next value; wait until there is an update
Get the list of events matching name rubocop:disable MethodLength, AbcSize
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/diplomat/event.rb', line 61 def get_all(name = nil, not_found = :reject, found = :return) url = ['/v1/event/list'] url += check_acl_token url += use_named_parameter('name', name) url = concat_url url # Event list never returns 404 or blocks, but may return an empty list @raw = @conn.get url if JSON.parse(@raw.body).count.zero? case not_found when :reject raise Diplomat::EventNotFound, name when :return return [] end else case found when :reject raise Diplomat::EventAlreadyExists, name when :return # Always set the response to 200 so we always return # the response body. @raw.status = 200 @raw = parse_body return return_payload end end @raw = wait_for_next_event(url) @raw = parse_body return_payload end |