Class: Gongren::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/gongren/server.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



6
7
8
9
# File 'lib/gongren/server.rb', line 6

def initialize(options={})
  @options = options.inject(Hash.new) {|memo, (k,v)| memo[k.to_sym] = v; memo} # #symbolize_keys
  @logger  = options[:logger] || Logger.new(options[:log] || STDERR)
end

Class Method Details

.start(options = {}, &block) ⇒ Object

A quick way to instantiate a server with some options.



12
13
14
# File 'lib/gongren/server.rb', line 12

def self.start(options={}, &block)
  new(options).start(&block)
end

Instance Method Details

#startObject

Starts the reactor / event loop. This method never returns. Must pass a block to yield to, enabling you to do useful work. The block must return a 2 element Array: ["name of work unit", object]

You must provide a block to which an instance of Gongren::Server will be yielded.



22
23
24
25
26
27
28
29
30
31
# File 'lib/gongren/server.rb', line 22

def start
  logger.info { "Gongren::Server #{Process.pid} starting with options: #{options.inspect}" }

  AMQP.start(options) do
    @units   = MQ.topic(exchange_name, exchange_options)
    @control = MQ.topic(control_exchange_name, control_exchange_options)

    yield self
  end
end

#submit(name, message) ⇒ Object



33
34
35
36
37
# File 'lib/gongren/server.rb', line 33

def submit(name, message)
  logger.debug { "Submitting #{name.inspect}: #{message.inspect}" }
  data = Marshal.dump(message)
  @units.publish(data, :persistent => true, :key => "unit.#{name}")
end