Sha256: 3aff82a288424d0f89506f9c18cb804c9ccf213c1febffbf4c335cb0e4be1cf9

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'drb'

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

module God

  class Socket
    attr_reader :port
    
    def self.socket_file(port)
      "/tmp/god.#{port}.sock"
    end
    
    def self.socket(port)
      "drbunix://#{self.socket_file(port)}"
    end
    
    def socket_file
      self.class.socket_file(@port)
    end
    
    def socket
      self.class.socket(@port)
    end
    
    def initialize(port = nil)
      @port = port
      start
    end
    
    def ping
      true
    end
    
    def method_missing(*args, &block)
      God.send(*args, &block)
    end
    
    private
    
    def start
      begin
        @drb ||= DRb.start_service(self.socket, self)
        LOG.log(nil, :info, "Started on #{DRb.uri}")
      rescue Errno::EADDRINUSE
        DRb.start_service
        server = DRbObject.new(nil, self.socket)
        
        begin
          server.ping
          abort "Socket #{self.socket} already in use by another instance of god"
        rescue
          File.delete(self.socket_file) rescue nil
          @drb ||= DRb.start_service(self.socket, self)
        end
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
god-0.5.0 lib/god/socket.rb