Class: ZK::Server::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
FileUtils, Logging
Defined in:
lib/zk-server/base.rb

Overview

common base class for Process and JavaEmbedded classes

Direct Known Subclasses

JavaEmbedded, SubProcess

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
23
24
25
# File 'lib/zk-server/base.rb', line 17

def initialize(opts={})
  @child_startup_timeout = opts.delete(:child_startup_timeout) || 6

  @run_called = false
  @config     = opts[:config] || Config.new(opts)

  @mutex      = Monitor.new
  @exit_cond  = @mutex.new_cond
end

Instance Attribute Details

#configObject

the Config object that will be used to configure this Process



15
16
17
# File 'lib/zk-server/base.rb', line 15

def config
  @config
end

Instance Method Details

#clobber!Object

removes all files related to this instance runs #shutdown first



29
30
31
32
# File 'lib/zk-server/base.rb', line 29

def clobber!
  shutdown
  FileUtils.rm_rf(base_dir)
end

#create_files!Object (protected)



69
70
71
72
73
74
75
76
# File 'lib/zk-server/base.rb', line 69

def create_files!
  mkdir_p base_dir
  mkdir_p data_dir
  write_myid!
  write_zoo_cfg!
  write_log4j_properties!
  mkdir_p(File.dirname(stdio_redirect_path))
end

#ping?Boolean Also known as: pingable?

can we connect to the server, issue an 'ruok', and receive an 'imok'?

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'lib/zk-server/base.rb', line 45

def ping?
  TCPSocket.open('localhost', client_port) do |sock|
    sock.puts('ruok')
    sock.read == 'imok'
  end
rescue
  false
end

#runObject

start the server

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/zk-server/base.rb', line 56

def run
  raise NotImplementedError
end

#running?Boolean

is the server running?

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def running?
  raise NotImplementedError
end

#shutdownObject

shut down the server, gracefully if possible, with force if necessary

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/zk-server/base.rb', line 40

def shutdown
  raise NotImplementedError
end

#wait_until_ping(timeout = 5) ⇒ Object (protected)



61
62
63
64
65
66
67
# File 'lib/zk-server/base.rb', line 61

def wait_until_ping(timeout=5)
  times_up = timeout ? Time.now + timeout : 0
  while Time.now < times_up
    return true if ping?
  end
  false
end

#write_log4j_properties!Object (protected)



78
79
80
81
82
# File 'lib/zk-server/base.rb', line 78

def write_log4j_properties!
  unless File.exists?(log4j_props_path)
    cp ZK::Server.default_log4j_props_path, log4j_props_path
  end
end

#write_myid!Object (protected)



84
85
86
87
88
# File 'lib/zk-server/base.rb', line 84

def write_myid!
  File.open(zoo_myid_path, 'w') do |io|
    io.puts myid
  end
end

#write_zoo_cfg!Object (protected)



90
91
92
93
94
95
# File 'lib/zk-server/base.rb', line 90

def write_zoo_cfg!
  File.open(zoo_cfg_path, 'w') do |fp|
    fp.puts(config.to_config_file_str)
    fp.fsync
  end
end