Permalink
Browse files

Moving logging and error logic out of the Daemon

  • Loading branch information...
veloper committed Mar 1, 2014
1 parent 2e9a2b2 commit 28c17d65aaf44be68e60f9768b2434c966607c01
Showing with 72 additions and 58 deletions.
  1. +20 −58 lib/zeusd/daemon.rb
  2. +4 −0 lib/zeusd/daemon_exception.rb
  3. +44 −0 lib/zeusd/daemon_logging.rb
  4. +4 −0 lib/zeusd/process.rb
View
@@ -1,37 +1,14 @@
-require 'logger'
require 'thread'
require 'childprocess'
require 'pathname'
-require 'hooks'
require 'file-tail'
-require 'json'
-module Zeusd
- class DaemonException < StandardError; end
+require 'zeusd/daemon_exception'
+require 'zeusd/daemon_logging'
+module Zeusd
class Daemon
- include Hooks
-
- define_hooks :before_action, :after_action, :after_output
-
- before_action do |action|
- details = {}
- details[:process] = process.attributes if process
- log_event("Before: #{action}", details)
- end
-
- after_action do |action|
- details = {}
- details[:process] = process.attributes if process
- log_event("After: #{action}", details)
- end
-
- after_output do |output|
- interpreter.translate(output)
- puts output if verbose
- end
-
- attr_reader :cwd, :verbose, :log_file, :interpreter, :child_process
+ attr_reader :cwd, :verbose, :interpreter, :child_process
def initialize(options = {})
@cwd = Pathname.new(options[:cwd] || Dir.pwd).realpath
@@ -40,8 +17,6 @@ def initialize(options = {})
end
def start!(options = {})
- run_hook :before_action, __method__
-
start_child_process!
@process = Zeusd::Process.find(child_process.pid)
@@ -50,24 +25,14 @@ def start!(options = {})
sleep(0.1) until loaded?
end
- run_hook :after_action, __method__
-
self
end
def restart!(options = {})
- run_hook :before_action, __method__
-
stop!.start!(options)
-
- run_hook :after_action, __method__
-
- self
end
def stop!
- run_hook :before_action, __method__
-
return self unless process
# Kill process tree and wait for exits
@@ -83,8 +48,6 @@ def stop!
@process = nil
- run_hook :after_action, __method__
-
self
end
@@ -106,27 +69,22 @@ def zeus_log_file
end
end
- protected
-
- def log_event(type, details = nil)
- logger.info do
- "\e[35m[Event] (#{type})\e[0m" + (!details.empty? ? " " + JSON.pretty_generate(details) : "")
- end
+ def to_json(*args)
+ {
+ :class => self.class.name,
+ :cwd => cwd.to_path,
+ :verbose => verbose,
+ :process => process
+ }.to_json(*args)
end
- def logger
- @logger ||= Logger.new(cwd.join('log', 'zeusd.log').to_path).tap do |x|
- x.formatter = proc do |severity, datetime, progname, msg|
- prefix = "[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}]"
- "\e[36m#{prefix}\e[0m" + " #{msg}\n"
- end
- end
- end
+ protected
def start_child_process!
- # Truncate and cast to File
+ # Truncate and cast to File instance
zeus_log_file.open("w") {}
- std_file = File.new(zeus_log_file, 'w+').tap{|x| x.sync = true}
+ std_file = File.new(zeus_log_file, 'w+')
+ std_file.sync = true
# Prep and Start child process
@child_process = ChildProcess.build("zeus", "start")
@@ -143,7 +101,10 @@ def start_child_process!
log.extend(File::Tail)
log.interval = 0.1
log.backward(100)
- log.tail {|line| run_hook(:after_output, line) }
+ log.tail do |line|
+ interpreter.translate(line)
+ puts line if verbose
+ end
end
end
@@ -153,5 +114,6 @@ def start_child_process!
@child_process
end
+ include Zeusd::DaemonLogging
end
end
@@ -0,0 +1,4 @@
+module Zeusd
+ class DaemonException < StandardError
+ end
+end
@@ -0,0 +1,44 @@
+require 'logger'
+require 'json'
+
+module Zeusd
+ module DaemonLogging
+
+ def log_file
+ cwd.join('log', 'zeusd.log')
+ end
+
+ def track(occurred, method, details = nil)
+ logger.info do
+ "\e[35m[Track] [#{occurred.to_s.upcase}] .#{method}()\e[0m" + (details ? " " + JSON.pretty_generate(details) : "")
+ end
+ end
+
+ def logger
+ @logger ||= Logger.new(log_file.to_path).tap do |l|
+ l.formatter = proc do |severity, datetime, progname, msg|
+ "\e[36m[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}]\e[0m" + " #{msg}\n"
+ end
+ end
+ end
+
+ def self.included(base)
+ tracked_methods = [:start!, :stop!, :restart!, :start_child_process!]
+ base.instance_eval do
+ tracked_methods.each do |method_name|
+ original_method = instance_method(method_name)
+ track = instance_method(:track)
+
+ define_method(method_name) do |*args, &block|
+ track.bind(self).call(:before, method_name, :args => args)
+ original_method.bind(self).call(*args, &block).tap do |x|
+ track.bind(self).call(:after, method_name, :return => x)
+ end
+ end
+ end
+ end
+ end
+
+
+ end
+end
View
@@ -18,6 +18,10 @@ def initialize(attributes_or_pid)
end
end
+ def to_json(*args)
+ attributes.to_json(*args)
+ end
+
def self.ps(options = {})
keywords = Array(options[:keywords]) | %w[pid ppid pgid stat command]
command = ["ps"].tap do |ps|

0 comments on commit 28c17d6

Please sign in to comment.