Class: Slnky::Transport::Rabbit

Inherits:
Object
  • Object
show all
Defined in:
lib/slnky/transport.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRabbit

attr_reader :queues



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/slnky/transport.rb', line 18

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

#channelObject (readonly)

Returns the value of attribute channel.



14
15
16
# File 'lib/slnky/transport.rb', line 14

def channel
  @channel
end

#exchangesObject (readonly)

Returns the value of attribute exchanges.



15
16
17
# File 'lib/slnky/transport.rb', line 15

def exchanges
  @exchanges
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/slnky/transport.rb', line 71

def connected?
  @channel != nil
end

#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 = 'events', options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/slnky/transport.rb', line 89

def queue(desc, exchange='events', options={})
  raise 'attempting to create queue without channel' unless @channel
  name = "service.#{desc}.#{exchange}"
  @queues[name] ||= begin
    options = {
        durable: true
    }.merge(options)
    routing = options.delete(:routing_key)
    bindoptions = routing ? {routing_key: routing} : {}
    @channel.queue(name, options).bind(@exchanges[exchange], bindoptions)
  end
end

#start!(service, &block) ⇒ Object



31
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
# File 'lib/slnky/transport.rb', line 31

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)

    yield self if block_given?

    if service.is_a?(Slnky::Service::Base)
      queue(service.name, 'events').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

      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



65
66
67
68
69
# File 'lib/slnky/transport.rb', line 65

def stop!(msg=nil)
  return unless @connection
  puts "#{Time.now}: stopping (#{msg})" if msg
  @connection.close { EventMachine.stop { exit } }
end