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

#serverObject

Server wrapper given as argument to ‘server` action.



15
16
17
# File 'lib/multi_process/process/rails.rb', line 15

def server
  @server
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'lib/multi_process/process/rails.rb', line 40

def available?
  raise ArgumentError.new "Cannot check availability for port #{port}." if port.zero?

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

#configure(opts) ⇒ Object

Load environment options from initialize options.



51
52
53
54
55
56
# File 'lib/multi_process/process/rails.rb', line 51

def configure(opts)
  super
  puts 'Configure RAILS'
  self.dir = Dir.pwd
  self.dir = opts[:dir].to_s if opts[:dir]
end

#initialize(opts = {}) ⇒ Object



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

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

  super(*server_command, opts)
end

#portObject



36
37
38
# File 'lib/multi_process/process/rails.rb', line 36

def port
  @port ||= free_port
end

#port=(port) ⇒ Object



32
33
34
# File 'lib/multi_process/process/rails.rb', line 32

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

#server_commandObject



24
25
26
# File 'lib/multi_process/process/rails.rb', line 24

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

#start_childprocess(*args) ⇒ Object



58
59
60
# File 'lib/multi_process/process/rails.rb', line 58

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