Class: ServerEngine::Daemon
- Inherits:
-
Object
- Object
- ServerEngine::Daemon
- Includes:
- ConfigLoader
- Defined in:
- lib/serverengine/daemon.rb
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
server is available when run() is called.
Attributes included from ConfigLoader
Class Method Summary collapse
Instance Method Summary collapse
- #daemonize_with_double_fork(inpipe) ⇒ Object
- #daemonize_with_spawn(inpipe) ⇒ Object
- #detach ⇒ Object
- #dump ⇒ Object
-
#initialize(server_module, worker_module, load_config_proc = {}, &block) ⇒ Daemon
constructor
A new instance of Daemon.
- #main ⇒ Object
- #reload ⇒ Object
- #restart(graceful) ⇒ Object
- #run ⇒ Object
- #server_main ⇒ Object
- #stop(graceful) ⇒ Object
- #write_pid_file ⇒ Object
Methods included from ConfigLoader
Constructor Details
#initialize(server_module, worker_module, load_config_proc = {}, &block) ⇒ Daemon
Returns a new instance of Daemon.
28 29 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 |
# File 'lib/serverengine/daemon.rb', line 28 def initialize(server_module, worker_module, load_config_proc={}, &block) @server_module = server_module @worker_module = worker_module super(load_config_proc, &block) @daemonize = @config.fetch(:daemonize, false) if @config.fetch(:supervisor, false) @create_server_proc = lambda do |_load_config_proc,logger| s = Supervisor.new(server_module, worker_module, _load_config_proc) s.logger = logger s end else @create_server_proc = Supervisor.create_server_proc(server_module, worker_module, @config) end @daemon_process_name = @config[:daemon_process_name] @daemonize_error_exit_code = @config[:daemonize_error_exit_code] || 1 @pid_path = @config[:pid_path] @chuser = @config[:chuser] @chgroup = @config[:chgroup] @chumask = @config[:chumask] @pid = nil @command_sender = @config.fetch(:command_sender, ServerEngine.windows? ? "pipe" : "signal") if @command_sender == "pipe" extend ServerEngine::CommandSender::Pipe else extend ServerEngine::CommandSender::Signal end end |
Instance Attribute Details
#server ⇒ Object (readonly)
server is available when run() is called. It is a Supervisor instance if supervisor is set to true. Otherwise a Server instance.
64 65 66 |
# File 'lib/serverengine/daemon.rb', line 64 def server @server end |
Class Method Details
.run_server(server_module, worker_module, load_config_proc = {}, &block) ⇒ Object
75 76 77 |
# File 'lib/serverengine/daemon.rb', line 75 def self.run_server(server_module, worker_module, load_config_proc={}, &block) Daemon.new(server_module, worker_module, load_config_proc, &block).server_main end |
Instance Method Details
#daemonize_with_double_fork(inpipe) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/serverengine/daemon.rb', line 138 def daemonize_with_double_fork(inpipe) rpipe, wpipe = IO.pipe wpipe.sync = true Process.fork do begin rpipe.close if @command_sender == "pipe" @command_sender_pipe.close end Process.setsid Process.fork do $0 = @daemon_process_name if @daemon_process_name wpipe.write "#{Process.pid}\n" Privilege.change(@chuser, @chgroup) File.umask(@chumask) if @chumask s = create_server(create_logger) if @command_sender == "pipe" s.instance_variable_set(:@command_pipe, inpipe) end STDIN.reopen(File::NULL) STDOUT.reopen(File::NULL, "wb") STDERR.reopen(File::NULL, "wb") s.install_signal_handlers wpipe.write "\n" wpipe.close begin s.main rescue SystemExit => e exit e.status end end exit 0 ensure exit! @daemonize_error_exit_code end end wpipe.close @pid = rpipe.gets.to_i data = rpipe.read rpipe.close if data != "\n" return @daemonize_error_exit_code end write_pid_file return 0 end |
#daemonize_with_spawn(inpipe) ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/serverengine/daemon.rb', line 127 def daemonize_with_spawn(inpipe) windows_daemon_cmdline = config[:windows_daemon_cmdline] config = {} if @command_sender == "pipe" config[:in] = inpipe end @pid = Process.spawn(*Array(windows_daemon_cmdline), config) write_pid_file end |
#detach ⇒ Object
218 219 220 |
# File 'lib/serverengine/daemon.rb', line 218 def detach _detach end |
#dump ⇒ Object
222 223 224 |
# File 'lib/serverengine/daemon.rb', line 222 def dump _dump end |
#main ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/serverengine/daemon.rb', line 96 def main if @daemonize if @command_sender == "pipe" inpipe, @command_sender_pipe = IO.pipe @command_sender_pipe.sync = true @command_sender_pipe.binmode end if ServerEngine.windows? ret = daemonize_with_spawn(inpipe) else ret = daemonize_with_double_fork(inpipe) end if @command_sender == "pipe" inpipe.close end return ret else @pid = Process.pid s = create_server(create_logger) s.install_signal_handlers begin s.main rescue SystemExit => e return e.status end return 0 end end |
#reload ⇒ Object
214 215 216 |
# File 'lib/serverengine/daemon.rb', line 214 def reload _reload end |
#restart(graceful) ⇒ Object
210 211 212 |
# File 'lib/serverengine/daemon.rb', line 210 def restart(graceful) _restart(graceful) end |
#run ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/serverengine/daemon.rb', line 66 def run begin exit main rescue => e ServerEngine.dump_uncaught_error(e) exit @daemonize_error_exit_code end end |
#server_main ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/serverengine/daemon.rb', line 79 def server_main $0 = @daemon_process_name if @daemon_process_name Privilege.change(@chuser, @chgroup) File.umask(@chumask) if @chumask s = create_server(create_logger) STDIN.reopen(File::NULL) STDOUT.reopen(File::NULL, "wb") STDERR.reopen(File::NULL, "wb") s.install_signal_handlers s.main end |
#stop(graceful) ⇒ Object
206 207 208 |
# File 'lib/serverengine/daemon.rb', line 206 def stop(graceful) _stop(graceful) end |
#write_pid_file ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/serverengine/daemon.rb', line 198 def write_pid_file if @pid_path File.open(@pid_path, "w") {|f| f.write "#{@pid}\n" } end end |