Class: Zeusd::Daemon
Instance Attribute Summary collapse
-
#child_process ⇒ Object
readonly
Returns the value of attribute child_process.
-
#cwd ⇒ Object
readonly
Returns the value of attribute cwd.
-
#interpreter ⇒ Object
readonly
Returns the value of attribute interpreter.
-
#log_file ⇒ Object
readonly
Returns the value of attribute log_file.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Daemon
constructor
A new instance of Daemon.
- #loaded? ⇒ Boolean
- #log_event(type, details = nil) ⇒ Object
- #logger ⇒ Object
- #process ⇒ Object
- #restart!(options = {}) ⇒ Object
- #start!(options = {}) ⇒ Object
- #stop! ⇒ Object
- #zeus_log_file ⇒ Object
- #zeus_socket_file ⇒ Object
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( = {}) @cwd = Pathname.new([:cwd] || Dir.pwd).realpath @verbose = !![:verbose] @interpreter = Interpreter.new end |
Instance Attribute Details
#child_process ⇒ Object (readonly)
Returns the value of attribute child_process.
30 31 32 |
# File 'lib/zeusd/daemon.rb', line 30 def child_process @child_process end |
#cwd ⇒ Object (readonly)
Returns the value of attribute cwd.
30 31 32 |
# File 'lib/zeusd/daemon.rb', line 30 def cwd @cwd end |
#interpreter ⇒ Object (readonly)
Returns the value of attribute interpreter.
30 31 32 |
# File 'lib/zeusd/daemon.rb', line 30 def interpreter @interpreter end |
#log_file ⇒ Object (readonly)
Returns the value of attribute log_file.
30 31 32 |
# File 'lib/zeusd/daemon.rb', line 30 def log_file @log_file end |
#verbose ⇒ Object (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
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 |
#logger ⇒ Object
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 |
#process ⇒ Object
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!( = {}) stop!.start!() 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!( = {}) start_child_process! @process = Zeusd::Process.find(child_process.pid) if .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_file ⇒ Object
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_file ⇒ Object
100 101 102 |
# File 'lib/zeusd/daemon.rb', line 100 def zeus_socket_file cwd.join('.zeus.sock') end |