Sha256: 79f78dce0b04da52c82a6199720a52cf42dba9dbe2cefc7e4aa914b78ea8d547

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

class MultiProcess::Process
  # Provides functionality for a process that is a rails server
  # process.
  #
  # Include this module if required.
  #
  # Functions include port generation, default server command and
  # availability check based on if server socket is reachable.
  #
  module Rails
    # Server wrapper given as argument to `server` action.
    #
    attr_reader :server

    # Port server should be running on.
    #
    # Default will be a free port determined when process is created.
    #
    attr_reader :port

    def initialize(opts = {})
      self.server = opts[:server] if opts[:server]
      self.port   = opts[:port]   if opts[:port]

      super *server_command, opts
    end

    def server_command
      ['rails', 'server', server, '--port', port].reject(&:nil?).map(&:to_s)
    end

    def server=(server)
      @server = server.to_s.empty? ? nil : server.to_s
    end

    def port=(port)
      @port = port.to_i == 0 ? free_port : port.to_i
    end

    def port
      @port ||= free_port
    end

    def available?
      fail ArgumentError.new "Cannot check availability for port #{port}." if port == 0

      TCPSocket.new('127.0.0.1', port).close
      true
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
      false
    end

    # Load environment options from initialize options.
    #
    def configure(opts)
      super
      puts 'Configure RAILS'
      self.dir = Dir.pwd
      self.dir = opts[:dir].to_s if opts[:dir]
    end

    def start_childprocess(*args)
      Dir.chdir(dir) { super }
    end

    private

    def free_port
      socket = Socket.new(:INET, :STREAM, 0)
      socket.bind(Addrinfo.tcp('127.0.0.1', 0))
      socket.local_address.ip_port
    ensure
      socket.close if socket
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
multi_process-1.0.0 lib/multi_process/process/rails.rb
multi_process-0.5.2 lib/multi_process/process/rails.rb
multi_process-0.5.1 lib/multi_process/process/rails.rb
multi_process-0.5.0 lib/multi_process/process/rails.rb