Class: ZK::Server::SubProcess

Inherits:
Base
  • Object
show all
Defined in:
lib/zk-server/sub_process.rb

Overview

This encapsulates the logic of running the zookeeper server, as a sub-process. It is intended that it will be stopped and started with the process that starts it. We are not going to do daemonized process management.

By default, we will create a directory in the current working directory called 'zk-server' to store our data under (configurable).

Instance Attribute Summary collapse

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#clobber!, #create_files!, #ping?, #wait_until_ping, #write_log4j_properties!, #write_myid!, #write_zoo_cfg!

Methods included from Logging

#logger

Constructor Details

#initialize(opts = {}) ⇒ SubProcess

Returns a new instance of SubProcess.



16
17
18
19
20
21
22
23
# File 'lib/zk-server/sub_process.rb', line 16

def initialize(opts={})
  @exit_watching_thread = nil

  @pid = nil
  @status = nil

  super
end

Instance Attribute Details

#child_startup_timeoutObject

how long should we wait for the child to start responding to 'ruok'?



14
15
16
# File 'lib/zk-server/sub_process.rb', line 14

def child_startup_timeout
  @child_startup_timeout
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/zk-server/sub_process.rb', line 11

def status
  @status
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/zk-server/sub_process.rb', line 39

def alive?
  return false unless spawned?
  false|kill(0)
rescue Errno::ESRCH
  false
end

#fork_and_exec!Object (protected)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/zk-server/sub_process.rb', line 158

def fork_and_exec!
  @pid ||= 
    fork do                 # gah, use fork because 1.8.7 sucks
      3.upto(255) do |fd|
        begin
          if io = IO.new(fd)
            io.close
          end
        rescue
        end
      end

      $stderr.puts "stdio_redirect_path: #{stdio_redirect_path.inspect}"
      $stdout.reopen($stderr)
      $stderr.reopen(stdio_redirect_path, 'a')

      exec(*command_args)
    end
end

#joinObject

wait for sub-process to exit



26
27
28
29
30
31
32
# File 'lib/zk-server/sub_process.rb', line 26

def join
  @mutex.synchronize do
    logger.debug { "spawned process #{@pid}" }
    @exit_cond.wait_until { @status }
  end
  @status.success?
end

#kill(signal) ⇒ Object



88
89
90
91
# File 'lib/zk-server/sub_process.rb', line 88

def kill(signal)
  raise "No pid yet!" unless @pid
  Process.kill(signal, @pid)
end

#pidObject

the pid of our child process



84
85
86
# File 'lib/zk-server/sub_process.rb', line 84

def pid
  @pid
end

#runfalse, true

start the child, using the Base#config. we create the files necessary, fork the child, and wait 5s for the child to start responding to pings

Returns:

  • (false, true)

    false if run has already been called on this instance true if we hav



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/zk-server/sub_process.rb', line 99

def run
  return false if @run_called
  @run_called = true

  create_files!

  if ZK::Server.mri_187?
    fork_and_exec!
  elsif ZK::Server.jruby? and not ZK::Server.ruby_19?
    raise "You must run Jruby in 1.9 compatibility mode! I'm very sorry, i need Kernel.spawn"
  else
    spawn!
  end

  spawn_exit_watching_thread

  unless wait_until_ping(@child_startup_timeout)
    raise "Oh noes! something went wrong!" unless running?
  end

  at_exit { self.shutdown }

  true
end

#running?Boolean

true if the process was started and is still running

Returns:

  • (Boolean)


35
36
37
# File 'lib/zk-server/sub_process.rb', line 35

def running?
  spawned? and !@status and alive?
end

#shutdownObject

shutdown the child, wait for it to exit, ensure it is dead



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/zk-server/sub_process.rb', line 52

def shutdown
  if @pid
    return if @status

    @mutex.synchronize do
      %w[HUP TERM KILL].each do |signal|
        logger.debug { "sending #{signal} to #{@pid}" }

        return unless running? # jruby doesn't seem to get @status ?

        begin
          kill(signal)
        rescue Errno::ESRCH
          return true
        end

        @exit_cond.wait(5) # check running? on next pass
      end
    end

    unless @exit_watching_thread.join(3) == @exit_watching_thread
      logger.warn { "exit watching thread didn't exit after 3 sec" }
    end

    @pid = @exit_watching_thread = nil

    logger.debug { "@status: #{@status}" }
  end
  true
end

#spawn!Object (protected)



148
149
150
151
152
153
154
155
156
# File 'lib/zk-server/sub_process.rb', line 148

def spawn!
  @pid ||= (
    args = command_args()
    args << { :err => [:child, :out], :out => [stdio_redirect_path, 'w'] }
    ::Kernel.spawn({}, *command_args).tap do |pid|
      logger.debug { "Spawned process #{pid}" }
    end
  )
end

#spawn_exit_watching_threadObject (protected)



125
126
127
128
129
130
131
132
# File 'lib/zk-server/sub_process.rb', line 125

def spawn_exit_watching_thread
  @exit_watching_thread ||= Thread.new do
    _, @status = Process.wait2(@pid)
    @mutex.synchronize do
      @exit_cond.broadcast
    end
  end
end

#spawned?Boolean

have we started the child process?

Returns:

  • (Boolean)


47
48
49
# File 'lib/zk-server/sub_process.rb', line 47

def spawned?
  !!@pid
end

#wait_for_pid(timeout = 2) ⇒ Object (protected)

wait for up to timeout seconds to pass, polling for completion returns nil if the process didn't exit



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/zk-server/sub_process.rb', line 136

def wait_for_pid(timeout=2)
  times_up = timeout ? Time.now + timeout : 0

  while Time.now < times_up
    pid, stat = ::Process.wait2(@pid, ::Process::WNOHANG)
    return stat if stat
    sleep(0.01)
  end

  nil
end