Class: Slnky::Service::Base
- Inherits:
-
Object
- Object
- Slnky::Service::Base
- Defined in:
- lib/slnky/service.rb
Class Attribute Summary collapse
-
.periodics ⇒ Object
readonly
Returns the value of attribute periodics.
-
.subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(url, options = {}) ⇒ Base
constructor
A new instance of Base.
- #start ⇒ Object
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, ={}) @server = url @name = self.class.name.split('::').last.downcase @environment = .delete(:env) || .delete(:environment) || 'development' @config = load_config() @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
.periodics ⇒ Object (readonly)
Returns the value of attribute periodics.
135 136 137 |
# File 'lib/slnky/service.rb', line 135 def periodics @periodics end |
.subscriptions ⇒ Object (readonly)
Returns the value of attribute subscriptions.
134 135 136 |
# File 'lib/slnky/service.rb', line 134 def subscriptions @subscriptions end |
Instance Attribute Details
#config ⇒ Object (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
142 143 144 145 |
# File 'lib/slnky/service.rb', line 142 def periodic(seconds, method) @periodics ||= Slnky::Service::Periodics.new @periodics.add(seconds, method) end |
.subscribe(name, method) ⇒ Object
137 138 139 140 |
# File 'lib/slnky/service.rb', line 137 def subscribe(name, method) @subscriptions ||= Slnky::Service::Subscriptions.new @subscriptions.add(name, method) end |
Instance Method Details
#start ⇒ Object
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 77 78 79 80 |
# 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']) stopper = Proc.new do puts "#{Time.now}: stopping" connection.close { EventMachine.stop } end Signal.trap("INT", stopper) Signal.trap("TERM", stopper) log :info, "running" run @subscriptions.each do |name, method| log :info, "subscribed to: #{name} -> #{self.class.name}.#{method}" end @queues[@name].subscribe do |raw| = parse(raw) event = .name data = .payload @subscriptions.for(event) do |name, method| self.send(method.to_sym, event, data) end if event == 'slnky.service.restart' # if we get this event, just stop. upstart will start us again. log :warn, "received restart event" stopper.call end end @periodics.each do |seconds, method| EventMachine.add_periodic_timer(seconds) do self.send(method.to_sym) end end end end |