Class: ZSS::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sid, config = {}) ⇒ Service

Returns a new instance of Service.



10
11
12
13
14
15
16
17
18
19
# File 'lib/zss/service.rb', line 10

def initialize(sid, config = {})

  fail Error[500] if sid.blank?

  @sid = sid.to_s.upcase
  @heartbeat = config.try(:heartbeat) || 1000
  @backend   = config.try(:backend) || Configuration.default.backend
  @router = ZSS::Router.new
  @identity = "#{sid}##{SecureRandom.uuid}"
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



8
9
10
# File 'lib/zss/service.rb', line 8

def backend
  @backend
end

#heartbeatObject (readonly)

Returns the value of attribute heartbeat.



8
9
10
# File 'lib/zss/service.rb', line 8

def heartbeat
  @heartbeat
end

#identityObject (readonly)

Returns the value of attribute identity.



8
9
10
# File 'lib/zss/service.rb', line 8

def identity
  @identity
end

#sidObject (readonly)

Returns the value of attribute sid.



8
9
10
# File 'lib/zss/service.rb', line 8

def sid
  @sid
end

Instance Method Details

#add_route(context, route, handler = nil) ⇒ Object



45
46
47
# File 'lib/zss/service.rb', line 45

def add_route(context, route, handler = nil)
  router.add(context, route, handler)
end

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zss/service.rb', line 21

def run
  Thread.abort_on_exception = true

  context = EM::ZeroMQ::Context.new(1)
  fail RuntimeError, 'failed to create create_context' unless context

  # puts "Starting SID: '#{sid}' ID: '#{identity}'"
  # puts "Env: #{ZSS::Environment.env}"
  # puts "Broker: #{backend}"

  EM.run do
    # handle interrupts
    Signal.trap("INT") { stop }
    Signal.trap("TERM") { stop }

    connect_socket context

    start_heartbeat_worker

    # send up message
    send Message::SMI.up(sid)
  end
end

#stopObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/zss/service.rb', line 49

def stop
  timer.cancel if timer

  # puts "Stoping SID: '#{sid}' ID: '#{socket.identity}'"
  EM.add_timer do
    send Message::SMI.down(sid)
    socket.disconnect backend
    EM::stop
  end
end