Permalink
Browse files

made logger configurable

  • Loading branch information...
1 parent ed3f1ff commit 014480836f73b95f03fa5f96eae3a1255ce9b940 @skaes skaes committed Mar 29, 2010
Showing with 54 additions and 64 deletions.
  1. +2 −2 Rakefile
  2. +1 −1 VERSION.yml
  3. +2 −2 lib/gearman/client.rb
  4. +14 −14 lib/gearman/taskset.rb
  5. +11 −23 lib/gearman/util.rb
  6. +14 −14 lib/gearman/worker.rb
  7. +10 −8 gearman-ruby.gemspec → xing-gearman-ruby.gemspec
View
@@ -6,7 +6,7 @@ require 'rcov/rcovtask'
begin
require 'jeweler'
Jeweler::Tasks.new do |s|
- s.name = "gearman-ruby"
+ s.name = "xing-gearman-ruby"
s.summary = "Library for the Gearman distributed job system"
s.email = "ladislav.martincik@xing.com"
s.homepage = "http://github.com/xing/gearman-ruby"
@@ -37,4 +37,4 @@ Rcov::RcovTask.new do |t|
t.verbose = true
end
-task :default => :rcov
+task :default => :rcov
View
@@ -1,4 +1,4 @@
---
-:patch: 3
+:patch: 4
:major: 1
:minor: 3
View
4 lib/gearman/client.rb 100644 → 100755
@@ -31,7 +31,7 @@ def initialize(job_servers=nil)
#
# @options options to pass to the servers "exeptions"
def option_request(opts)
- Util.log "Send options request with #{opts}"
+ Util.logger.debug "GearmanRuby: Send options request with #{opts}"
request = Util.pack_request("option_req", opts)
sock= self.get_socket(self.get_job_server)
Util.send_request(sock, request)
@@ -104,7 +104,7 @@ def return_socket(sock)
hostport = get_hostport_for_socket(sock)
if not hostport
inet, port, host, ip = s.addr
- Util.err "Got socket for #{ip}:#{port}, which we don't " +
+ Util.logger.error "GearmanRuby: Got socket for #{ip}:#{port}, which we don't " +
"know about -- closing"
sock.close
return
View
28 lib/gearman/taskset.rb 100644 → 100755
@@ -66,13 +66,13 @@ def add_task_internal(task, reset_state=true)
should_try_rehash = false
end
end
- Util.log "Using socket #{sock.inspect} for #{hostport}"
+ Util.logger.debug "GearmanRuby: Using socket #{sock.inspect} for #{hostport}"
Util.send_request(sock, req)
while @task_waiting_for_handle
begin
read_packet(sock, @client.task_create_timeout_sec)
rescue NetworkError
- Util.log "Got timeout on read from #{hostport}"
+ Util.logger.debug "GearmanRuby: Got timeout on read from #{hostport}"
@task_waiting_for_handle = nil
@client.close_socket(sock)
return false
@@ -90,7 +90,7 @@ def add_task_internal(task, reset_state=true)
# @param hostport "host:port" of job server
# @param data data returned in packet from server
def handle_job_created(hostport, data)
- Util.log "Got job_created with handle #{data} from #{hostport}"
+ Util.logger.debug "GearmanRuby: Got job_created with handle #{data} from #{hostport}"
if not @task_waiting_for_handle
raise ProtocolError, "Got unexpected job_created notification " + "with handle #{data} from #{hostport}"
end
@@ -113,7 +113,7 @@ def handle_job_created(hostport, data)
# @param data data returned in packet from server
def handle_work_complete(hostport, data)
handle, data = data.split("\0", 2)
- Util.log "Got work_complete with handle #{handle} and #{data ? data.size : '0'} byte(s) of data from #{hostport}"
+ Util.logger.debug "GearmanRuby: Got work_complete with handle #{handle} and #{data ? data.size : '0'} byte(s) of data from #{hostport}"
tasks_in_progress(hostport, handle, true).each do |t|
t.handle_completion(data)
@finished_tasks << t
@@ -129,7 +129,7 @@ def handle_work_complete(hostport, data)
# @param data data returned in packet from server
def handle_work_exception(hostport, data)
handle, exception = data.split("\0", 2)
- Util.log "Got work_exception with handle #{handle} from #{hostport}: '#{exception}'"
+ Util.logger.debug "GearmanRuby: Got work_exception with handle #{handle} from #{hostport}: '#{exception}'"
tasks_in_progress(hostport, handle).each {|t| t.handle_exception(exception) }
end
private :handle_work_exception
@@ -140,7 +140,7 @@ def handle_work_exception(hostport, data)
# @param hostport "host:port" of job server
# @param data data returned in packet from server
def handle_work_fail(hostport, data)
- Util.log "Got work_fail with handle #{data} from #{hostport}"
+ Util.logger.debug "GearmanRuby: Got work_fail with handle #{data} from #{hostport}"
tasks_in_progress(hostport, data, true).each do |t|
if t.handle_failure
add_task_internal(t, false)
@@ -158,7 +158,7 @@ def handle_work_fail(hostport, data)
# @param data data returned in packet from server
def handle_work_status(hostport, data)
handle, num, den = data.split("\0", 3)
- Util.log "Got work_status with handle #{handle} from #{hostport}: #{num}/#{den}"
+ Util.logger.debug "GearmanRuby: Got work_status with handle #{handle} from #{hostport}: #{num}/#{den}"
tasks_in_progress(hostport, handle).each {|t| t.handle_status(num, den) }
end
private :handle_work_status
@@ -170,7 +170,7 @@ def handle_work_status(hostport, data)
# @param data data returned in packet from server
def handle_work_warning(hostport, data)
handle, message = data.split("\0", 2)
- Util.log "Got work_warning with handle #{handle} from #{hostport}: '#{message}'"
+ Util.logger.debug "GearmanRuby: Got work_warning with handle #{handle} from #{hostport}: '#{message}'"
tasks_in_progress(hostport, handle).each {|t| t.handle_warning(message) }
end
private :handle_work_warning
@@ -182,7 +182,7 @@ def handle_work_warning(hostport, data)
# @param data data returned in packet from server
def handle_work_data(hostport, data)
handle, data = data.split("\0", 2)
- Util.log "Got work_data with handle #{handle} and #{data ? data.size : '0'} byte(s) of data from #{hostport}"
+ Util.logger.debug "GearmanRuby: Got work_data with handle #{handle} and #{data ? data.size : '0'} byte(s) of data from #{hostport}"
js_handle = Util.handle_to_str(hostport, handle)
tasks = @tasks_in_progress[js_handle]
@@ -215,7 +215,7 @@ def read_packet(sock, timeout=nil)
if known_types.include?(type)
send("handle_#{type}".to_sym, hostport, data)
else
- Util.log "Got #{type.to_s} from #{hostport}"
+ Util.logger.debug "GearmanRuby: Got #{type.to_s} from #{hostport}"
end
nil
end
@@ -241,7 +241,7 @@ def wait(timeout = 1)
ready_socks = IO::select(@sockets.values, nil, nil, remaining)
if not ready_socks or not ready_socks[0]
- Util.log "Timed out while waiting for tasks to finish"
+ Util.logger.debug "GearmanRuby: Timed out while waiting for tasks to finish"
# not sure what state the connections are in, so just be lame and
# close them for now
@sockets.values.each {|s| @client.close_socket(s) }
@@ -253,18 +253,18 @@ def wait(timeout = 1)
read_packet(sock, (end_time ? end_time - Time.now.to_f : nil))
rescue ProtocolError
hostport = @client.get_hostport_for_socket(sock)
- Util.log "Ignoring bad packet from #{hostport}"
+ Util.logger.debug "GearmanRuby: Ignoring bad packet from #{hostport}"
rescue NetworkError
hostport = @client.get_hostport_for_socket(sock)
- Util.log "Got timeout on read from #{hostport}"
+ Util.logger.debug "GearmanRuby: Got timeout on read from #{hostport}"
end
end
end
@sockets.values.each {|s| @client.return_socket(s) }
@sockets = {}
@finished_tasks.each do |t|
if ( (t.background.nil? || t.background == false) && !t.successful)
- Util.log "Taskset failed"
+ Util.logger.debug "GearmanRuby: Taskset failed"
return false
end
end
View
34 lib/gearman/util.rb 100644 → 100755
@@ -2,6 +2,7 @@
require 'socket'
require 'time'
+require 'logger'
module Gearman
@@ -59,14 +60,17 @@ class Util
# Default job server port.
DEFAULT_PORT = 7003
- @@debug = false
+ def Util.logger=(logger)
+ @logger = logger
+ end
- ##
- # Enable or disable debugging output (off by default).
- #
- # @param v print debugging output
- def Util.debug=(v)
- @@debug = v
+ def Util.logger
+ @logger ||=
+ begin
+ l = Logger.new($stdout)
+ l.level = Logger::FATAL
+ l
+ end
end
##
@@ -183,22 +187,6 @@ def Util.str_to_handle(str)
return [$1, $3]
end
- ##
- # Log a message if debugging is enabled.
- #
- # @param str message to log
- def Util.log(str, force=false)
- puts "#{Time.now.strftime '%Y%m%d %H%M%S'} #{str}" if force or @@debug
- end
-
- ##
- # Log a message no matter what.
- #
- # @param str message to log
- def Util.err(str)
- log(str, true)
- end
-
def self.with_safe_socket_op
begin
yield
View
28 lib/gearman/worker.rb 100644 → 100755
@@ -168,7 +168,7 @@ def update_job_servers(servers)
# Disconnect from servers that we no longer care about.
@sockets.each do |server,sock|
if not servers.include? server
- Util.log "Disconnecting from old server #{server}"
+ Util.logger.debug "GearmanRuby: Disconnecting from old server #{server}"
sock.close
@sockets.delete(server)
end
@@ -177,11 +177,11 @@ def update_job_servers(servers)
servers.each do |server|
if not @sockets[server]
begin
- Util.log "Connecting to server #{server}"
+ Util.logger.debug "GearmanRuby: Connecting to server #{server}"
@sockets[server] = connect(server)
rescue NetworkError, Errno::ECONNRESET
@bad_servers << server
- Util.log "Unable to connect to #{server}"
+ Util.logger.debug "GearmanRuby: Unable to connect to #{server}"
end
end
end
@@ -260,16 +260,16 @@ def remove_ability(func)
def handle_job_assign(data, sock, hostport)
handle, func, data = data.split("\0", 3)
if not func
- Util.err "Ignoring job_assign with no function from #{hostport}"
+ Util.logger.error "GearmanRuby: Ignoring job_assign with no function from #{hostport}"
return false
end
- Util.log "Got job_assign with handle #{handle} and #{data.size} byte(s) " +
+ Util.logger.error "GearmanRuby: Got job_assign with handle #{handle} and #{data.size} byte(s) " +
"from #{hostport}"
ability = @abilities[func]
if not ability
- Util.err "Ignoring job_assign for unsupported func #{func} " +
+ Util.logger.error "Ignoring job_assign for unsupported func #{func} " +
"with handle #{handle} from #{hostport}"
Util.send_request(sock, Util.pack_request(:work_fail, handle))
return false
@@ -285,14 +285,14 @@ def handle_job_assign(data, sock, hostport)
cmd = if ret && exception.nil?
ret = ret.to_s
- Util.log "Sending work_complete for #{handle} with #{ret.size} byte(s) " +
+ Util.logger.debug "GearmanRuby: Sending work_complete for #{handle} with #{ret.size} byte(s) " +
"to #{hostport}"
[ Util.pack_request(:work_complete, "#{handle}\0#{ret}") ]
elsif exception.nil?
- Util.log "Sending work_fail for #{handle} to #{hostport}"
+ Util.logger.debug "GearmanRuby: Sending work_fail for #{handle} to #{hostport}"
[ Util.pack_request(:work_fail, handle) ]
elsif exception
- Util.log "Sending work_warning, work_fail for #{handle} to #{hostport}"
+ Util.logger.debug "GearmanRuby: Sending work_warning, work_fail for #{handle} to #{hostport}"
[ Util.pack_request(:work_warning, "#{handle}\0#{exception.message}"),
Util.pack_request(:work_fail, handle) ]
end
@@ -312,7 +312,7 @@ def work
servers = nil
@servers_mutex.synchronize { servers = @sockets.keys.sort }
servers.each do |hostport|
- Util.log "Sending grab_job to #{hostport}"
+ Util.logger.debug "GearmanRuby: Sending grab_job to #{hostport}"
sock = @sockets[hostport]
Util.send_request(sock, req)
@@ -324,16 +324,16 @@ def work
type, data = Util.read_response(sock, @network_timeout_sec)
case type
when :no_job
- Util.log "Got no_job from #{hostport}"
+ Util.logger.debug "GearmanRuby: Got no_job from #{hostport}"
break
when :job_assign
return if handle_job_assign(data, sock, hostport)
break
else
- Util.log "Got #{type.to_s} from #{hostport}"
+ Util.logger.debug "GearmanRuby: Got #{type.to_s} from #{hostport}"
end
rescue Exception
- Util.log "Server #{hostport} timed out or lost connection (#{$!.inspect}); marking bad"
+ Util.logger.debug "GearmanRuby: Server #{hostport} timed out or lost connection (#{$!.inspect}); marking bad"
bad_servers << hostport
break
end
@@ -348,7 +348,7 @@ def work
end
end
- Util.log "Sending pre_sleep and going to sleep for #{@reconnect_sec} sec"
+ Util.logger.debug "GearmanRuby: Sending pre_sleep and going to sleep for #{@reconnect_sec} sec"
@servers_mutex.synchronize do
@sockets.values.each do |sock|
Util.send_request(sock, Util.pack_request(:pre_sleep))
@@ -1,17 +1,21 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
s.name = %q{xing-gearman-ruby}
- s.version = "1.3.3"
+ s.version = "1.3.4"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Daniel Erat", "Ladislav Martincik", "Pablo Delgado", "Mauro Pompilio", "Antonio Garrote", "Kim Altintop"]
- s.date = %q{2009-10-19}
+ s.date = %q{2010-03-29}
s.description = %q{Library for the Gearman distributed job system}
s.email = %q{ladislav.martincik@xing.com}
s.extra_rdoc_files = [
"LICENSE",
- "README"
+ "README",
+ "TODO"
]
s.files = [
".gitignore",
@@ -49,7 +53,6 @@ Gem::Specification.new do |s|
"examples/worker_data.rb",
"examples/worker_exception.rb",
"examples/worker_prefix.rb",
- "gearman-ruby.gemspec",
"lib/gearman.rb",
"lib/gearman/client.rb",
"lib/gearman/server.rb",
@@ -62,7 +65,8 @@ Gem::Specification.new do |s|
"test/mock_client_test.rb",
"test/mock_worker_test.rb",
"test/util_test.rb",
- "test/worker_test.rb"
+ "test/worker_test.rb",
+ "xing-gearman-ruby.gemspec"
]
s.homepage = %q{http://github.com/xing/gearman-ruby}
s.rdoc_options = ["--charset=UTF-8"]
@@ -80,16 +84,13 @@ Gem::Specification.new do |s|
"examples/client.rb",
"examples/client_background.rb",
"examples/client_data.rb",
- "examples/client_echo.rb",
"examples/client_exception.rb",
"examples/client_prefix.rb",
"examples/scale_image.rb",
"examples/scale_image_worker.rb",
"examples/server.rb",
"examples/worker.rb",
"examples/worker_data.rb",
- "examples/worker_echo.rb",
- "examples/worker_echo_pprof.rb",
"examples/worker_exception.rb",
"examples/worker_prefix.rb"
]
@@ -104,3 +105,4 @@ Gem::Specification.new do |s|
else
end
end
+

0 comments on commit 0144808

Please sign in to comment.