Class: MultiProcess::Process
- Inherits:
-
Object
- Object
- MultiProcess::Process
- 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 process is available.
-
#available? ⇒ Boolean
Check if server is available.
-
#initialize(*args) ⇒ Process
constructor
A new instance of Process.
-
#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.
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.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/multi_process/process.rb', line 21 def initialize(*args) args.flatten! opts = (Hash === args.last ? args.pop : {}) @title = opts[:title].to_s || args.first.to_s.strip.split(/\s+/, 2)[0] @command = args.map { |arg| (arg =~ /\A[\s"']+\z/ ? arg.inspect : arg).gsub '"', '\"' }.join(' ') @childprocess = create_childprocess *args @env = opts[:env] if Hash === opts[:env] @env_clean = opts[:clean_env].nil? ? true : !!opts[:clean_env] 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.
19 20 21 |
# File 'lib/multi_process/process.rb', line 19 def childprocess @childprocess end |
#command ⇒ Object (readonly)
Command as full string.
16 17 18 |
# File 'lib/multi_process/process.rb', line 16 def command @command end |
#dir ⇒ Object
Working directory for child process.
122 123 124 |
# File 'lib/multi_process/process.rb', line 122 def dir @dir end |
#receiver ⇒ Object
Current receiver. Defaults to ‘MultiProcess::Logger.global`.
161 162 163 |
# File 'lib/multi_process/process.rb', line 161 def receiver @receiver end |
#title ⇒ Object (readonly)
Process title used in e.g. logger
13 14 15 |
# File 'lib/multi_process/process.rb', line 13 def title @title end |
Instance Method Details
#available!(opts = {}) ⇒ Object
Wait until process is available. See #available?.
94 95 96 97 98 99 100 101 102 |
# File 'lib/multi_process/process.rb', line 94 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 => ex 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.
84 85 86 |
# File 'lib/multi_process/process.rb', line 84 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.
140 141 142 |
# File 'lib/multi_process/process.rb', line 140 def clean_env? !!@env_clean end |
#env ⇒ Object
Return current environment.
146 147 148 |
# File 'lib/multi_process/process.rb', line 146 def env @env ||= {} end |
#env=(env) ⇒ Object
Set environment.
152 153 154 155 |
# File 'lib/multi_process/process.rb', line 152 def env=(env) fail ArgumentError.new 'Environment must be a Hash.' unless hash === env @env = env end |
#run(opts = {}) ⇒ Object
Start process and wait until it’s finished.
Given arguments will be passed to #wait.
114 115 116 117 |
# File 'lib/multi_process/process.rb', line 114 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`.
62 63 64 65 66 67 68 69 |
# File 'lib/multi_process/process.rb', line 62 def start return false if started? at_exit { stop } receiver.(self, :sys, command) if receiver start_childprocess @started = true end |
#started? ⇒ Boolean
Check if process was started.
106 107 108 |
# File 'lib/multi_process/process.rb', line 106 def started? !!@started end |
#stop(*args) ⇒ Object
Stop process.
Will call ‘ChildProcess#stop`.
75 76 77 |
# File 'lib/multi_process/process.rb', line 75 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.
49 50 51 52 53 54 55 |
# File 'lib/multi_process/process.rb', line 49 def wait(opts = {}) if opts[:timeout] childprocess.wait_for_exit opts[:timeout] else childprocess.wait end end |