Permalink
Browse files

Added sync node option for work that should happen in the reactor thread

  • Loading branch information...
mloughran committed Nov 14, 2009
1 parent 915b4c6 commit a30e9e22548a6a685a1b5e0bbdb24875655cd437
Showing with 20 additions and 5 deletions.
  1. +10 −4 dsltest.rb
  2. +10 −1 lib/noodler/node.rb
View
@@ -28,30 +28,36 @@ class Download < Noodler::Job
"Report done"
end
- node :another, :threaded do |node, result|
+ node :another, :sync do |node, result|
puts result
+ puts "Sleeping in the reactor thread is bad"
+ sleep 1
+
node.add_child(:yet_more)
"Another result"
end
node :yet_more, :threaded do |node, result|
+ puts "But sleeping in a thread is ok"
+ sleep 1
+
puts result
end
end
EM.run {
- 1.times {
+ 2.times do |i|
job = Download.new({}).run
job.callback do
- puts "Finished everything!"
+ puts "Finished job #{i}"
end
job.errback do |e|
# raise e
puts "Job failed with exception #{e.class}, #{e.message}"
end
- }
+ end
Signal.trap('INT') { EM.stop }
}
View
@@ -5,7 +5,7 @@ class Node
# attr_accessor :parent
attr_accessor :job
- VALID_TYPES = [:evented, :threaded]
+ VALID_TYPES = [:sync, :evented, :threaded]
def initialize(run_method, strategy = nil, &strategy_block)
unless VALID_TYPES.include?(run_method)
@@ -39,13 +39,22 @@ def add_child(name)
def run(input = nil)
case @run_method
+ when :sync: run_sync(input)
when :evented: run_evented(input)
when :threaded: run_threaded(input)
else
raise "#{@run_method} not supported"
end
end
+ def run_sync(input)
+ @output = @strategy.call(self, input)
+ EM.next_tick(method(:run_children))
+ rescue => e
+ puts "Exception in sync code"
+ fail e
+ end
+
def run_evented(input)
deferrable = @strategy.call(self, input)
deferrable.callback do |output|

0 comments on commit a30e9e2

Please sign in to comment.