class God::Socket

The God::Server oversees the DRb server which dishes out info on this God daemon.

Attributes

port[R]

Public Class Methods

new(port = nil, user = nil, group = nil, perm = nil) click to toggle source

Create a new Server and star the DRb server

+port+ is the port on which to start the DRb service (default nil)
# File lib/god/socket.rb, line 41
def initialize(port = nil, user = nil, group = nil, perm = nil)
  @port  = port
  @user  = user
  @group = group
  @perm  = perm
  start
end
socket(port) click to toggle source

The address of the socket for a given port

+port+ is the port number

Returns String (drb address)

# File lib/god/socket.rb, line 21
def self.socket(port)
  "drbunix://#{self.socket_file(port)}"
end
socket_file(port) click to toggle source

The location of the socket for a given port

+port+ is the port number

Returns String (file location)

# File lib/god/socket.rb, line 13
def self.socket_file(port)
  "/tmp/god.#{port}.sock"
end

Public Instance Methods

method_missing(*args, &block) click to toggle source

Forward API calls to God

Returns whatever the forwarded call returns

# File lib/god/socket.rb, line 57
def method_missing(*args, &block)
  God.send(*args, &block)
end
ping() click to toggle source

Returns true

# File lib/god/socket.rb, line 50
def ping
  true
end
socket() click to toggle source

The address of the socket for this Server

Returns String (drb address)

# File lib/god/socket.rb, line 35
def socket
  self.class.socket(@port)
end
socket_file() click to toggle source

The location of the socket for this Server

Returns String (file location)

# File lib/god/socket.rb, line 28
def socket_file
  self.class.socket_file(@port)
end
stop() click to toggle source

Stop the DRb server and delete the socket file

Returns nothing

# File lib/god/socket.rb, line 64
def stop
  DRb.stop_service
  FileUtils.rm_f(self.socket_file)
end

Private Instance Methods

start() click to toggle source

Start the DRb server. Abort if there is already a running god instance on the socket.

Returns nothing

# File lib/god/socket.rb, line 75
def start
  begin
    @drb ||= DRb.start_service(self.socket, self)
    applog(nil, :info, "Started on #{DRb.uri}")
  rescue Errno::EADDRINUSE
    applog(nil, :info, "Socket already in use")
    server = DRbObject.new(nil, self.socket)

    begin
      Timeout.timeout(5) do
        server.ping
      end
      abort "Socket #{self.socket} already in use by another instance of god"
    rescue StandardError, Timeout::Error
      applog(nil, :info, "Socket is stale, reopening")
      File.delete(self.socket_file) rescue nil
      @drb ||= DRb.start_service(self.socket, self)
      applog(nil, :info, "Started on #{DRb.uri}")
    end
  end

  if File.exists?(self.socket_file)
    if @user
      user_method = @user.is_a?(Integer) ? :getpwuid : :getpwnam
      uid = Etc.send(user_method, @user).uid
      gid = Etc.send(user_method, @user).gid
    end
    if @group
      group_method = @group.is_a?(Integer) ? :getgrgid : :getgrnam
      gid = Etc.send(group_method, @group).gid
    end

    File.chmod(Integer(@perm), socket_file) if @perm
    File.chown(uid, gid, socket_file) if uid or gid
  end
end