Skip to content

Commit

Permalink
Provide a way to log output to a file rather than just STDOUT.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Sep 10, 2012
1 parent 0ada9ad commit 3f046c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/qless/worker.rb
Expand Up @@ -10,9 +10,14 @@ class Worker
def initialize(client, job_reserver, options = {})
@client, @job_reserver = client, job_reserver
@shutdown = @paused = false

self.very_verbose = options[:very_verbose]
self.verbose = options[:verbose]
self.run_as_single_process = options[:run_as_single_process]
self.output = options.fetch(:output, $stdout)

output.puts "\n\n\n" if verbose || very_verbose
log "Instantiated Worker"
end

# Whether the worker should log basic info to STDOUT
Expand All @@ -26,6 +31,10 @@ def initialize(client, job_reserver, options = {})
# This should only be true in a dev/test environment
attr_accessor :run_as_single_process

# An IO-like object that logging output is sent to.
# Defaults to $stdout.
attr_accessor :output

# Starts a worker based on ENV vars. Supported ENV vars:
# - REDIS_URL=redis://host:port/db-num (the redis gem uses this automatically)
# - QUEUES=high,medium,low or QUEUE=blah
Expand Down Expand Up @@ -179,10 +188,10 @@ def register_signal_handlers
# Log a message to STDOUT if we are verbose or very_verbose.
def log(message)
if verbose
puts "*** #{message}"
output.puts "*** #{message}"
elsif very_verbose
time = Time.now.strftime('%H:%M:%S %Y-%m-%d')
puts "** [#{time}] #$$: #{message}"
output.puts "** [#{time}] #$$: #{message}"
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unit/worker_spec.rb
Expand Up @@ -361,7 +361,7 @@ def with_env_vars(vars = {})
with_env_vars do
orig_new = Worker.method(:new)
Worker.should_receive(:new) do |client, reserver, options = {}|
worker = orig_new.call(client, reserver, options)
worker = orig_new.call(client, reserver, options.merge(:output => StringIO.new))
worker.verbose.should be_false
worker.very_verbose.should be_false
worker.run_as_single_process.should be_false
Expand All @@ -377,7 +377,7 @@ def with_env_vars(vars = {})
with_env_vars 'VERBOSE' => '1' do
orig_new = Worker.method(:new)
Worker.should_receive(:new) do |client, reserver, options = {}|
worker = orig_new.call(client, reserver, options)
worker = orig_new.call(client, reserver, options.merge(:output => StringIO.new))
worker.verbose.should be_true
worker.very_verbose.should be_false
worker.run_as_single_process.should be_false
Expand All @@ -393,7 +393,7 @@ def with_env_vars(vars = {})
with_env_vars 'VVERBOSE' => '1' do
orig_new = Worker.method(:new)
Worker.should_receive(:new) do |client, reserver, options = {}|
worker = orig_new.call(client, reserver, options)
worker = orig_new.call(client, reserver, options.merge(:output => StringIO.new))
worker.verbose.should be_false
worker.very_verbose.should be_true
worker.run_as_single_process.should be_false
Expand Down

0 comments on commit 3f046c9

Please sign in to comment.