class Glint::Server

Attributes

block[RW]
child_pid[RW]
on_stopped[RW]
pid[RW]
port[RW]

Public Class Methods

info() click to toggle source
# File lib/glint/server.rb, line 8
def self.info
  @@info ||= {}
end
new(port = nil, opts = {}, &block) click to toggle source
# File lib/glint/server.rb, line 12
def initialize(port = nil, opts = {}, &block)
  unless block_given?
    raise ArgumentError.new('block is not given')
  end

  @port  = port || Util.empty_port
  @opts  = { timeout: 1, signals: [:TERM, :INT, :KILL] }.merge(opts)
  @block = block
  @pid   = Process.pid

  ObjectSpace.define_finalizer(self) { stop }
end

Public Instance Methods

start() click to toggle source
# File lib/glint/server.rb, line 25
def start
  self.child_pid = fork do
    block.call(port)
    exit 0
  end

  Util.wait_port(port)
  self
end
stop() click to toggle source
# File lib/glint/server.rb, line 35
def stop
  if pid == Process.pid && child_pid
    signals = @opts[:signals].clone
    begin
      Process.kill(signals.shift, child_pid)
      Timeout.timeout(@opts[:timeout]) do
        Process.waitpid(child_pid)
      end
    rescue Timeout::Error
      retry
    end
    self.child_pid = nil
    on_stopped && on_stopped.call(self)
  end
end