Class: Glint::Server

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = nil, opts = {}, &block) ⇒ Server

Returns a new instance of Server.



12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#block ⇒ Object

Returns the value of attribute block.



6
7
8
# File 'lib/glint/server.rb', line 6

def block
  @block
end

#child_pid ⇒ Object

Returns the value of attribute child_pid.



6
7
8
# File 'lib/glint/server.rb', line 6

def child_pid
  @child_pid
end

#on_stopped ⇒ Object

Returns the value of attribute on_stopped.



6
7
8
# File 'lib/glint/server.rb', line 6

def on_stopped
  @on_stopped
end

#pid ⇒ Object

Returns the value of attribute pid.



6
7
8
# File 'lib/glint/server.rb', line 6

def pid
  @pid
end

#port ⇒ Object

Returns the value of attribute port.



6
7
8
# File 'lib/glint/server.rb', line 6

def port
  @port
end

Class Method Details

.info ⇒ Object



8
9
10
# File 'lib/glint/server.rb', line 8

def self.info
  @@info ||= {}
end

Instance Method Details

#start ⇒ Object



25
26
27
28
29
30
31
32
33
# 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 ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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