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.



32
33
34
35
36
# File 'lib/zeusd/daemon.rb', line 32

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.



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

def child_process
  @child_process
end

#cwdObject (readonly)

Returns the value of attribute cwd.



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

def cwd
  @cwd
end

#interpreterObject (readonly)

Returns the value of attribute interpreter.



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

def interpreter
  @interpreter
end

#log_fileObject (readonly)

Returns the value of attribute log_file.



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

def log_file
  @log_file
end

#verboseObject (readonly)

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#loaded?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/zeusd/daemon.rb', line 80

def loaded?
  interpreter.complete?
end

#log_event(type, details = nil) ⇒ Object



84
85
86
87
88
# File 'lib/zeusd/daemon.rb', line 84

def log_event(type, details = nil)
  logger.info("EVENT") do
    ">>> #{type.to_s.upcase}" + (details ? (" >>> " + JSON.pretty_generate(details)) : "")
  end
end

#loggerObject



90
91
92
93
94
95
96
97
98
# File 'lib/zeusd/daemon.rb', line 90

def logger
  @logger ||= Logger.new(log_file.to_path).tap do |x|
    x.formatter = proc do |severity, datetime, type, msg|
      prefix    = "[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}][#{type}]"
      msg       = msg.chomp.gsub("\n", "\n".ljust(prefix.length) + "\e[36m|\e[0m ")
      "\e[36m#{prefix}\e[0m" + " #{msg}\n"
    end
  end
end

#processObject



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

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

#restart!(options = {}) ⇒ Object



52
53
54
# File 'lib/zeusd/daemon.rb', line 52

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

#start!(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zeusd/daemon.rb', line 38

def start!(options = {})
  start_child_process!

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

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

  run_hook :after_start!

  self
end

#stop!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zeusd/daemon.rb', line 56

def stop!
  run_hook :before_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

  run_hook :after_stop!

  self
end

#zeus_log_fileObject



108
109
110
111
112
# File 'lib/zeusd/daemon.rb', line 108

def zeus_log_file
  cwd.join('.zeus.log').tap do |path|
    FileUtils.touch(path.to_path)
  end
end

#zeus_socket_fileObject



100
101
102
# File 'lib/zeusd/daemon.rb', line 100

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