lib/ruote/exp/fe_participant.rb in ruote-2.3.0.1 vs lib/ruote/exp/fe_participant.rb in ruote-2.3.0.2
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2012, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2013, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -142,17 +142,12 @@
) if h.participant.nil?
#
# trigger on_apply if the participant sports it
- pa = @context.plist.instantiate(
- h.participant, :if_respond_to? => :on_apply)
+ trigger_callback(:on_apply, h.applied_workitem)
- Ruote.participant_send(
- pa, :on_apply, 'workitem' => Ruote::Workitem.new(h.applied_workitem)
- ) if pa
-
#
# dispatch to participant
h.applied_workitem['participant_name'] = h.participant_name
@@ -200,30 +195,68 @@
'workitem' => h.applied_workitem)
end
def reply(workitem)
- pinfo =
- h.participant ||
- @context.plist.lookup_info(h.participant_name, workitem)
+ trigger_callback(:on_reply, workitem)
- pa = @context.plist.instantiate(pinfo, :if_respond_to? => :on_reply)
-
- Ruote.participant_send(
- pa, :on_reply, 'workitem' => Ruote::Workitem.new(workitem)
- ) if pa
-
super(workitem)
end
def reply_to_parent(workitem)
workitem['fields'].delete('params')
workitem['fields'].delete('dispatched_at')
super(workitem)
end
+ # Overrides FlowExpression#handle_on_error. Attempts to call a
+ # potential #on_error method in the participant implementation.
+ #
+ # If that method exists and returns something true-ish, will not call
+ # the super #handle_on_error, it will directly reply to the parent.
+ #
+ def handle_on_error(msg, err)
+
+ r = trigger_callback(:on_error, msg, err)
+
+ if r
+ reply_to_parent(msg['workitem'])
+ true # yes, we've dealt with the error.
+ else
+ super(msg, err)
+ end
+ end
+
protected
+
+ # Used to trigger #on_apply, #on_reply, #on_cancel and #on_error in
+ # the participant implementation.
+ #
+ def trigger_callback(meth, wi_or_msg, err=nil)
+
+ wi, msg, err = if err
+ [ wi_or_msg['workitem'], wi_or_msg, err ]
+ elsif wi_or_msg['workitem']
+ [ wi_or_msg['workitem'], wi_or_msg, nil ]
+ else
+ [ wi_or_msg, nil, nil ]
+ end
+
+ pinfo =
+ h.participant ||
+ @context.plist.lookup_info(h.participant_name, wi)
+
+ pa = @context.plist.instantiate(pinfo, :if_respond_to? => meth)
+
+ return nil unless pa
+
+ args = { 'workitem' => Ruote::Workitem.new(wi) }
+ args['error'] = err if err
+ args['msg'] = msg if msg
+
+ Ruote.participant_send(pa, meth, args)
+ end
# Once the dispatching work (done by the dispatch pool) is done, a
# 'dispatched' msg is sent, we have to flag the participant expression
# as 'dispatched' => true
#