Class: LambdaQueuer

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda-queuer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exchange, routing_key, host = '127.0.0.1', port = 5672) ⇒ LambdaQueuer

Returns a new instance of LambdaQueuer.



7
8
9
10
11
12
# File 'lib/lambda-queuer.rb', line 7

def initialize(exchange, routing_key, host='127.0.0.1', port=5672)
	@host = host
	@port = port
	@exchange = exchange
	@routing_key = routing_key
end

Instance Attribute Details

#queueObject

Returns the value of attribute queue.



6
7
8
# File 'lib/lambda-queuer.rb', line 6

def queue
  @queue
end

Instance Method Details

#post(&block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lambda-queuer.rb', line 14

def post(&block)
	EventMachine.run do
		begin
			connection = AMQP.connect(:host => @host, :port => @port)
		  	channel = AMQP::Channel.new(connection)
		  	exchange = channel.direct(@exchange, :auto_delete => true)
		  	@queue = channel.queue(@routing_key, :auto_delete => false, :passive => true)
		  	@queue.bind(exchange, :routing_key => @routing_key)
			v = block.to_source
		  	exchange.publish(v, :routing_key => @routing_key)
			EventMachine.add_timer(2) do
				connection.close { EventMachine.stop }
			end
		rescue => e
			puts e
		end
	end
end