Skip to content

Commit

Permalink
Propagate exceptions from sync or async jobs back up the graph as err…
Browse files Browse the repository at this point in the history
…backs

* Allows all exceptions to be handled in main thread
* Exception can be reraised in job.errback if required
  • Loading branch information
mloughran committed Nov 13, 2009
1 parent c414a29 commit 9c1ab3e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions dsltest.rb
Expand Up @@ -30,5 +30,9 @@ class Download < Noodler::Job
job.callback do
puts "Finished everything!"
end
job.errback do |e|
# raise e
puts "Job failed with exception #{e.class}, #{e.message}"
end
}
}
3 changes: 3 additions & 0 deletions lib/noodler/job.rb
Expand Up @@ -28,6 +28,9 @@ def run
root.callback do
succeed
end
root.errback do |e|
fail e
end
self
end

Expand Down
14 changes: 13 additions & 1 deletion lib/noodler/node.rb
Expand Up @@ -22,6 +22,9 @@ def <<(child)
succeed
end
end
child.errback do |e|
fail e
end
@children << child
end

Expand All @@ -39,11 +42,20 @@ def run_async(input)
deferrable.errback do
puts "Deferrable strategy failed"
end
rescue => e
fail e
end

def run_sync(input)
EM.defer \
lambda { @output = @strategy.call(input); },
lambda {
begin
@output = @strategy.call(input);
rescue => e
puts "exception caught in thread - propagating back"
fail e
end
},
lambda { EM.next_tick(method(:run_children)) }
end

Expand Down

0 comments on commit 9c1ab3e

Please sign in to comment.