Permalink
Browse files

use log levels properly

  • Loading branch information...
1 parent 9741d82 commit 6b7df0c07bef453ac6bb963a4c9adb947d5eb335 @thoughtless committed Jun 20, 2011
Showing with 51 additions and 42 deletions.
  1. +23 −26 lib/angael/manager.rb
  2. +1 −1 lib/angael/version.rb
  3. +27 −15 lib/angael/worker.rb
View
@@ -1,4 +1,5 @@
require 'angael/process_helper'
+require 'logger'
module Angael
# A Manager has a number of of worker objects. Starting the Manager simply
# calls #start! on each worker, then it goes into an infinite loop, waiting
@@ -21,13 +22,6 @@ class Manager
# @opts [Hash] Additional options:
# :logger => A logger object, which should follow the Logger class in the
# standard library. Default nil, as in no logging.
- # :log_level => The log level, as defined by the Logger class in the
- # standard library. One of:
- # Logger::FATAL
- # Logger::ERROR
- # Logger::WARN
- # Logger::INFO # Default
- # Logger::DEBUG
# :restart_after => If set, 1 worker will be restarted after this number
# of seconds. If it is nil (the default), then workers will not get
# restarted for no reason. If your workers leak memory, this can help
@@ -39,12 +33,6 @@ def initialize(worker_class, worker_count=1, worker_args=[], opts={})
# TODO: Add a spec for this
raise ArgumentError, ':restart_after must be either an Integer greater than zero or nil' if @restart_after && (@restart_after.to_i != @restart_after || @restart_after == 0)
@logger = opts[:logger]
- if @logger
- @log_level = opts[:log_level] || begin
- require 'logger' # Only require it if it is absolutely neccessary.
- Logger::INFO
- end
- end
end
@@ -54,15 +42,15 @@ def start!
workers.each { |w| w.start! }
trap("CHLD") do
- log("SIGCHLD Received")
+ debug("SIGCHLD Received")
@sigchld = true
end
trap("INT") do
- log("SIGINT Received")
+ info("SIGINT Received")
@interrupted = true
end
trap("TERM") do
- log("SIGTERM Received")
+ info("SIGTERM Received")
@interrupted = true
end
@@ -89,24 +77,33 @@ def start!
#########
def stop!
- log("Attempting to gracefully stopping worker manager")
+ info("Attempting to gracefully stopping worker manager")
# Tell each worker to stop, without waiting to see if it worked.
workers.each { |w|
- log("Calling #stop_without_wait for worker #{w.inspect}")
+ debug("Calling #stop_without_wait for worker #{w.inspect}")
w.stop_without_wait
- log("Finished call to #stop_without_wait for worker #{w.inspect}")
+ debug("Finished call to #stop_without_wait for worker #{w.inspect}")
}
# Wait for each worker to stop, one at a time.
workers.each { |w|
- log("Calling #stop_with_wait for worker #{w.inspect}")
+ debug("Calling #stop_with_wait for worker #{w.inspect}")
w.stop_with_wait
- log("Finished call to #stop_with_wait for worker #{w.inspect}")
+ debug("Finished call to #stop_with_wait for worker #{w.inspect}")
}
+ info("Exiting")
exit 0
end
- def log(msg)
- @logger.add(@log_level, "#{Time.now.utc} - #{self.class} (pid #{$$}): #{msg}") if @logger
+ def log(level, msg)
+ @logger.add(level, "#{Time.now.utc} - #{self.class} (pid #{$$}): #{msg}") if @logger
+ end
+
+ def debug(msg)
+ log(Logger::DEBUG, msg)
+ end
+
+ def info(msg)
+ log(Logger::INFO, msg)
end
@@ -147,11 +144,11 @@ def restart_worker_if_needed
@seconds_until_restart_next_worker -= LOOP_SLEEP_SECONDS
else
w = next_worker_to_restart
- log("Time to restart a worker: Calling #stop_with_wait for worker #{w.inspect}")
+ debug("Time to restart a worker: Calling #stop_with_wait for worker #{w.inspect}")
w.stop_with_wait
- log("Worker has been stopped: #{w.inspect}")
+ debug("Worker has been stopped: #{w.inspect}")
w.start!
- log("Worker has been restarted: #{w.inspect}")
+ debug("Worker has been restarted: #{w.inspect}")
w = nil
# Reset the counter
@@ -1,3 +1,3 @@
module Angael
- VERSION = "0.0.9"
+ VERSION = "0.1.0"
end
View
@@ -1,4 +1,5 @@
require 'angael/process_helper'
+require 'logger'
module Angael
# Usage
# include Angael::Worker
@@ -28,27 +29,27 @@ def start!
@stopping = false
@pid = fork_child do
- __log("Started")
+ __info("Started")
if respond_to?(:after_fork)
- __log("Running after fork callback")
+ __debug("Running after fork callback")
after_fork
- __log("Finished running after fork callback")
+ __debug("Finished running after fork callback")
end
@interrupted = false
trap("INT") do
- __log("SIGINT Received")
+ __info("SIGINT Received")
@interrupted = true
end
trap("TERM") do
- __log("SIGTERM Received")
+ __info("SIGTERM Received")
@interrupted = true
end
loop do
if @interrupted
- __log("Child process exiting gracefully")
+ __info("Child process exiting gracefully")
exit 0
end
work
@@ -71,15 +72,15 @@ def restart!
# Sets stopping? to false.
def stop_without_wait
unless started?
- __log("Tried to stop worker with PID #{pid} but it is not started")
+ __warn("Tried to stop worker with PID #{pid} but it is not started")
return false
end
# This informs the Manager (through #stopping?) that we intentionally
# stopped the child process.
@stopping = true
- __log("Sending SIGINT to child process with pid #{pid}.")
+ __debug("Sending SIGINT to child process with pid #{pid}.")
send_signal('INT', pid)
true
end
@@ -89,27 +90,27 @@ def stop_without_wait
def stop_with_wait
return false unless stop_without_wait
- __log("Waiting for child process with pid #{pid} to stop.")
+ __debug("Waiting for child process with pid #{pid} to stop.")
counter = 0
while pid_running? && counter < timeout
sleep 1
counter += 1
- __log("Sending SIGINT to child process with pid #{pid}. Attempt Count: #{counter}.")
+ __info("Sending SIGINT to child process with pid #{pid}. Attempt Count: #{counter}.")
send_signal('INT', pid)
end
if pid_running?
- __log("Child process with pid #{pid} did not stop within #{timeout} seconds of SIGINT. Sending SIGKILL to child process.")
+ __warn("Child process with pid #{pid} did not stop within #{timeout} seconds of SIGINT. Sending SIGKILL to child process.")
send_signal('KILL', pid)
sleep 1
end
if pid_running?
# SIGKILL didn't work.
msg = "Unable to kill child process with PID: #{pid}"
- __log(msg)
+ __error(msg)
raise ChildProcessNotStoppedError, msg
end
end
@@ -136,10 +137,21 @@ def work
end
- def __log(msg)
- log(msg) if respond_to?(:log)
+ def __log(level, msg)
+ log(level, msg) if respond_to?(:log)
+ end
+ def __debug(msg)
+ __log(Logger::DEBUG, msg)
+ end
+ def __info(msg)
+ __log(Logger::INFO, msg)
+ end
+ def __warn(msg)
+ __log(Logger::WARN, msg)
+ end
+ def __error(msg)
+ __log(Logger::ERROR, msg)
end
-
# In seconds
def timeout

0 comments on commit 6b7df0c

Please sign in to comment.