Class: ZK::Server::Command

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/zk-server/command.rb

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initializeCommand

Returns a new instance of Command.



8
9
10
11
12
# File 'lib/zk-server/command.rb', line 8

def initialize
  @mutex  = Mutex.new
  @cond   = ConditionVariable.new
  @shutdown_requested = false
end

Instance Method Details

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zk-server/command.rb', line 24

def run
  opts = Slop.parse(:help => true, :strict => true) do
    banner "zk-server [opts] runs a ZooKeeper server in the foreground"
    on :d,  :base_dir=,       "defaults to #{ZK::Server::Config.default_base_dir}" 
    on      :no_force_sync,   "don't force fsync on every snapshot"
    on      :skip_acl,        'skip acl checks'
    on :p,  :port=,           'port to listen on', :as => :integer
    on      :jvm_flags=,      'additional JVM flags to pass'
    on      :snap_count=,     'how often to take a snapshot, default 100_000', :as => :integer
  end

  return if opts.help?

  hash = opts.to_hash

  hash.delete(:help)
  hash[:force_sync] = !hash.delete(:no_force_sync)

  if flags = hash.delete(:jvm_flags)
    hash[:jvm_flags] = flags.split(' ')
  end

  hash.reject! { |k,v| v.nil? }

  config = ZK::Server::Config.new(hash)

  spawn_shutdown_handler


  %w[HUP INT].each do |sig|
    trap(sig) do
      @mutex.synchronize do
        $stderr.puts "trapped #{sig}, shutting down"
        @shutdown_requested = true
        @cond.broadcast
      end
    end
  end

  @server = ZK::Server.new(:config => config)
  @server.run

  unless @server.join
    $stderr.puts "server exited with status #{@server.status.inspect}"
    st = @server.status
    exit st.exited? ? st.exitstatus : 42
  end
end

#spawn_shutdown_handlerObject



14
15
16
17
18
19
20
21
22
# File 'lib/zk-server/command.rb', line 14

def spawn_shutdown_handler
  @shutdown_thread ||= Thread.new do
    @mutex.synchronize do
      @cond.wait until @shutdown_requested
      logger.debug { "shutdown thread awakened! shutting down server!" }
      @server.shutdown if @server
    end
  end
end