Class: MultiProcess::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_process/process.rb,
lib/multi_process/process/rails.rb,
lib/multi_process/process/bundle_exec.rb

Overview

Describes a single process that can be configured and run.

Process basically is just a thin wrapper around ChildProcess.

Defined Under Namespace

Modules: BundleExec, Rails

Process collapse

Working Directory collapse

Receiver collapse

Process collapse

Environment collapse

Constructor Details

#initialize(*args) ⇒ Process

Returns a new instance of Process.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/multi_process/process.rb', line 23

def initialize(*args)
  args.flatten!
  opts = (args.last.is_a?(Hash) ? args.pop : {})

  @title        = opts[:title].to_s || args.first.to_s.strip.split(/\s+/, 2)[0]
  @command      = args.map {|arg| (/\A[\s"']+\z/.match?(arg) ? arg.inspect : arg).gsub '"', '\"' }.join(' ')
  @childprocess = create_childprocess(*args)

  @env          = opts[:env] if opts[:env].is_a?(Hash)
  @env_clean    = opts[:clean_env].nil? ? true : !opts[:clean_env].nil?

  self.receiver = opts[:receiver] || MultiProcess::Logger.global

  self.dir      = Dir.pwd
  self.dir      = opts[:dir].to_s if opts[:dir]
end

Instance Attribute Details

#childprocessObject (readonly)

ChildProcess object.



21
22
23
# File 'lib/multi_process/process.rb', line 21

def childprocess
  @childprocess
end

#commandObject (readonly)

Command as full string.



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

def command
  @command
end

#dirObject

Working directory for child process.



147
148
149
# File 'lib/multi_process/process.rb', line 147

def dir
  @dir
end

#receiverObject

Current receiver. Defaults to MultiProcess::Logger.global.



187
188
189
# File 'lib/multi_process/process.rb', line 187

def receiver
  @receiver
end

#titleObject (readonly)

Process title used in e.g. logger



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

def title
  @title
end

Instance Method Details

#available!(opts = {}) ⇒ Object

Wait until process is available. See #available?.

Parameters:

  • opts (Hash) (defaults to: {})

    Options.

Options Hash (opts):

  • :timeout (Integer)

    Timeout in seconds. Will raise Timeout::Error if timeout is reached.



110
111
112
113
114
115
116
117
118
# File 'lib/multi_process/process.rb', line 110

def available!(opts = {})
  timeout = opts[:timeout] ? opts[:timeout].to_i : MultiProcess::DEFAULT_TIMEOUT

  Timeout.timeout timeout do
    sleep 0.2 until available?
  end
rescue Timeout::Error
  raise Timeout::Error.new "Server #{id.inspect} on port #{port} didn't get up after #{timeout} seconds..."
end

#available?Boolean

Check if server is available. What available means can be defined by subclasses e.g. a server process can check if server port is reachable.

By default is process if available if alive? returns true.

Returns:

  • (Boolean)


100
101
102
# File 'lib/multi_process/process.rb', line 100

def available?
  alive?
end

#clean_env?Boolean

Check if environment will be cleaned up for process.

Currently that includes wrapping the process start in Bundler.with_clean_env to remove bundler environment variables.

Returns:

  • (Boolean)


165
166
167
# File 'lib/multi_process/process.rb', line 165

def clean_env?
  !!@env_clean
end

#envObject

Return current environment.



171
172
173
# File 'lib/multi_process/process.rb', line 171

def env
  @env ||= {}
end

#env=(env) ⇒ Object

Set environment.



177
178
179
180
181
# File 'lib/multi_process/process.rb', line 177

def env=(env)
  raise ArgumentError.new 'Environment must be a Hash.' unless env.is_a?(Hash)

  @env = env
end

#run(opts = {}) ⇒ Object

Start process and wait until it’s finished.

Given arguments will be passed to #wait.



130
131
132
133
# File 'lib/multi_process/process.rb', line 130

def run(opts = {})
  start
  wait opts
end

#run!(opts = {}) ⇒ Object

Start process and wait until it’s finished.

Given arguments will be passed to #wait!.



139
140
141
142
# File 'lib/multi_process/process.rb', line 139

def run!(opts = {})
  start
  wait!(opts)
end

#startObject

Start process.

Started processes will be stopped when ruby VM exists by hooking into at_exit.



78
79
80
81
82
83
84
85
# File 'lib/multi_process/process.rb', line 78

def start
  return false if started?

  at_exit { stop }
  receiver&.message(self, :sys, command)
  start_childprocess
  @started = true
end

#started?Boolean

Check if process was started.

Returns:

  • (Boolean)


122
123
124
# File 'lib/multi_process/process.rb', line 122

def started?
  !!@started
end

#stop(*args) ⇒ Object

Stop process.

Will call ‘ChildProcess#stop`.



91
92
93
# File 'lib/multi_process/process.rb', line 91

def stop(*args)
  childprocess.stop(*args) if started?
end

#wait(opts = {}) ⇒ Object

Wait until process finished.

If no timeout is given it will wait definitely.

Parameters:

  • opts (Hash) (defaults to: {})

    Options.

Options Hash (opts):

  • :timeout (Integer)

    Timeout to wait in seconds.



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

def wait(opts = {})
  if opts[:timeout]
    childprocess.wait_for_exit opts[:timeout]
  else
    childprocess.wait
  end
end

#wait!(opts = {}) ⇒ Object

Wait until process finished.

If no timeout is given it will wait definitely.

Parameters:

  • opts (Hash) (defaults to: {})

    Options.

Options Hash (opts):

  • :timeout (Integer)

    Timeout to wait in seconds.

Raises:



66
67
68
69
70
71
# File 'lib/multi_process/process.rb', line 66

def wait!(opts = {})
  wait(opts)
  return if exit_code.zero?

  raise ::MultiProcess::ProcessError.new(self, "Process #{pid} exited with code #{exit_code}")
end