Class: Slnky::Transport::Rabbit
- Inherits:
-
Object
- Object
- Slnky::Transport::Rabbit
- Defined in:
- lib/slnky/transport.rb
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#exchanges ⇒ Object
readonly
Returns the value of attribute exchanges.
-
#queues ⇒ Object
readonly
Returns the value of attribute queues.
-
#stopper ⇒ Object
readonly
Returns the value of attribute stopper.
Instance Method Summary collapse
- #exchange(desc, type) ⇒ Object
-
#initialize ⇒ Rabbit
constructor
A new instance of Rabbit.
- #queue(desc, exchange, options = {}) ⇒ Object
- #start!(service, &block) ⇒ Object
- #stop!(msg = nil) ⇒ Object
Constructor Details
#initialize ⇒ Rabbit
Returns a new instance of Rabbit.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/slnky/transport.rb', line 19 def initialize @config = Slnky.config @host = @config.rabbit.host @port = @config.rabbit.port @user = @config.rabbit.user @pass = @config.rabbit.pass userpass = @user ? "#{@user}:#{@pass}@" : "" @url = "amqp://#{userpass}#{@host}:#{@port}" @channel = nil @exchanges = {} @queues = {} end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
14 15 16 |
# File 'lib/slnky/transport.rb', line 14 def channel @channel end |
#exchanges ⇒ Object (readonly)
Returns the value of attribute exchanges.
15 16 17 |
# File 'lib/slnky/transport.rb', line 15 def exchanges @exchanges end |
#queues ⇒ Object (readonly)
Returns the value of attribute queues.
16 17 18 |
# File 'lib/slnky/transport.rb', line 16 def queues @queues end |
#stopper ⇒ Object (readonly)
Returns the value of attribute stopper.
17 18 19 |
# File 'lib/slnky/transport.rb', line 17 def stopper @stopper end |
Instance Method Details
#exchange(desc, type) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/slnky/transport.rb', line 75 def exchange(desc, type) raise 'attempting to create exchange without channel' unless @channel name = "slnky.#{desc}" @exchanges[desc] ||= case type when :fanout @channel.fanout(name) when :direct @channel.direct(name) else raise "unknown exchange type: #{ex.type}" end end |
#queue(desc, exchange, options = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/slnky/transport.rb', line 89 def queue(desc, exchange, ={}) raise 'attempting to create queue without channel' unless @channel name = "service.#{desc}.#{exchange}" = { durable: true }.merge() routing = .delete(:routing_key) = routing ? {routing_key: routing} : {} @queues[desc] ||= @channel.queue(name, ).bind(@exchanges[exchange], ) end |
#start!(service, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/slnky/transport.rb', line 32 def start!(service, &block) AMQP.start(@url) do |connection| @connection = connection @channel = AMQP::Channel.new(@connection) @channel.on_error do |ch, channel_close| raise "Channel-level exception: #{channel_close.reply_text}" end Signal.trap("INT", proc { self.stop!('Interrupted') }) Signal.trap("TERM", proc { self.stop!('Terminated') }) exchange('events', :fanout) exchange('logs', :fanout) exchange('response', :direct) queue(service.name, 'events') yield self if block_given? if service.is_a?(Slnky::Service::Base) queues.each do |name, queue| queue.subscribe do |raw| event = Slnky::Message.parse(raw) service.subscriber.for(event.name) do |name, method| service.send(method.to_sym, event.name, event.payload) end end end service.timers.each do |seconds, method| EventMachine.add_periodic_timer(seconds) do service.send(method.to_sym) end end end end end |
#stop!(msg = nil) ⇒ Object
69 70 71 72 73 |
# File 'lib/slnky/transport.rb', line 69 def stop!(msg=nil) return unless @connection puts "#{Time.now}: stopping (#{msg})" if msg @connection.close { EventMachine.stop { exit } } end |