Class: Zeusd::Daemon

Inherits:
Object
  • Object
show all
Includes:
Hooks
Defined in:
lib/zeusd/daemon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Daemon

Returns a new instance of Daemon.



29
30
31
32
33
# File 'lib/zeusd/daemon.rb', line 29

def initialize(options = {})
  @cwd         = Pathname.new(options[:cwd] || Dir.pwd).realpath
  @verbose     = !!options[:verbose]
  @interpreter = Interpreter.new
end

Instance Attribute Details

#child_processObject (readonly)

Returns the value of attribute child_process.



10
11
12
# File 'lib/zeusd/daemon.rb', line 10

def child_process
  @child_process
end

#cwdObject (readonly)

Returns the value of attribute cwd.



10
11
12
# File 'lib/zeusd/daemon.rb', line 10

def cwd
  @cwd
end

#interpreterObject (readonly)

Returns the value of attribute interpreter.



10
11
12
# File 'lib/zeusd/daemon.rb', line 10

def interpreter
  @interpreter
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



10
11
12
# File 'lib/zeusd/daemon.rb', line 10

def log_file
  @log_file
end

#verboseObject (readonly)

Returns the value of attribute verbose.



10
11
12
# File 'lib/zeusd/daemon.rb', line 10

def verbose
  @verbose
end

Instance Method Details

#loaded?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/zeusd/daemon.rb', line 75

def loaded?
  interpreter.complete?
end

#processObject



71
72
73
# File 'lib/zeusd/daemon.rb', line 71

def process
  @process ||= Process.all.find {|p| !!p.command[/zeus.*start$/] && p.cwd == cwd }
end

#restart!(options = {}) ⇒ Object



49
50
51
# File 'lib/zeusd/daemon.rb', line 49

def restart!(options = {})
  stop!.start!(options)
end

#socket_fileObject



83
84
85
# File 'lib/zeusd/daemon.rb', line 83

def socket_file
  cwd.join('.zeus.sock')
end

#start!(options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zeusd/daemon.rb', line 35

def start!(options = {})
  start_child_process!

  @process = Zeusd::Process.find(child_process.pid)

  if options.fetch(:block, false)
    sleep(0.1) until loaded?
  end

  self
ensure
  run_hook :after_start!
end

#stop!Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zeusd/daemon.rb', line 53

def stop!
  return self unless process

  # Kill process tree and wait for exits
  process.kill!(:recursive => true, :wait => true)

  # Check for remaining processes
  if[process, process.descendants].flatten.select(&:alive?).any?
    raise DaemonException, "Unable to KILL processes: " + alive_processes.join(', ')
  end

  @process = nil

  self
ensure
  run_hook :after_stop!
end

#verbose?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/zeusd/daemon.rb', line 87

def verbose?
  !!verbose
end