Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

An example with nodes sharing a variable on job (not threadsafe yet) …

…and controlling excecution with a case statement
  • Loading branch information...
commit 1d82da960f3c17cc26141bb07361fd15667c8866 1 parent 8a741f0
@mloughran authored
Showing with 51 additions and 0 deletions.
  1. +51 −0 examples/job_shared_state.rb
View
51 examples/job_shared_state.rb
@@ -0,0 +1,51 @@
+# An example with nodes sharing a variable on job (not threadsafe yet) and
+# controlling excecution with a case statement
+
+require 'rubygems'
+require 'eventmachine'
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+require 'noodler'
+
+class Job < Noodler::Job
+ attr_accessor :retries
+
+ def initialize
+ @retries = 0
+ end
+
+ run [:case_statement]
+
+ node :case_statement, :sync do |node, status|
+ puts node.job.retries
+ if status == 200 || node.job.retries > 5
+ node.add_child :complete
+ else
+ node.add_child :download
+ end
+ status
+ end
+
+ node :download, :evented do |node, state|
+ puts "Downloading..."
+ deferrable = EM::DefaultDeferrable.new
+ EM::Timer.new(1) do
+ status = (rand < 0.3) ? 200 : 500
+ puts "Finished downloading (status #{status})"
+ node.job.retries += 1
+ deferrable.succeed status
+ end
+ node.add_child :case_statement
+ deferrable
+ end
+
+ node :complete, :threaded do |node, status|
+ puts "Finished with status #{status}"
+ end
+end
+
+EM.run {
+ job = Job.new.run
+ job.callback do
+ puts "Job done"
+ end
+}
Please sign in to comment.
Something went wrong with that request. Please try again.