module MultiProcess::Process::Rails

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.

Attributes

port[R]

Port server should be running on.

Default will be a free port determined when process is created.

server[R]

Server wrapper given as argument to `server` action.

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method
# File lib/multi_process/process/rails.rb, line 21
def initialize(opts = {})
  self.server = opts[:server] if opts[:server]
  self.port   = opts[:port]   if opts[:port]

  super *server_command, opts
end

Public Instance Methods

available?() click to toggle source
# File lib/multi_process/process/rails.rb, line 44
def available?
  fail ArgumentError.new "Cannot check availability for port #{port}." if port == 0

  TCPSocket.new('localhost', port).close
  true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  false
end
configure(opts) click to toggle source

Load environment options from initialize options.

Calls superclass method
# File lib/multi_process/process/rails.rb, line 55
def configure(opts)
  super
  puts 'Configure RAILS'
  self.dir = Dir.pwd
  self.dir = opts[:dir].to_s if opts[:dir]
end
port=(port) click to toggle source
# File lib/multi_process/process/rails.rb, line 36
def port=(port)
  @port = port.to_i == 0 ? free_port : port.to_i
end
server=(server) click to toggle source
# File lib/multi_process/process/rails.rb, line 32
def server=(server)
  @server = server.to_s.empty? ? nil : server.to_s
end
server_command() click to toggle source
# File lib/multi_process/process/rails.rb, line 28
def server_command
  ['rails', 'server', server, '--port', port].reject(&:nil?).map(&:to_s)
end
start_childprocess(*args) click to toggle source
Calls superclass method
# File lib/multi_process/process/rails.rb, line 62
def start_childprocess(*args)
  Dir.chdir(dir) { super }
end

Private Instance Methods

free_port() click to toggle source
# File lib/multi_process/process/rails.rb, line 68
def free_port
  socket = Socket.new(:INET, :STREAM, 0)
  socket.bind(Addrinfo.tcp('localhost', 0))
  socket.local_address.ip_port
ensure
  socket.close if socket
end