Permalink
Browse files

Experimental dsl for defining jobs

  • Loading branch information...
1 parent 635cde8 commit 22d51c5307e4242796ce221c8887fe3563055e5c @mloughran committed Oct 21, 2009
Showing with 84 additions and 0 deletions.
  1. +31 −0 dsltest.rb
  2. +1 −0 lib/noodler.rb
  3. +52 −0 lib/noodler/job.rb
View
@@ -0,0 +1,31 @@
+require 'rubygems'
+require 'eventmachine'
+$:.unshift('lib')
+require 'noodler'
+
+class Download < Noodler::Job
+ run [:download, :report]
+
+ node :download, :async do |params|
+ puts "Downloading..."
+ deferrable = EM::DefaultDeferrable.new
+ deferrable.callback do
+ puts "Finished downloading"
+ end
+ EM::Timer.new(1) do
+ deferrable.succeed [200, 'Download result']
+ end
+ deferrable
+ end
+
+ node :report, :sync do |status, body|
+ puts "I got status code #{status} and body '#{body}'"
+ 'Report result'
+ end
+end
+
+EM.run {
+ 1.times {
+ job = Download.new({}).run
+ }
+}
View
@@ -3,3 +3,4 @@ module Noodler
end
require 'noodler/node'
+require 'noodler/job'
View
@@ -0,0 +1,52 @@
+module Noodler
+ class Job
+ include EM::Deferrable
+
+ class << self
+ attr_reader :_jobs, :_nodes
+
+ def run(jobs)
+ @_jobs = jobs
+ end
+
+ def node(name, type, &strategy)
+ @_nodes ||= {}
+ @_nodes[name] = {
+ :type => type,
+ :strategy => strategy
+ }
+ end
+ end
+
+ def initialize(options = {})
+ @options = options
+ end
+
+ def run
+ construct_graph.run
+ self
+ end
+
+ private
+
+ def construct_graph
+ jobs = self.class._jobs.dup
+
+ root = nil
+ current = nil
+
+ while !jobs.empty?
+ n = self.class._nodes[jobs.shift]
+ node = Noodler::Node.new(n[:type], n[:strategy])
+ if current
+ current << node
+ current = node
+ else
+ current = root = node
+ end
+ end
+
+ root
+ end
+ end
+end

0 comments on commit 22d51c5

Please sign in to comment.