Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2010 17:35
Show Gist options
  • Save anonymous/597086 to your computer and use it in GitHub Desktop.
Save anonymous/597086 to your computer and use it in GitHub Desktop.
### in lib/ruote/engine.rb
# Quick note : the implementation of launch is found in the module
# Ruote::ReceiverMixin that the engine includes.
#
# Some processes have to have one and only one instance of themselves
# running, these are called 'singles' ('singleton' is too object-oriented).
#
# When called, this method will check if an instance of the pdef is
# already running (it uses the process definition name attribute), if
# yes, it will return without having launched anything. If there is no
# such process running, it will launch it (and register it).
#
# TODO : explain return value
#
def launch_single (process_definition, fields={}, variables={})
tree = @context.parser.parse(process_definition)
name = tree[1]['name'] || (tree[1].find { |k, v| v.nil? } || []).first
raise ArgumentError.new(
'process definition is missing a name, cannot launch as single'
) unless name
singles = @context.storage.get('variables', 'singles') || {
'_id' => 'singles', 'type' => 'variables', 'h' => {}
}
wfid = singles['h'][name]
# TODO : what if reserved ?
return wfid if wfid && process(wfid) != nil
singles['h'][name] = '(reserved)'
r = @context.storage.put('variables', singles)
return launch_single(tree, fields, variables) unless r.nil?
#
# reservation of the name failed, get back to the "start"...
#
# all this to prevent races between multiple engines...
wfid = launch(tree, fields, variables)
singles['h'][name] = wfid
r = @context.storage.put('variables', singles)
# TODO : what if r != nil ? Can this happen ?
wfid
end
### test/functional/ft_46_launch_single.rb
class FtLaunchSingleTest < Test::Unit::TestCase
include FunctionalBase
def test_no_name_singles_are_rejected
assert_raise ArgumentError do
@engine.launch_single(Ruote.process_definition do
wait '2y'
echo 'over.'
end)
end
end
def test_launch_single
pdef = Ruote.process_definition 'unique_process' do
wait '2y'
echo 'over.'
end
wfid = @engine.launch_single(pdef)
p @engine.storage.get('variables', 'singles')
# TODO : continue here
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment