Class: MultiProcess::Process
- Inherits:
-
Object
- Object
- MultiProcess::Process
- Extended by:
- Forwardable
- 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
-
#childprocess ⇒ Object
readonly
ChildProcess object.
-
#command ⇒ Object
readonly
Command as full string.
-
#title ⇒ Object
readonly
Process title used in e.g.
Working Directory collapse
-
#dir ⇒ Object
Working directory for child process.
Receiver collapse
-
#receiver ⇒ Object
Current receiver.
Process collapse
-
#available!(opts = {}) ⇒ Object
Wait until the process is available.
-
#available? ⇒ Boolean
Check if the process is available.
-
#initialize(*args) ⇒ Process
constructor
A new instance of Process.
-
#run(opts = {}) ⇒ Object
Start process and wait until it’s finished.
-
#run!(opts = {}) ⇒ Object
Start process and wait until it’s finished.
-
#start ⇒ Object
Start process.
-
#started? ⇒ Boolean
Check if process was started.
-
#stop(*args) ⇒ Object
Stop process.
-
#wait(opts = {}) ⇒ Object
Wait until process finished.
-
#wait!(opts = {}) ⇒ Object
Wait until process finished.
Environment collapse
-
#clean_env? ⇒ Boolean
Check if environment will be cleaned up for process.
-
#env ⇒ Object
Return current environment.
-
#env=(env) ⇒ Object
Set environment.
Constructor Details
#initialize(*args) ⇒ Process
Returns a new instance of Process.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/multi_process/process.rb', line 26 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? || !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
#childprocess ⇒ Object (readonly)
ChildProcess object.
24 25 26 |
# File 'lib/multi_process/process.rb', line 24 def childprocess @childprocess end |
#command ⇒ Object (readonly)
Command as full string.
21 22 23 |
# File 'lib/multi_process/process.rb', line 21 def command @command end |
#dir ⇒ Object
Working directory for child process.
150 151 152 |
# File 'lib/multi_process/process.rb', line 150 def dir @dir end |
#receiver ⇒ Object
Current receiver. Defaults to MultiProcess::Logger.global.
190 191 192 |
# File 'lib/multi_process/process.rb', line 190 def receiver @receiver end |
#title ⇒ Object (readonly)
Process title used in e.g. logger
18 19 20 |
# File 'lib/multi_process/process.rb', line 18 def title @title end |
Instance Method Details
#available!(opts = {}) ⇒ Object
Wait until the process is available. See #available?.
113 114 115 116 117 118 119 120 121 |
# File 'lib/multi_process/process.rb', line 113 def available!(opts = {}) timeout = opts[:timeout] ? Float(opts[:timeout]) : MultiProcess::DEFAULT_TIMEOUT Timeout.timeout timeout do sleep 0.2 until available? end rescue Timeout::Error raise Timeout::Error.new "Process #{pid} failed to start." end |
#available? ⇒ Boolean
Check if the process is available. What available means can be defined by subclasses e.g. a server process can check if its port is reachable.
By default a process is available if #alive? returns true.
103 104 105 |
# File 'lib/multi_process/process.rb', line 103 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_unbundled_env to remove bundler environment variables.
168 169 170 |
# File 'lib/multi_process/process.rb', line 168 def clean_env? !!@env_clean end |
#env ⇒ Object
Return current environment.
174 175 176 |
# File 'lib/multi_process/process.rb', line 174 def env @env ||= {} end |
#env=(env) ⇒ Object
Set environment.
180 181 182 183 184 |
# File 'lib/multi_process/process.rb', line 180 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.
133 134 135 136 |
# File 'lib/multi_process/process.rb', line 133 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!.
142 143 144 145 |
# File 'lib/multi_process/process.rb', line 142 def run!(opts = {}) start wait!(opts) end |
#start ⇒ Object
Start process.
Started processes will be stopped when ruby VM exists by hooking into at_exit.
81 82 83 84 85 86 87 88 |
# File 'lib/multi_process/process.rb', line 81 def start return false if started? at_exit { stop } receiver&.(self, :sys, command) start_childprocess @started = true end |
#started? ⇒ Boolean
Check if process was started.
125 126 127 |
# File 'lib/multi_process/process.rb', line 125 def started? !!@started end |
#stop(*args) ⇒ Object
Stop process.
Will call ‘ChildProcess#stop`.
94 95 96 |
# File 'lib/multi_process/process.rb', line 94 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.
54 55 56 57 58 59 60 |
# File 'lib/multi_process/process.rb', line 54 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.
69 70 71 72 73 74 |
# File 'lib/multi_process/process.rb', line 69 def wait!(opts = {}) wait(opts) return if exit_code.zero? raise ::MultiProcess::ProcessError.new(self, "Process #{pid} exited with code #{exit_code}") end |