Class: Mtbb::ServerRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/mtbb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @options = options
  @executable = options.fetch(:executable)
  options.delete(:executable)
  @argv = options.delete(:argv) || []

  @server_name = options.delete(:server_name) || "mtbb-server-#{Time.now.utc.to_i}"
  @start_time = options.delete(:start) || Time.now.utc
  @port = options.delete(:port)
  @working_dir = options.delete(:working_dir) || default_working_dir
  @stdout_file = options.delete(:stdout_file) || default_stdout_file
  @stderr_file = options.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

#argvObject (readonly)

Returns the value of attribute argv.



132
133
134
# File 'lib/mtbb.rb', line 132

def argv
  @argv
end

#executableObject (readonly)

Returns the value of attribute executable.



132
133
134
# File 'lib/mtbb.rb', line 132

def executable
  @executable
end

#optionsObject (readonly)

Returns the value of attribute options.



134
135
136
# File 'lib/mtbb.rb', line 134

def options
  @options
end

#portObject (readonly)

Returns the value of attribute port.



132
133
134
# File 'lib/mtbb.rb', line 132

def port
  @port
end

#server_exit_codeObject (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_nameObject (readonly)

Returns the value of attribute server_name.



132
133
134
# File 'lib/mtbb.rb', line 132

def server_name
  @server_name
end

#server_pidObject (readonly)

Returns the value of attribute server_pid.



133
134
135
# File 'lib/mtbb.rb', line 133

def server_pid
  @server_pid
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



132
133
134
# File 'lib/mtbb.rb', line 132

def start_time
  @start_time
end

#stderr_fileObject (readonly)

Returns the value of attribute stderr_file.



133
134
135
# File 'lib/mtbb.rb', line 133

def stderr_file
  @stderr_file
end

#stdout_fileObject (readonly)

Returns the value of attribute stdout_file.



133
134
135
# File 'lib/mtbb.rb', line 133

def stdout_file
  @stdout_file
end

#working_dirObject (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

#commandObject



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/mtbb.rb', line 197

def command
  cmd = [executable] + argv
  options.each do |k,v|
    if v.nil?
      cmd << k
      next
    end
    cmd << k << v.inspect
  end
  cmd
end

#config_detailsObject



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

#descriptionObject



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

#startObject



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

#stopObject



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