Class: Emissary::Server

Inherits:
Servolux::Server
  • Object
show all
Defined in:
lib/emissary/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}, &block) ⇒ Server

Returns a new instance of Server.



23
24
25
26
27
28
29
30
# File 'lib/emissary/server.rb', line 23

def initialize(name, opts = {}, &block)
  opts[:logger] = Emissary.logger
  @running = false
  @operator = opts.delete(:operator) or raise Emissary::Error.new(ArgumentError, "Operator not provided")
  
  at_exit { shutdown! :graceful }    
  super(name, opts, &block)
end

Instance Attribute Details

#runningObject

Returns the value of attribute running.



21
22
23
# File 'lib/emissary/server.rb', line 21

def running
  @running
end

Instance Method Details

#runObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/emissary/server.rb', line 73

def run
  return unless not running?

  thr = Thread.new { EM.run }

  begin
    EM.add_periodic_timer(0.5) { @operator.shutdown! unless not @operator.shutting_down? }

    begin
      $0 = @name
      logger.info "Starting up new Operator process"
      @running = @operator.run
    rescue Exception => e
      Emissary.logger.error "Server '#{$0}': #{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
      raise e
    end
  rescue ::Emissary::Error::ConnectionError => e
    shutdown! :hard
  rescue Exception => e
    shutdown! :graceful
  end

  thr.join
end

#running?Boolean

Returns:

  • (Boolean)


32
# File 'lib/emissary/server.rb', line 32

def running?() !!@running; end

#shutdown!(type = :graceful) ⇒ Object Also known as: int, term



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/emissary/server.rb', line 34

def shutdown! type = :graceful
  begin
    case type
      when :graceful
        @operator.shutdown! unless not @operator.connected?
        EM.stop_event_loop
    end
  rescue Exception => e
    Emissary.logger.error "Exception caught during graceful shutdown: #{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
  ensure
    exit!(0)
  end
end

#startupObject

override Servolux::Server’s startup because we don’t need threaded here. also, we want to enforce exiting on completion of startup’s run



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/emissary/server.rb', line 53

def startup
  return self unless not running?

  begin
    create_pid_file
    trap_signals
    run
  rescue Exception => e
    # if something is caught here, then we can only log it. at this point we are in an
    # unknown state and can only delete our pid file and #exit!. Attempting to call
    # our #term method could cause other problems here.
    Emissary.logger.error "Server '#{$0}': #{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
  ensure
    delete_pid_file
    shutdown! :hard
  end
  
  return self
end