class Plux::Server

Constants

Active

Attributes

name[R]
pid[R]

Public Class Methods

new(name, thread: ) click to toggle source
# File lib/plux/server.rb, line 12
def initialize(name, thread: )
  @name = name
  @thread = thread
end

Public Instance Methods

boot(worker) click to toggle source
# File lib/plux/server.rb, line 17
def boot(worker)
  Plux.lock_pid_file(name) do |file|
    start_server_if_not_pid(file, worker)
  end
  self
end
close() click to toggle source
# File lib/plux/server.rb, line 24
def close
  Process.kill('TERM', pid) rescue Errno::ESRCH
end
connect() click to toggle source
# File lib/plux/server.rb, line 28
def connect
  Client.new(name)
end

Private Instance Methods

delete_server() click to toggle source
# File lib/plux/server.rb, line 62
def delete_server
  [:server_file, :pid_file].each do |file|
    File.delete(Plux.send(file, name))
  end
end
start_server_if_not_pid(file, worker) click to toggle source
# File lib/plux/server.rb, line 34
def start_server_if_not_pid(file, worker)
  @pid = file.read.to_i
  return unless pid == 0

  child, parent = IO.pipe

  @pid = fork do
    at_exit{ delete_server }
    file.close
    child.close
    UNIXServer.open(Plux.server_file(name)) do |serv|
      parent.close
      worker.prepare
      reactor = Reactor.new(@thread, worker)
      loop{ reactor.register(serv.accept) }
    end
  end

  parent.close
  child.read
  child.close

  file.rewind
  file.write(pid)
  Process.detach(pid)
  Active[name] = self
end