Module: MultiProcess::Process::Rails

Defined in:
lib/multi_process/process/rails.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#portObject

Port server should be running on.

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



19
20
21
# File 'lib/multi_process/process/rails.rb', line 19

def port
  @port
end

#serverObject

Server wrapper given as argument to ‘server` action.



13
14
15
# File 'lib/multi_process/process/rails.rb', line 13

def server
  @server
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# 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('127.0.0.1', port).close
  true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  false
end

#configure(opts) ⇒ Object

Load environment options from initialize options.



55
56
57
58
59
60
# 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

#initialize(opts = {}) ⇒ Object



21
22
23
24
25
26
# 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

#server_commandObject



28
29
30
# 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) ⇒ Object



62
63
64
# File 'lib/multi_process/process/rails.rb', line 62

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