lib/openwfe/participants/participants.rb in openwferu-0.9.5 vs lib/openwfe/participants/participants.rb in openwferu-0.9.6

- old
+ new

@@ -37,40 +37,106 @@ # "made in Japan" # # John Mettraux at openwfe.org # +require 'yaml' + +require 'openwfe/utils' require 'openwfe/participants/participant' # # some base participant implementations # module OpenWFE # - # The PrintParticipant will just emit its name to the - # test tracer if any or to stdout else. - # Used by some unit tests. + # Just dumps the incoming workitem in a file as a YAML String. # - class PrintParticipant + # By default, this participant will not reply to the engine once + # the workitem got dumped to its file, but you can set its + # reply_anyway field to true to make it reply anyway... + # + class FileParticipant include LocalParticipant - def consume (workitem) + attr_accessor :reply_anyway, :workdir - tracer = @application_context['__tracer'] + # + # The constructor expects as a unique optional param either the + # application_context either the 'output' dir for the participant. + # + def initialize (context_or_dir=nil) - if tracer - tracer << workitem.participant_name + if context_or_dir.kind_of? Hash + @application_context = context_or_dir + @workdir = @application_context[:work_directory] else - puts workitem.participant_name + @workdir = context_or_dir end - reply_to_engine(workitem) + @workdir = OpenWFE::DEFAULT_WORK_DIRECTORY unless @workdir + @workdir = "#{@workdir}/out/" + + @reply_anyway = false end - end + # + # The method called by the engine for each incoming workitem. + # + def consume (workitem) + + FileUtils.makedirs(@workdir) unless File.exist?(@workdir) + + file_name = @workdir + determine_file_name(workitem) + + dump_to_file(file_name, workitem) + + reply_to_engine(workitem) if @reply_anyway + end + + # + # This method does the actual job of dumping the workitem (as some + # YAML to a file). + # It can be easily overriden. + # + def dump_to_file (file_name, workitem) + + File.open(file_name, "w") do |file| + file.print encode_workitem(workitem) + end + end + + # + # You can override this method to control into which file (name) + # each workitem gets dumped. + # You could even have a unique file for all workitems transiting + # through this participant. + # + def determine_file_name (workitem) + + fei = workitem.fei + + OpenWFE::ensure_for_filename( + "#{fei.wfid}_#{fei.expression_id}__" + + "#{fei.workflow_definition_name}__" + + "#{fei.workflow_definition_revision}" + + "#{workitem.participant_name}.yaml") + end + + protected + + # + # By default, uses YAML to serialize the workitem + # (of course you can override this method). + # + def encode_workitem (wi) + YAML.dump(wi) + end + end + # # This participant is used by the register_participant() method of # Engine class. # # engine.register_participant("the_boss") do |workitem| @@ -114,9 +180,31 @@ end reply_to_engine(workitem) \ if workitem.kind_of? InFlowWorkItem # else it's a cancel item + end + end + + # + # The PrintParticipant will just emit its name to the + # test tracer if any or to stdout else. + # Used by some unit tests. + # + class PrintParticipant + include LocalParticipant + + def consume (workitem) + + tracer = @application_context['__tracer'] + + if tracer + tracer << workitem.participant_name + else + puts workitem.participant_name + end + + reply_to_engine(workitem) end end end