Class: Mtbb::ServerRunner
- Inherits:
-
Object
- Object
- Mtbb::ServerRunner
- Defined in:
- lib/mtbb.rb
Instance Attribute Summary collapse
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#executable ⇒ Object
readonly
Returns the value of attribute executable.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server_exit_code ⇒ Object
readonly
Returns the value of attribute server_exit_code.
-
#server_name ⇒ Object
readonly
Returns the value of attribute server_name.
-
#server_pid ⇒ Object
readonly
Returns the value of attribute server_pid.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#stderr_file ⇒ Object
readonly
Returns the value of attribute stderr_file.
-
#stdout_file ⇒ Object
readonly
Returns the value of attribute stdout_file.
-
#working_dir ⇒ Object
readonly
Returns the value of attribute working_dir.
Instance Method Summary collapse
- #command ⇒ Object
- #config_details ⇒ Object
- #description ⇒ Object
- #dump(which = :stdout) ⇒ Object
-
#initialize(options = {}) ⇒ ServerRunner
constructor
A new instance of ServerRunner.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ServerRunner
Returns a new instance of ServerRunner.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/mtbb.rb', line 136 def initialize( = {}) = @executable = .fetch(:executable) .delete(:executable) @argv = .delete(:argv) || [] @server_name = .delete(:server_name) || "mtbb-server-#{Time.now.utc.to_i}" @start_time = .delete(:start) || Time.now.utc @port = .delete(:port) @working_dir = .delete(:working_dir) || default_working_dir @stdout_file = .delete(:stdout_file) || default_stdout_file @stderr_file = .delete(:stderr_file) || default_stderr_file if !File.exist?(executable) raise "Can't locate `#{File.basename(executable).inspect}` executable! " << "(it's not here: #{executable.inspect})" end end |
Instance Attribute Details
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
132 133 134 |
# File 'lib/mtbb.rb', line 132 def argv @argv end |
#executable ⇒ Object (readonly)
Returns the value of attribute executable.
132 133 134 |
# File 'lib/mtbb.rb', line 132 def executable @executable end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
134 135 136 |
# File 'lib/mtbb.rb', line 134 def end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
132 133 134 |
# File 'lib/mtbb.rb', line 132 def port @port end |
#server_exit_code ⇒ Object (readonly)
Returns the value of attribute server_exit_code.
133 134 135 |
# File 'lib/mtbb.rb', line 133 def server_exit_code @server_exit_code end |
#server_name ⇒ Object (readonly)
Returns the value of attribute server_name.
132 133 134 |
# File 'lib/mtbb.rb', line 132 def server_name @server_name end |
#server_pid ⇒ Object (readonly)
Returns the value of attribute server_pid.
133 134 135 |
# File 'lib/mtbb.rb', line 133 def server_pid @server_pid end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
132 133 134 |
# File 'lib/mtbb.rb', line 132 def start_time @start_time end |
#stderr_file ⇒ Object (readonly)
Returns the value of attribute stderr_file.
133 134 135 |
# File 'lib/mtbb.rb', line 133 def stderr_file @stderr_file end |
#stdout_file ⇒ Object (readonly)
Returns the value of attribute stdout_file.
133 134 135 |
# File 'lib/mtbb.rb', line 133 def stdout_file @stdout_file end |
#working_dir ⇒ Object (readonly)
Returns the value of attribute working_dir.
132 133 134 |
# File 'lib/mtbb.rb', line 132 def working_dir @working_dir end |
Instance Method Details
#command ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/mtbb.rb', line 197 def command cmd = [executable] + argv .each do |k,v| if v.nil? cmd << k next end cmd << k << v.inspect end cmd end |
#config_details ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'lib/mtbb.rb', line 187 def config_details { argv: argv, working_dir: working_dir, start_time: start_time, stdout_file: stdout_file, stderr_file: stderr_file, } end |
#description ⇒ Object
183 184 185 |
# File 'lib/mtbb.rb', line 183 def description "#{server_name} on port #{port}" end |
#dump(which = :stdout) ⇒ Object
209 210 211 |
# File 'lib/mtbb.rb', line 209 def dump(which = :stdout) $stderr.puts File.read(send(:"#{which.to_s}_file")) end |
#start ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/mtbb.rb', line 155 def start FileUtils.mkdir_p(working_dir) process_command = command spawn_args = process_command + [ out: [stdout_file, 'w'], err: [stderr_file, 'w'] ] Mtbb.announce! "Starting #{description}" Mtbb.announce! " ---> #{process_command.join(' ')}" Mtbb.announce! " (#{config_details.inspect})" @server_pid = Process.spawn(*spawn_args) server_pid end |
#stop ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/mtbb.rb', line 168 def stop return if !server_pid Mtbb.announce! "Stopping #{description} (PID=#{server_pid})" Process.kill(:TERM, server_pid) begin wait_for_server_process(Integer(ENV['MTBB_PROCESS_EXIT_TIMEOUT'] || 3)) rescue TimeoutError end Process.kill(:KILL, server_pid) _, status = Process.waitpid2(server_pid) @server_exit_code = status.exitstatus || status.termsig rescue Errno::ECHILD, Errno::ESRCH true end |