Class: VCAP::Spec::ForkedComponent::Base
- Inherits:
-
Object
- Object
- VCAP::Spec::ForkedComponent::Base
- Defined in:
- lib/vcap/spec/forked_component/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#daemon ⇒ Object
Returns the value of attribute daemon.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#output_basedir ⇒ Object
readonly
Returns the value of attribute output_basedir.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#pid_filename ⇒ Object
readonly
Returns the value of attribute pid_filename.
-
#reopen_stdio ⇒ Object
Returns the value of attribute reopen_stdio.
Instance Method Summary collapse
-
#initialize(cmd, name, output_basedir = '/tmp', pid_filename = nil) ⇒ Base
constructor
A new instance of Base.
- #ready? ⇒ Boolean
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
- #wait_ready(timeout = 1) ⇒ Object
Constructor Details
#initialize(cmd, name, output_basedir = '/tmp', pid_filename = nil) ⇒ Base
Returns a new instance of Base.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/vcap/spec/forked_component/base.rb', line 21 def initialize(cmd, name, output_basedir='/tmp', pid_filename=nil) @cmd = cmd @name = name @output_basedir = output_basedir @pid_filename = pid_filename @reopen_stdio = true @daemon = false end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
12 13 14 |
# File 'lib/vcap/spec/forked_component/base.rb', line 12 def cmd @cmd end |
#daemon ⇒ Object
Returns the value of attribute daemon.
14 15 16 |
# File 'lib/vcap/spec/forked_component/base.rb', line 14 def daemon @daemon end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/vcap/spec/forked_component/base.rb', line 12 def name @name end |
#output_basedir ⇒ Object (readonly)
Returns the value of attribute output_basedir.
12 13 14 |
# File 'lib/vcap/spec/forked_component/base.rb', line 12 def output_basedir @output_basedir end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
12 13 14 |
# File 'lib/vcap/spec/forked_component/base.rb', line 12 def pid @pid end |
#pid_filename ⇒ Object (readonly)
Returns the value of attribute pid_filename.
12 13 14 |
# File 'lib/vcap/spec/forked_component/base.rb', line 12 def pid_filename @pid_filename end |
#reopen_stdio ⇒ Object
Returns the value of attribute reopen_stdio.
14 15 16 |
# File 'lib/vcap/spec/forked_component/base.rb', line 14 def reopen_stdio @reopen_stdio end |
Instance Method Details
#ready? ⇒ Boolean
73 74 75 |
# File 'lib/vcap/spec/forked_component/base.rb', line 73 def ready? raise NotImplementedError end |
#running? ⇒ Boolean
69 70 71 |
# File 'lib/vcap/spec/forked_component/base.rb', line 69 def running? VCAP.process_running?(@pid) end |
#start ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/vcap/spec/forked_component/base.rb', line 32 def start pid = fork do if @reopen_stdio fn = File.join(@output_basedir, "#{@name}.#{Process.pid}.out") outfile = File.new(fn, 'w+') $stderr.reopen(outfile) $stdout.reopen(outfile) end exec(@cmd) end if @pid_filename wait_for(5) { File.exists?(@pid_filename) } @pid = File.read(@pid_filename).chomp.to_i else @pid = pid end self end |
#stop ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/vcap/spec/forked_component/base.rb', line 55 def stop return unless @pid && VCAP.process_running?(@pid) if @daemon Process.kill('KILL', @pid) else Process.kill('TERM', @pid) Process.waitpid(@pid, 0) end FileUtils.rm_f(@pid_filename) if @pid_filename @pid = nil self end |
#wait_ready(timeout = 1) ⇒ Object
77 78 79 |
# File 'lib/vcap/spec/forked_component/base.rb', line 77 def wait_ready(timeout=1) wait_for { ready? } end |