Skip to content
This repository
Browse code

Now possible to add child nodes by name at excecution time

  • Loading branch information...
commit 21f55c1cdf8de6a1786a02aadc1e1bf4a62e0754 1 parent 9c1ab3e
Martyn Loughran authored November 14, 2009
25  dsltest.rb
@@ -6,8 +6,9 @@
6 6
 class Download < Noodler::Job
7 7
   run [:download, :report]
8 8
 
9  
-  node :download, :async do |params|
  9
+  node :download, :async do |node, params|
10 10
     puts "Downloading..."
  11
+    # raise 'foo'
11 12
     deferrable = EM::DefaultDeferrable.new
12 13
     deferrable.callback do
13 14
       puts "Finished downloading"
@@ -18,9 +19,25 @@ class Download < Noodler::Job
18 19
     deferrable
19 20
   end
20 21
 
21  
-  node :report, :sync do |status, body|
  22
+  node :report, :sync do |node, status, body|
22 23
     puts "I got status code #{status} and body '#{body}'"
23  
-    'Report result'
  24
+
  25
+    node.add_child(:another)
  26
+    node.add_child(:another)
  27
+
  28
+    "Report done"
  29
+  end
  30
+
  31
+  node :another, :sync do |node, result|
  32
+    puts result
  33
+
  34
+    node.add_child(:yet_more)
  35
+
  36
+    "Another result"
  37
+  end
  38
+
  39
+  node :yet_more, :sync do |node, result|
  40
+    puts result
24 41
   end
25 42
 end
26 43
 
@@ -35,4 +52,6 @@ class Download < Noodler::Job
35 52
       puts "Job failed with exception #{e.class}, #{e.message}"
36 53
     end
37 54
   }
  55
+
  56
+  Signal.trap('INT') { EM.stop }
38 57
 }
6  lib/noodler/job.rb
@@ -34,6 +34,11 @@ def run
34 34
       self
35 35
     end
36 36
 
  37
+    def create_named_node(name)
  38
+      n = self.class._nodes[name]
  39
+      Noodler::Node.new(n[:type], n[:strategy])
  40
+    end
  41
+
37 42
     private
38 43
 
39 44
     def construct_graph
@@ -50,6 +55,7 @@ def construct_graph
50 55
           current = node
51 56
         else
52 57
           current = root = node
  58
+          root.job = self
53 59
         end
54 60
       end
55 61
 
11  lib/noodler/node.rb
@@ -3,6 +3,7 @@ class Node
3 3
     include EM::Deferrable
4 4
 
5 5
     # attr_accessor :parent
  6
+    attr_accessor :job
6 7
 
7 8
     def initialize(type, strategy = nil, &strategy_block)
8 9
       unless type == :sync || type == :async
@@ -16,6 +17,7 @@ def initialize(type, strategy = nil, &strategy_block)
16 17
 
17 18
     def <<(child)
18 19
       # child.parent = self
  20
+      child.job = self.job
19 21
       child.callback do
20 22
         @children_complete += 1
21 23
         if @children_complete == @children.size
@@ -28,12 +30,17 @@ def <<(child)
28 30
       @children << child
29 31
     end
30 32
 
  33
+    def add_child(name)
  34
+      node = job.create_named_node(name)
  35
+      self << node
  36
+    end
  37
+
31 38
     def run(input = nil)
32 39
       @synchronous ? run_sync(input) : run_async(input)
33 40
     end
34 41
 
35 42
     def run_async(input)
36  
-      deferrable = @strategy.call(input)
  43
+      deferrable = @strategy.call(self, input)
37 44
       deferrable.callback do |output|
38 45
         @output = output
39 46
         puts "Deferrable strategy succeeded"
@@ -50,7 +57,7 @@ def run_sync(input)
50 57
       EM.defer \
51 58
         lambda {
52 59
           begin
53  
-            @output = @strategy.call(input);
  60
+            @output = @strategy.call(self, input);
54 61
           rescue => e
55 62
             puts "exception caught in thread - propagating back"
56 63
             fail e

0 notes on commit 21f55c1

Please sign in to comment.
Something went wrong with that request. Please try again.