Created
March 27, 2009 10:05
-
-
Save choda/86628 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapted from John Mettraux's ruote quickstart at | |
# http://openwferu.rubyforge.org/quickstart.html | |
# | |
# This script uses the 'atom' module rather than 'atom-tools' so that you can | |
# run it successfully even from behind a proxy (defined in the http_proxy | |
# environment variable) | |
require 'rubygems' | |
require 'openwfe/engine' # sudo gem install ruote | |
require 'atom' # sudo gem install atom | |
require 'uri' | |
# | |
# starting a transient engine (no need to make it persistent) | |
engine = OpenWFE::Engine.new(:definition_in_launchitem_allowed => true) | |
# | |
# a process that fetches the latest pictures from flickr.com and submits | |
# them concurrently to three users for review | |
class PicSelectionProcess < OpenWFE::ProcessDefinition | |
sequence do | |
get_pictures | |
concurrence :merge_type => 'mix' do | |
# pass the picture list to three users concurrently | |
# make sure to let their choice appear in the final workitem | |
# at the end of the concurrence | |
user_alice | |
user_bob | |
user_charly | |
end | |
show_results | |
# display the pictures chosen by the users | |
end | |
end | |
# | |
# fetching the flickr.com pictures via Atom | |
engine.register_participant :get_pictures do |workitem| | |
proxy_uri = URI.parse(ENV['http_proxy']) | |
proxy = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) | |
url = URI::parse("http://api.flickr.com/services/feeds/photos_public.gne?tags=#{workitem.tags.join(',')}&format=atom") | |
feed = Atom::Feed.new(proxy.get(url)) | |
workitem.pictures = feed.entries.inject([]) do |a, entry| | |
a << [ entry.title, entry.authors.first.name, entry.links.first.href ] | |
end | |
end | |
# | |
# the users (well, here, just randomly picking a picture) | |
engine.register_participant 'user-.*' do |workitem| | |
workitem.fields[workitem.participant_name] = | |
workitem.pictures[(rand * workitem.pictures.length).to_i] | |
end | |
# | |
# the final participant, it displays the user choices | |
engine.register_participant :show_results do |workitem| | |
puts | |
puts ' users selected those images : ' | |
puts | |
workitem.attributes.each do |k, v| | |
next unless k.match(/^user-.*$/) | |
puts "- #{k} :: #{v.last}" | |
end | |
puts | |
end | |
# | |
# launching the process, requesting pictures tagged 'cat' and 'fish' | |
li = OpenWFE::LaunchItem.new(PicSelectionProcess) | |
li.tags = [ 'cat', 'fish' ] | |
fei = engine.launch(li) | |
#li = OpenWFE::LaunchItem.new(PicSelectionProcess) | |
#li.tags = [ 'flower', 'sun' ] | |
#fei = engine.launch(li) | |
# | |
#li = OpenWFE::LaunchItem.new(PicSelectionProcess) | |
#li.tags = [ 'cow', 'shetland' ] | |
#fei = engine.launch(li) | |
# | |
# it's OK to launch multiple processes in the same Ruote engine | |
# | |
# | |
# workflow engines are asynchronous beasts, have to wait for them | |
# (here we wait for a particular process) | |
engine.wait_for(fei) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment