Class: Stapfen::Worker
Constant Summary
Constants included from Logger
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
-
.consumers ⇒ Object
Returns the value of attribute consumers.
-
.logger ⇒ Object
Returns the value of attribute logger.
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
-
.configure ⇒ Object
Expects a block to be passed which will yield the appropriate configuration for the Stomp gem.
-
.consume(queue_name, headers = {}, &block) ⇒ Object
Main message consumption block.
-
.log ⇒ Object
Optional method, should be passed a block which will yield a {Logger} instance for the Stapfen worker to use.
-
.run! ⇒ Object
Instantiate a new
Workerinstance and run it.
Instance Method Summary collapse
- #exit_cleanly ⇒ Object
- #handle_signals! ⇒ Object
-
#initialize ⇒ Worker
constructor
A new instance of Worker.
- #run ⇒ Object
Methods included from Logger
Constructor Details
#initialize ⇒ Worker
Returns a new instance of Worker.
45 46 47 |
# File 'lib/stapfen/worker.rb', line 45 def initialize handle_signals! end |
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
9 10 11 |
# File 'lib/stapfen/worker.rb', line 9 def configuration @configuration end |
.consumers ⇒ Object
Returns the value of attribute consumers.
9 10 11 |
# File 'lib/stapfen/worker.rb', line 9 def consumers @consumers end |
.logger ⇒ Object
Returns the value of attribute logger.
9 10 11 |
# File 'lib/stapfen/worker.rb', line 9 def logger @logger end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
43 44 45 |
# File 'lib/stapfen/worker.rb', line 43 def client @client end |
Class Method Details
.configure ⇒ Object
Expects a block to be passed which will yield the appropriate configuration for the Stomp gem. Whatever the block yields will be passed directly into the {Stomp{Stomp::Client{Stomp::Client#new} method
20 21 22 23 24 25 |
# File 'lib/stapfen/worker.rb', line 20 def self.configure unless block_given? raise Stapfen::ConfigurationError end @configuration = yield end |
.consume(queue_name, headers = {}, &block) ⇒ Object
Main message consumption block
35 36 37 38 39 40 41 |
# File 'lib/stapfen/worker.rb', line 35 def self.consume(queue_name, headers={}, &block) unless block_given? raise Stapfen::ConsumeError, "Cannot consume #{queue_name} without a block!" end @consumers ||= [] @consumers << [queue_name, headers, block] end |
.log ⇒ Object
Optional method, should be passed a block which will yield a {Logger} instance for the Stapfen worker to use
29 30 31 |
# File 'lib/stapfen/worker.rb', line 29 def self.log @logger = yield end |
.run! ⇒ Object
Instantiate a new Worker instance and run it
13 14 15 |
# File 'lib/stapfen/worker.rb', line 13 def self.run! self.new.run end |
Instance Method Details
#exit_cleanly ⇒ Object
87 88 89 90 91 |
# File 'lib/stapfen/worker.rb', line 87 def exit_cleanly unless client.closed? client.close end end |
#handle_signals! ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/stapfen/worker.rb', line 77 def handle_signals! Signal.trap(:INT) do exit_cleanly exit! end Signal.trap(:TERM) do exit_cleanly end end |
#run ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/stapfen/worker.rb', line 49 def run @client = Stomp::Client.new(self.class.configuration) self.class.consumers.each do |name, headers, block| # We're taking each block and turning it into a method so that we can # use the instance scope instead of the blocks originally bound scope # which would be at a class level method_name = name.gsub(/[.|\-]/, '_').to_sym self.class.send(:define_method, method_name, &block) client.subscribe(name, headers) do || self.send(method_name, ) end end begin # Performing this join/open loop to make sure that we don't # experience potential deadlocks between signal handlers who might # close the connection, and an infinite Client#join call while client.open? do client.join(1) end rescue Interrupt exit_cleanly end end |