Class: Slnky::Service::Base

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(url, options={})
  @server = url
  @name = self.class.name.split('::').last.downcase
  @environment = options.delete(:env) || options.delete(:environment) || 'development'
  @config = load_config(options)

  @subscriptions = self.class.subscriptions || Slnky::Service::Subscriptions.new
  @periodics = self.class.periodics || Slnky::Service::Periodics.new
  @hostname = Socket.gethostname
  @ipaddress = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address
end

Class Attribute Details

.periodicsObject (readonly)

Returns the value of attribute periodics.



131
132
133
# File 'lib/slnky/service.rb', line 131

def periodics
  @periodics
end

.subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



130
131
132
# File 'lib/slnky/service.rb', line 130

def subscriptions
  @subscriptions
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/slnky/service.rb', line 16

def config
  @config
end

Class Method Details

.periodic(seconds, method) ⇒ Object Also known as: timer



138
139
140
141
# File 'lib/slnky/service.rb', line 138

def periodic(seconds, method)
  @periodics ||= Slnky::Service::Periodics.new
  @periodics.add(seconds, method)
end

.subscribe(name, method) ⇒ Object



133
134
135
136
# File 'lib/slnky/service.rb', line 133

def subscribe(name, method)
  @subscriptions ||= Slnky::Service::Subscriptions.new
  @subscriptions.add(name, method)
end

Instance Method Details

#startObject



30
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
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/slnky/service.rb', line 30

def start
  AMQP.start("amqp://#{config.rabbit.host}:#{config.rabbit.port}") do |connection|
    @channel = AMQP::Channel.new(connection)
    @channel.on_error do |ch, channel_close|
      raise "Channel-level exception: #{channel_close.reply_text}"
    end

    @exchanges = Slnky::Service::Exchanges.new(@channel)
    @exchanges.create('events')
    @exchanges.create('logs')

    @queues = Slnky::Service::Queues.new(@channel)
    @queues.create(@name, @exchanges['events'])

    log :info, "running"

    run

    @subscriptions.each do |name, method|
      log :info, "subscribed to: #{name} -> #{self.class.name}.#{method}"
    end

    @queues[@name].subscribe do |raw|
      message = parse(raw)
      event = message.name
      data = message.payload
      @subscriptions.for(event) do |name, method|
        self.send(method.to_sym, event, data)
      end
    end

    @periodics.each do |seconds, method|
      EventMachine.add_periodic_timer(seconds) do
        self.send(method.to_sym)
      end
    end

    stopper = Proc.new do
      puts 'stopping'
      # TODO: log :warn, "slnky.service.#{@name}: stopping"
      connection.close { EventMachine.stop }
    end

    Signal.trap("INT", stopper)
    Signal.trap("TERM", stopper)
  end
end