Class: ProcessWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/nsq-cluster/process_wrapper.rb

Direct Known Subclasses

Nsqadmin, Nsqd, Nsqlookupd

Constant Summary collapse

HTTPCHECK_INTERVAL =
0.01

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ProcessWrapper



5
6
7
# File 'lib/nsq-cluster/process_wrapper.rb', line 5

def initialize(opts = {})
  @silent = opts[:silent]
end

Instance Method Details

#another_instance_is_running?Boolean



34
35
36
37
38
39
40
# File 'lib/nsq-cluster/process_wrapper.rb', line 34

def another_instance_is_running?
  if respond_to?(:http_port)
    http_port_open?
  else
    false
  end
end

#argsObject



48
49
50
# File 'lib/nsq-cluster/process_wrapper.rb', line 48

def args
  raise 'you have to override this in a subclass as well, buddy'
end

#block_until_runningObject



62
63
64
65
66
67
68
# File 'lib/nsq-cluster/process_wrapper.rb', line 62

def block_until_running
  if respond_to?(:http_port) && respond_to?(:host)
    wait_for_http_port
  else
    raise "Can't block without http port and host"
  end
end

#block_until_stoppedObject



71
72
73
74
75
76
77
# File 'lib/nsq-cluster/process_wrapper.rb', line 71

def block_until_stopped
  if respond_to?(:http_port) && respond_to?(:host)
    wait_for_no_http_port
  else
    raise "Can't block without http port and host"
  end
end

#commandObject



43
44
45
# File 'lib/nsq-cluster/process_wrapper.rb', line 43

def command
  raise 'you have to override this in a subclass, hotshot'
end

#destroyObject



24
25
26
# File 'lib/nsq-cluster/process_wrapper.rb', line 24

def destroy
  stop if running?
end

#outputObject



53
54
55
56
57
58
59
# File 'lib/nsq-cluster/process_wrapper.rb', line 53

def output
  if @silent
    '/dev/null'
  else
    :out
  end
end

#running?Boolean



29
30
31
# File 'lib/nsq-cluster/process_wrapper.rb', line 29

def running?
  !!@pid
end

#startObject



10
11
12
13
# File 'lib/nsq-cluster/process_wrapper.rb', line 10

def start
  raise "#{command} is already running" if running? || another_instance_is_running?
  @pid = spawn(command, *args, [:out, :err] => output)
end

#stopObject



16
17
18
19
20
21
# File 'lib/nsq-cluster/process_wrapper.rb', line 16

def stop
  raise "#{command} is not running" unless running?
  Process.kill('TERM', @pid)
  Process.waitpid(@pid)
  @pid = nil
end