Skip to content

Commit

Permalink
The error handler is now called with three parameters: job name,
Browse files Browse the repository at this point in the history
arguments and the exception.
  • Loading branch information
Adam Pohorecki committed Feb 16, 2011
1 parent 813fe5f commit 1a18e96
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -72,7 +72,7 @@ Error Handling

If you include an `error` block in your jobs definition, that block will be invoked when a worker encounters an error. You might use this to report errors to an external monitoring service:

error do |e|
error do |job_name, args, e|
Exceptional.handle(e)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/stalker.rb
Expand Up @@ -96,7 +96,7 @@ def work_one_job
log_error exception_message(e)
job.bury rescue nil
log_job_end(name, 'failed')
error_handler.call(e) if error_handler
error_handler.call(name, args, e) if error_handler
end

def failed_connection(e)
Expand Down
28 changes: 20 additions & 8 deletions test/stalker_test.rb
Expand Up @@ -14,6 +14,14 @@ class StalkerTest < Test::Unit::TestCase
$handled = false
end

def with_an_error_handler
Stalker.error do |job_name, args, e|
$handled = e.class
$job_name = job_name
$job_args = args
end
end

test "enqueue and work a job" do
val = rand(999999)
Stalker.job('my.job') { |args| $result = args['val'] }
Expand All @@ -24,12 +32,14 @@ class StalkerTest < Test::Unit::TestCase
end

test "invoke error handler when defined" do
Stalker.error { |e| $handled = true }
Stalker.job('my.job') { fail }
Stalker.enqueue('my.job')
with_an_error_handler
Stalker.job('my.job') { |args| fail }
Stalker.enqueue('my.job', :foo => 123)
Stalker.prep
Stalker.work_one_job
assert_equal true, $handled
assert $handled
assert_equal 'my.job', $job_name
assert_equal({'foo' => 123}, $job_args)
end

test "continue working when error handler not defined" do
Expand All @@ -41,7 +51,7 @@ class StalkerTest < Test::Unit::TestCase
end

test "exception raised one second before beanstalk ttr reached" do
Stalker.error { |e| $handled = e.class }
with_an_error_handler
Stalker.job('my.job') { sleep(3); $handled = "didn't time out" }
Stalker.enqueue('my.job', {}, :ttr => 2)
Stalker.prep
Expand Down Expand Up @@ -77,13 +87,15 @@ class StalkerTest < Test::Unit::TestCase
end

test "before filter invokes error handler when defined" do
Stalker.error { |e| $handled = true }
with_an_error_handler
Stalker.before { |name| fail }
Stalker.job('my.job') { }
Stalker.enqueue('my.job')
Stalker.enqueue('my.job', :foo => 123)
Stalker.prep
Stalker.work_one_job
assert_equal true, $handled
assert $handled
assert_equal 'my.job', $job_name
assert_equal({'foo' => 123}, $job_args)
end

end

0 comments on commit 1a18e96

Please sign in to comment.