Brian's Waste of Time

Mon, 24 Apr 2006

Ruby Orchestration Language Fun

After the Silicon Valley Ruby Conference I wanted to hack some at a declarative language for web service orchestration. Tests pass, but the web service bindings don't exist yet =( Right now it can be used as a nice declarative process for... er, ruby?

class TestProcess < Test::Unit::TestCase
  
  def test_example
    p = Processor::process :wombats do
      receive :message

      forward :message, :to => :provisioning, 
                        :save_reply => :provision_response
            
      forward :message, :to => :payroll
      
      if_test( proc { provision_response =~ /example/ } ) do
        transform :message, :with => :some_transformer,
                            :save_in => :transformed_message

        forward :transformed_message, :to => :server_group    
      end
                            
      forward :provision_response, :to => :helpdesk
    end    

The rest of the test case just mocks out the services needed and tests that the things works. Not as exciting but here it is for completeness =)

  
    p.services[:provisioning] = lambda do |process, msg| 
      @provision_msg = msg 
      assert process.expecting_response?
      "address@example.com"
    end
    
    p.services[:payroll] = lambda do |process, msg|
      @payroll_msg = msg
    end
    
    p.services[:server_group] = lambda do |process, msg| 
      @server_group_msg = msg
    end
    
    p.services[:helpdesk] = lambda do |process, msg|
      process.suspend
    end
    
    msg = OpenStruct.new
    
    p.start msg
    
    assert_equal @provision_msg, msg    
    assert_equal @payroll_msg, msg
    assert_equal @server_group_msg, "address@example.com"
    assert_equal p.transformed_msg, "address@example.com"
    assert p.suspended?
    
    p.resume "back from helpdesk"
    
    assert p.completed?
  end
end

For the most part I am kind of happy with it. The nasty wart of

if_test( proc { provision_response =~ /example/ } ) do
  transform :message, :with => :some_transformer,
                      :save_in => :transformed_message

  forward :transformed_message, :to => :server_group    
end

annoys me. It's needed to lazily evaluate the construct, using a real if there evaluates it at the wrong time. Astute code readers will also notice I am not testing the transform. Haven't done that yet -- will probably remove it as a first class concept if I do anything more with the code, a transform is just a local service, and services are all wrapped in ruby methods, so instead of importing the service, just provide the transform and voila, your toast is burnt.

Wish ruby continuations were migratable/serializable.

1 writebacks [/src/ruby] permanent link