Class: VCAP::Spec::ForkedComponent::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vcap/spec/forked_component/base.rb

Direct Known Subclasses

NatsServer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd, name, output_basedir = '/tmp', pid_filename = nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • cmd

    String Command to run

  • name

    String Short name for this component (e.g. ‘redis’)

  • output_basedir (defaults to: '/tmp')

    String Stderr/stdout will be placed under this directory

  • pid_filename (defaults to: nil)

    String If not nil, we ready the pid from this file instead of using the pid returned from fork



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

#cmdObject (readonly)

Returns the value of attribute cmd.



12
13
14
# File 'lib/vcap/spec/forked_component/base.rb', line 12

def cmd
  @cmd
end

#daemonObject

Returns the value of attribute daemon.



14
15
16
# File 'lib/vcap/spec/forked_component/base.rb', line 14

def daemon
  @daemon
end

#nameObject (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_basedirObject (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

#pidObject (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_filenameObject (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_stdioObject

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

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


73
74
75
# File 'lib/vcap/spec/forked_component/base.rb', line 73

def ready?
  raise NotImplementedError
end

#running?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/vcap/spec/forked_component/base.rb', line 69

def running?
  VCAP.process_running?(@pid)
end

#startObject



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

#stopObject



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