Class: Stapfen::Worker

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/stapfen/worker.rb

Constant Summary

Constants included from Logger

Logger::PROXY_METHODS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

included

Constructor Details

#initializeWorker

Returns a new instance of Worker.



45
46
47
# File 'lib/stapfen/worker.rb', line 45

def initialize
  handle_signals!
end

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



9
10
11
# File 'lib/stapfen/worker.rb', line 9

def configuration
  @configuration
end

.consumersObject

Returns the value of attribute consumers.



9
10
11
# File 'lib/stapfen/worker.rb', line 9

def consumers
  @consumers
end

.loggerObject

Returns the value of attribute logger.



9
10
11
# File 'lib/stapfen/worker.rb', line 9

def logger
  @logger
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



43
44
45
# File 'lib/stapfen/worker.rb', line 43

def client
  @client
end

Class Method Details

.configureObject

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

.logObject

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_cleanlyObject



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

#runObject



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 |message|
      self.send(method_name, message)
    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