Class: ServerEngine::Server

Inherits:
Object
  • Object
show all
Includes:
ConfigLoader
Defined in:
lib/serverengine/server.rb

Direct Known Subclasses

EmbeddedServer, MultiWorkerServer

Defined Under Namespace

Modules: WorkerInitializer

Instance Attribute Summary

Attributes included from ConfigLoader

#config, #logger

Instance Method Summary collapse

Methods included from ConfigLoader

#reload_config

Constructor Details

#initialize(worker_module, load_config_proc = {}, &block) ⇒ Server

Returns a new instance of Server.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/serverengine/server.rb', line 27

def initialize(worker_module, load_config_proc={}, &block)
  @worker_module = worker_module

  @stop = false
  @stop_status = nil

  super(load_config_proc, &block)

  @log_stdout = !!@config.fetch(:log_stdout, true)
  @log_stderr = !!@config.fetch(:log_stderr, true)
  @log_stdout = false if logdev_from_config(@config) == STDOUT
  @log_stderr = false if logdev_from_config(@config) == STDERR

  @command_sender = @config.fetch(:command_sender, ServerEngine.windows? ? "pipe" : "signal")
  @command_pipe = @config.fetch(:command_pipe, nil)
end

Instance Method Details

#after_runObject



47
48
# File 'lib/serverengine/server.rb', line 47

def after_run
end

#after_startObject



56
57
# File 'lib/serverengine/server.rb', line 56

def after_start
end

#before_runObject



44
45
# File 'lib/serverengine/server.rb', line 44

def before_run
end

#dumpObject



73
74
75
76
# File 'lib/serverengine/server.rb', line 73

def dump
  Sigdump.dump unless config[:disable_sigdump]
  nil
end

#install_signal_handlersObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/serverengine/server.rb', line 78

def install_signal_handlers
  s = self
  if @command_pipe
    Thread.new do
      until @command_pipe.closed?
        case @command_pipe.gets&.chomp
        when "GRACEFUL_STOP"
          s.stop(true)
        when "IMMEDIATE_STOP"
          s.stop(false)
        when "GRACEFUL_RESTART"
          s.restart(true)
        when "IMMEDIATE_RESTART"
          s.restart(false)
        when "RELOAD"
          s.reload
        when "DETACH"
          s.detach(true)
        when "DUMP"
          s.dump
        end
      end
    end
  else
    SignalThread.new do |st|
      st.trap(@config[:signal_graceful_stop] || Signals::GRACEFUL_STOP) { s.stop(true) }
      st.trap(@config[:signal_detach] || Signals::DETACH) { s.stop(true) }
      # Here disables signals excepting GRACEFUL_STOP == :SIGTERM because
      # only SIGTERM is available on all version of Windows.
      unless ServerEngine.windows?
        st.trap(@config[:signal_immediate_stop] || Signals::IMMEDIATE_STOP) { s.stop(false) }
        st.trap(@config[:signal_graceful_restart] || Signals::GRACEFUL_RESTART) { s.restart(true) }
        st.trap(@config[:signal_immediate_restart] || Signals::IMMEDIATE_RESTART) { s.restart(false) }
        st.trap(@config[:signal_reload] || Signals::RELOAD) { s.reload }
        st.trap(@config[:signal_dump] || Signals::DUMP) { s.dump }
      end
    end
  end
end

#mainObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/serverengine/server.rb', line 118

def main
  create_logger unless @logger

  # start threads to transfer logs from STDOUT/ERR to the logger
  start_io_logging_thread(STDOUT) if @log_stdout && try_get_io_from_logger(@logger) != STDOUT
  start_io_logging_thread(STDERR) if @log_stderr && try_get_io_from_logger(@logger) != STDERR

  before_run

  begin
    run
  ensure
    after_run
  end
  if @stop_status
    raise SystemExit.new(@stop_status)
  end
end

#reloadObject



66
67
68
69
70
71
# File 'lib/serverengine/server.rb', line 66

def reload
  @logger.info "Received reload" if @logger
  reload_config
  @logger.reopen! if @logger
  nil
end

#restart(stop_graceful) ⇒ Object



59
60
61
62
63
64
# File 'lib/serverengine/server.rb', line 59

def restart(stop_graceful)
  @logger.info "Received #{stop_graceful ? 'graceful' : 'immediate'} restart" if @logger
  reload_config
  @logger.reopen! if @logger
  nil
end

#stop(stop_graceful) ⇒ Object



50
51
52
53
54
# File 'lib/serverengine/server.rb', line 50

def stop(stop_graceful)
  @logger.info "Received #{stop_graceful ? 'graceful' : 'immediate'} stop" if @logger
  @stop = true
  nil
end