Class: ZSS::Service

Inherits:
Object
  • Object
show all
Includes:
LoggerFacade::Loggable
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.



13
14
15
16
17
18
19
20
21
22
# File 'lib/zss/service.rb', line 13

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.



11
12
13
# File 'lib/zss/service.rb', line 11

def backend
  @backend
end

#heartbeatObject (readonly)

Returns the value of attribute heartbeat.



11
12
13
# File 'lib/zss/service.rb', line 11

def heartbeat
  @heartbeat
end

#identityObject (readonly)

Returns the value of attribute identity.



11
12
13
# File 'lib/zss/service.rb', line 11

def identity
  @identity
end

#sidObject (readonly)

Returns the value of attribute sid.



11
12
13
# File 'lib/zss/service.rb', line 11

def sid
  @sid
end

Instance Method Details

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



34
35
36
# File 'lib/zss/service.rb', line 34

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

#runObject



24
25
26
27
28
29
30
31
32
# File 'lib/zss/service.rb', line 24

def run
  Thread.abort_on_exception = true

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

  log.info("Starting SID: '#{sid}' ID: '#{identity}' Env: '#{ZSS::Environment.env}' Broker: '#{backend}'")
  event_machine_start(context)
end

#stopObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zss/service.rb', line 38

def stop
  timer.cancel if timer

  EM.add_timer do

    log.info("Stoping SID: '#{sid}' ID: '#{identity}'", )

    send Message::SMI.down(sid)
    socket.disconnect backend
    EM::stop
  end
end