Class: Krakow::Consumer
- Inherits:
-
Object
- Object
- Krakow::Consumer
- Includes:
- Celluloid, Utils::Lazy
- Defined in:
- lib/krakow/consumer.rb,
lib/krakow/consumer/queue.rb
Overview
Consume messages from a server
Defined Under Namespace
Classes: Queue
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#discovery ⇒ Object
readonly
Returns the value of attribute discovery.
-
#distribution ⇒ Object
readonly
Returns the value of attribute distribution.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Attributes collapse
-
#backoff_interval ⇒ Numeric
The backoff_interval attribute.
-
#backoff_interval? ⇒ TrueClass, FalseClass
Truthiness of the backoff_interval attribute.
-
#channel ⇒ String
The channel attribute.
-
#channel? ⇒ TrueClass, FalseClass
Truthiness of the channel attribute.
-
#connection_options ⇒ Hash
The connection_options attribute.
-
#connection_options? ⇒ TrueClass, FalseClass
Truthiness of the connection_options attribute.
-
#discovery_interval ⇒ Numeric
The discovery_interval attribute.
-
#discovery_interval? ⇒ TrueClass, FalseClass
Truthiness of the discovery_interval attribute.
-
#discovery_jitter ⇒ Numeric
The discovery_jitter attribute.
-
#discovery_jitter? ⇒ TrueClass, FalseClass
Truthiness of the discovery_jitter attribute.
-
#host ⇒ String
The host attribute.
-
#host? ⇒ TrueClass, FalseClass
Truthiness of the host attribute.
-
#max_in_flight ⇒ Integer
The max_in_flight attribute.
-
#max_in_flight? ⇒ TrueClass, FalseClass
Truthiness of the max_in_flight attribute.
-
#notifier ⇒ [Celluloid::Signals, Celluloid::Condition, Celluloid::Actor]
The notifier attribute.
-
#notifier? ⇒ TrueClass, FalseClass
Truthiness of the notifier attribute.
-
#nsqlookupd ⇒ [Array, String]
The nsqlookupd attribute.
-
#nsqlookupd? ⇒ TrueClass, FalseClass
Truthiness of the nsqlookupd attribute.
-
#port ⇒ [String, Integer]
The port attribute.
-
#port? ⇒ TrueClass, FalseClass
Truthiness of the port attribute.
-
#topic ⇒ String
The topic attribute.
-
#topic? ⇒ TrueClass, FalseClass
Truthiness of the topic attribute.
Instance Method Summary collapse
-
#build_connection(host, port, queue) ⇒ Krakow::Connection?
Build a new [Krakow::Connection].
-
#confirm(message_id) ⇒ TrueClass
(also: #finish)
Confirm message has been processed.
-
#connected? ⇒ TrueClass, FalseClass
Currently connected to at least one nsqd.
-
#connection(key) ⇒ Krakow::Connection
Returns [Krakow::Connection] associated to key.
-
#connection_failure(actor, reason) ⇒ nil
Remove connection references when connection is terminated.
-
#consumer_cleanup ⇒ nil
Instance destructor.
-
#direct_connect ⇒ Connection
Connect to nsqd instance directly.
-
#discover ⇒ nil
Start the discovery interval lookup.
-
#init! ⇒ nil
Initialize the consumer by starting lookup and adding connections.
-
#initialize(args = {}) ⇒ Consumer
constructor
A new instance of Consumer.
-
#process_message(message, connection) ⇒ Krakow::FrameType
Process a given message if required.
-
#register(connection) ⇒ TrueClass, FalseClass
Register connection with distribution.
-
#remove_message(messages) ⇒ NilClass
Remove message.
-
#requeue(message_id, timeout = 0) ⇒ TrueClass
Requeue message (generally due to processing failure).
-
#to_s ⇒ String
Stringify object.
-
#touch(message_id) ⇒ TrueClass
Touch message (to extend timeout).
-
#update_ready!(connection) ⇒ nil
Send RDY for connection based on distribution rules.
Methods included from Utils::Lazy
Methods included from Utils::Logging
Constructor Details
#initialize(args = {}) ⇒ Consumer
Returns a new instance of Consumer.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/krakow/consumer.rb', line 41 def initialize(args={}) super arguments[:connection_options] = {:features => {}, :config => {}}.merge( arguments[:connection_options] || {} ) @connections = {} @queue = Queue.new( current_actor, :removal_callback => :remove_message ) @distribution = Distribution::Default.new( :max_in_flight => max_in_flight, :backoff_interval => backoff_interval, :consumer => current_actor ) if(nsqlookupd) debug "Connections will be established via lookup #{nsqlookupd.inspect}" @discovery = Discovery.new(:nsqlookupd => nsqlookupd) discover elsif(host && port) direct_connect else abort Error::ConfigurationError.new('No connection information provided!') end end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
18 19 20 |
# File 'lib/krakow/consumer.rb', line 18 def connections @connections end |
#discovery ⇒ Object (readonly)
Returns the value of attribute discovery.
18 19 20 |
# File 'lib/krakow/consumer.rb', line 18 def discovery @discovery end |
#distribution ⇒ Object (readonly)
Returns the value of attribute distribution.
18 19 20 |
# File 'lib/krakow/consumer.rb', line 18 def distribution @distribution end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
18 19 20 |
# File 'lib/krakow/consumer.rb', line 18 def queue @queue end |
Instance Method Details
#backoff_interval ⇒ Numeric
Returns the backoff_interval attribute.
33 |
# File 'lib/krakow/consumer.rb', line 33 attribute :backoff_interval, Numeric |
#backoff_interval? ⇒ TrueClass, FalseClass
Returns truthiness of the backoff_interval attribute.
33 |
# File 'lib/krakow/consumer.rb', line 33 attribute :backoff_interval, Numeric |
#build_connection(host, port, queue) ⇒ Krakow::Connection?
Build a new [Krakow::Connection]
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 |
# File 'lib/krakow/consumer.rb', line 131 def build_connection(host, port, queue) begin connection = Connection.new( :host => host, :port => port, :queue => queue, :topic => topic, :channel => channel, :notifier => notifier, :features => [:features], :features_args => [:config], :callbacks => { :handle => { :actor => current_actor, :method => :process_message } } ) queue.register_connection(connection) connection rescue => e error "Failed to build connection (host: #{host} port: #{port} queue: #{queue}) - #{e.class}: #{e}" debug "#{e.class}: #{e}\n#{e.backtrace.join("\n")}" nil end end |
#channel ⇒ String
Returns the channel attribute.
28 |
# File 'lib/krakow/consumer.rb', line 28 attribute :channel, String, :required => true |
#channel? ⇒ TrueClass, FalseClass
Returns truthiness of the channel attribute.
28 |
# File 'lib/krakow/consumer.rb', line 28 attribute :channel, String, :required => true |
#confirm(message_id) ⇒ TrueClass Also known as: finish
Confirm message has been processed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/krakow/consumer.rb', line 286 def confirm() = . if .respond_to?(:message_id) begin begin connection = distribution.in_flight_lookup() connection.transmit(Command::Fin.new(:message_id => )) distribution.success(connection.identifier) rescue => e abort e end true rescue KeyError => e error "Message confirmation failed: #{e}" abort e rescue Error::LookupFailed => e error "Lookup of message for confirmation failed! <Message ID: #{} - Error: #{e}>" abort e rescue Error::ConnectionUnavailable => e abort e rescue Celluloid::DeadActorError abort Error::ConnectionUnavailable.new ensure con = distribution.() update_ready!(con) if con end end |
#connected? ⇒ TrueClass, FalseClass
Returns currently connected to at least one nsqd.
69 70 71 72 73 74 75 76 77 |
# File 'lib/krakow/consumer.rb', line 69 def connected? !!connections.values.any? do |con| begin con.connected? rescue Celluloid::DeadActorError false end end end |
#connection(key) ⇒ Krakow::Connection
Returns [Krakow::Connection] associated to key
98 99 100 |
# File 'lib/krakow/consumer.rb', line 98 def connection(key) @connections[key] end |
#connection_failure(actor, reason) ⇒ nil
Remove connection references when connection is terminated
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/krakow/consumer.rb', line 252 def connection_failure(actor, reason) if(reason && key = connections.key(actor)) warn "Connection failure detected. Removing connection: #{key} - #{reason}" connections.delete(key) begin distribution.remove_connection(key) rescue Error::ConnectionUnavailable, Error::ConnectionFailure warn 'Caught connection unavailability' end queue.deregister_connection(key) distribution.redistribute! direct_connect unless discovery end nil end |
#connection_options ⇒ Hash
Returns the connection_options attribute.
37 |
# File 'lib/krakow/consumer.rb', line 37 attribute :connection_options, Hash, :default => ->{ Hash.new } |
#connection_options? ⇒ TrueClass, FalseClass
Returns truthiness of the connection_options attribute.
37 |
# File 'lib/krakow/consumer.rb', line 37 attribute :connection_options, Hash, :default => ->{ Hash.new } |
#consumer_cleanup ⇒ nil
Instance destructor
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/krakow/consumer.rb', line 110 def consumer_cleanup debug 'Tearing down consumer' if(distribution && distribution.alive?) distribution.terminate end if(queue && queue.alive?) queue.terminate end connections.values.each do |con| con.terminate if con.alive? end info 'Consumer torn down' nil end |
#direct_connect ⇒ Connection
Connect to nsqd instance directly
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/krakow/consumer.rb', line 82 def direct_connect debug "Connection will be established via direct connection #{host}:#{port}" connection = build_connection(host, port, queue) if(register(connection)) info "Registered new connection #{connection}" distribution.redistribute! else abort Error::ConnectionFailure.new("Failed to establish subscription at provided end point (#{host}:#{port}") end connection end |
#discover ⇒ nil
Start the discovery interval lookup
223 224 225 226 |
# File 'lib/krakow/consumer.rb', line 223 def discover init! after(discovery_interval + (discovery_jitter * rand)){ discover } end |
#discovery_interval ⇒ Numeric
Returns the discovery_interval attribute.
34 |
# File 'lib/krakow/consumer.rb', line 34 attribute :discovery_interval, Numeric, :default => 30 |
#discovery_interval? ⇒ TrueClass, FalseClass
Returns truthiness of the discovery_interval attribute.
34 |
# File 'lib/krakow/consumer.rb', line 34 attribute :discovery_interval, Numeric, :default => 30 |
#discovery_jitter ⇒ Numeric
Returns the discovery_jitter attribute.
35 |
# File 'lib/krakow/consumer.rb', line 35 attribute :discovery_jitter, Numeric, :default => 10.0 |
#discovery_jitter? ⇒ TrueClass, FalseClass
Returns truthiness of the discovery_jitter attribute.
35 |
# File 'lib/krakow/consumer.rb', line 35 attribute :discovery_jitter, Numeric, :default => 10.0 |
#host ⇒ String
Returns the host attribute.
29 |
# File 'lib/krakow/consumer.rb', line 29 attribute :host, String |
#host? ⇒ TrueClass, FalseClass
Returns truthiness of the host attribute.
29 |
# File 'lib/krakow/consumer.rb', line 29 attribute :host, String |
#init! ⇒ nil
Initialize the consumer by starting lookup and adding connections
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/krakow/consumer.rb', line 201 def init! debug 'Running consumer `init!` connection builds' found = discovery.lookup(topic) debug "Discovery results: #{found.inspect}" connection = nil found.each do |node| debug "Processing discovery result: #{node.inspect}" key = Connection.identifier(node[:broadcast_address], node[:tcp_port], topic, channel) unless(connections[key]) connection = build_connection(node[:broadcast_address], node[:tcp_port], queue) info "Registered new connection #{connection}" if register(connection) else debug "Discovery result already registered: #{node.inspect}" end end distribution.redistribute! if connection nil end |
#max_in_flight ⇒ Integer
Returns the max_in_flight attribute.
32 |
# File 'lib/krakow/consumer.rb', line 32 attribute :max_in_flight, Integer, :default => 1 |
#max_in_flight? ⇒ TrueClass, FalseClass
Returns truthiness of the max_in_flight attribute.
32 |
# File 'lib/krakow/consumer.rb', line 32 attribute :max_in_flight, Integer, :default => 1 |
#notifier ⇒ [Celluloid::Signals, Celluloid::Condition, Celluloid::Actor]
Returns the notifier attribute.
36 |
# File 'lib/krakow/consumer.rb', line 36 attribute :notifier, [Celluloid::Signals, Celluloid::Condition, Celluloid::Actor] |
#notifier? ⇒ TrueClass, FalseClass
Returns truthiness of the notifier attribute.
36 |
# File 'lib/krakow/consumer.rb', line 36 attribute :notifier, [Celluloid::Signals, Celluloid::Condition, Celluloid::Actor] |
#nsqlookupd ⇒ [Array, String]
Returns the nsqlookupd attribute.
31 |
# File 'lib/krakow/consumer.rb', line 31 attribute :nsqlookupd, [Array, String] |
#nsqlookupd? ⇒ TrueClass, FalseClass
Returns truthiness of the nsqlookupd attribute.
31 |
# File 'lib/krakow/consumer.rb', line 31 attribute :nsqlookupd, [Array, String] |
#port ⇒ [String, Integer]
Returns the port attribute.
30 |
# File 'lib/krakow/consumer.rb', line 30 attribute :port, [String, Integer] |
#port? ⇒ TrueClass, FalseClass
Returns truthiness of the port attribute.
30 |
# File 'lib/krakow/consumer.rb', line 30 attribute :port, [String, Integer] |
#process_message(message, connection) ⇒ Krakow::FrameType
If we receive a message that is already in flight, attempt to scrub message from wait queue. If message is found, retry distribution registration. If message is not found, assume it is currently being processed and do not allow new message to be queued
Process a given message if required
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/krakow/consumer.rb', line 168 def (, connection) discard = false if(.is_a?(FrameType::Message)) .origin = current_actor .connection = connection retried = false begin distribution.(, connection.identifier) rescue KeyError => e if(!retried && queue.()) retried = true retry else error "Received message is currently in flight and not in wait queue. Discarding! (#{})" discard = true end end end discard ? nil : end |
#register(connection) ⇒ TrueClass, FalseClass
Register connection with distribution
232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/krakow/consumer.rb', line 232 def register(connection) begin connection.init! connection.transmit(Command::Sub.new(:topic_name => topic, :channel_name => channel)) self.link connection connections[connection.identifier] = connection distribution.add_connection(connection) true rescue Error::BadResponse => e debug "Failed to establish connection: #{e.result ? e.result.error : '<No Response!>'}" connection.terminate false end end |
#remove_message(messages) ⇒ NilClass
used mainly for queue callback
Remove message
273 274 275 276 277 278 279 |
# File 'lib/krakow/consumer.rb', line 273 def () [].flatten.compact.each do |msg| distribution.(msg.) update_ready!(msg.connection) end nil end |
#requeue(message_id, timeout = 0) ⇒ TrueClass
Requeue message (generally due to processing failure)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/krakow/consumer.rb', line 319 def requeue(, timeout=0) = . if .respond_to?(:message_id) distribution.in_flight_lookup() do |connection| distribution.() connection.transmit( Command::Req.new( :message_id => , :timeout => timeout ) ) distribution.failure(connection.identifier) update_ready!(connection) end true end |
#to_s ⇒ String
Returns stringify object.
103 104 105 |
# File 'lib/krakow/consumer.rb', line 103 def to_s "<#{self.class.name}:#{object_id} T:#{topic} C:#{channel}>" end |
#topic ⇒ String
Returns the topic attribute.
27 |
# File 'lib/krakow/consumer.rb', line 27 attribute :topic, String, :required => true |
#topic? ⇒ TrueClass, FalseClass
Returns truthiness of the topic attribute.
27 |
# File 'lib/krakow/consumer.rb', line 27 attribute :topic, String, :required => true |
#touch(message_id) ⇒ TrueClass
Touch message (to extend timeout)
339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/krakow/consumer.rb', line 339 def touch() = . if .respond_to?(:message_id) begin distribution.in_flight_lookup() do |connection| connection.transmit( Command::Touch.new(:message_id => ) ) end true rescue Error::LookupFailed => e error "Lookup of message for touch failed! <Message ID: #{} - Error: #{e}>" abort e end end |
#update_ready!(connection) ⇒ nil
Send RDY for connection based on distribution rules
193 194 195 196 |
# File 'lib/krakow/consumer.rb', line 193 def update_ready!(connection) distribution.set_ready_for(connection) nil end |