Class: Raq::Server
- Inherits:
-
Object
- Object
- Raq::Server
- Defined in:
- lib/raq/server.rb,
lib/raq/server/builder.rb
Defined Under Namespace
Classes: Builder
Constant Summary collapse
- CONNECTION_DEFAULTS =
{host: "127.0.0.1"}
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#connection_options ⇒ Object
readonly
Returns the value of attribute connection_options.
-
#queue_names ⇒ Object
readonly
Returns the value of attribute queue_names.
Instance Method Summary collapse
- #connect ⇒ Object
- #handle_message(meta, payload) ⇒ Object
-
#initialize(options = {}, app = nil, &block) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
Constructor Details
#initialize(options = {}, app = nil, &block) ⇒ Server
Returns a new instance of Server.
11 12 13 14 15 16 |
# File 'lib/raq/server.rb', line 11 def initialize(={}, app=nil, &block) @queue_names = .fetch(:queues) { raise ArgumentError, "You must provide a list of at least 1 queue to subscribe to." } @connection_options = .fetch(:connection,CONNECTION_DEFAULTS) @app = app @app = Server::Builder.new(&block).to_app if block end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
9 10 11 |
# File 'lib/raq/server.rb', line 9 def app @app end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/raq/server.rb', line 9 def connection @connection end |
#connection_options ⇒ Object (readonly)
Returns the value of attribute connection_options.
9 10 11 |
# File 'lib/raq/server.rb', line 9 def @connection_options end |
#queue_names ⇒ Object (readonly)
Returns the value of attribute queue_names.
9 10 11 |
# File 'lib/raq/server.rb', line 9 def queue_names @queue_names end |
Instance Method Details
#connect ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/raq/server.rb', line 30 def connect @connection = AMQP.connect(self.) @channel = AMQP::Channel.new(@connection) #@channel.prefetch(1) @queues = Array(self.queue_names).collect do |queue_name| queue = @channel.queue(queue_name, durable: true, auto_delete: false) queue.subscribe(ack: true, &method(:handle_message)) end end |
#handle_message(meta, payload) ⇒ Object
40 41 42 |
# File 'lib/raq/server.rb', line 40 def (, payload) @app.call(,payload) end |
#run ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/raq/server.rb', line 18 def run starter = proc do connect end if EventMachine.reactor_running? starter.call else EventMachine.run(&starter) end end |