lib/openwfe/expressions/fe_time.rb in openwferu-0.9.2 vs lib/openwfe/expressions/fe_time.rb in openwferu-0.9.3

- old
+ new

@@ -37,12 +37,13 @@ # "made in Japan" # # John Mettraux at openwfe.org # -require 'openwfe/otime' require 'openwfe/rudefinitions' +require 'openwfe/utils' +require 'openwfe/util/otime' require 'openwfe/util/scheduler' # # expressions like 'sleep' and 'cron' @@ -55,18 +56,16 @@ # used directly. # It contains a simple get_scheduler() method simplifying the scheduler # localization for <sleep/> and <cron/>. # class TimeExpression < FlowExpression + include Schedulable attr_accessor \ + :applied_workitem, :scheduler_job_id - def get_scheduler - return @application_context[S_SCHEDULER] - end - # # Makes sure to cancel any scheduler job associated with this # expression # def cancel () @@ -93,14 +92,12 @@ # # will wait for 10 minutes and 12 seconds before sending a workitem # to participant 'alpha'. # class SleepExpression < TimeExpression - include Schedulable attr_accessor \ - :sleeping_workitem, :awakening_time def apply (workitem) synchronize do @@ -121,23 +118,13 @@ reply_to_parent(workitem) return end @awakening_time = tuntil - @sleeping_workitem = workitem.dup + @applied_workitem = workitem.dup - ldebug do - "apply() " + - "will sleep until '#{tuntil}' " + - "(#{OpenWFE::to_iso8601_date(tuntil)})" - end - - @scheduler_job_id = get_scheduler.schedule_at(tuntil, self, nil) - - ldebug { "apply() @scheduler_job_id is #{@scheduler_job_id}" } - - store_itself() + reschedule(get_scheduler) end end #def reply (workitem) #end @@ -147,19 +134,127 @@ # the workflow engine when the 'sleep' of this expression is # over # def trigger (params) ldebug do - "trigger() #{@fei.to_debug_s} waking up (#{Time.new.to_f})" + "trigger() #{@fei.to_debug_s} waking up (#{Time.new.to_f}) "+ + "(scheduler #{get_scheduler.object_id})" end - reply_to_parent(@sleeping_workitem) + reply_to_parent(@applied_workitem) end + + # + # [Re]schedules this expression, effectively registering it within + # the scheduler. + # This method is called when the expression is applied and each + # time the owning engine restarts. + # + def reschedule (scheduler) + + ldebug do + "[re]schedule() " + + "will sleep until '#{@awakening_time}' " + + "(#{OpenWFE::to_iso8601_date(@awakening_time)})" + end + + @scheduler_job_id = + scheduler.schedule_at(@awakening_time, self, nil) + + ldebug do + "[re]schedule() @scheduler_job_id is #{@scheduler_job_id} "+ + " (scheduler #{scheduler.object_id})" + end + + store_itself() + end end # - # TODO #6929 : implement me + # <cron tab="0 9-17 * * mon-fri" name="//reminder"> + # <send-reminder/> + # </cron> # class CronExpression < TimeExpression + + attr_accessor \ + :raw_child, :tab, :name + + def apply (workitem) + + if @children.size < 1 + reply_to_parent(workitem) + return + end + + @applied_workitem = workitem.dup + @applied_workitem.flow_expression_id = nil + + @tab = lookup_attribute(:tab, workitem) + @name = lookup_attribute(:name, workitem) + + @raw_child, _fei = get_expression_pool.fetch(@children[0]) + @raw_child.parent_id = nil + + clean_children() + + @children = nil + + # + # schedule self + + reschedule(get_scheduler) + + # + # store self as a variable + # (have to do it after the reschedule, so that the schedule + # info is stored within the variable) + + set_variable(@name, self) + + # + # resume flow + + reply_to_parent(workitem) + end + + def reply (workitem) + # discard silently... should never get called though + end + + #def cancel () + #end + # + # implemented in parent TimeExpression class + + def trigger (params) + # + # launch raw child + + ldebug { "trigger() cron : #{@fei.to_debug_s}" } + + @raw_child.application_context = @application_context + + begin + get_expression_pool.launch_template( + @fei.wfid, @raw_child, @applied_workitem.dup) + rescue + lerror do + "trigger() cron caught exception\n"+ + OpenWFE::exception_to_s($!) + end + end + end + + def reschedule (scheduler) + + @scheduler_id = get_scheduler.schedule(@tab, @name, self, nil) + + ldebug { "reschedule() job id is #{@scheduler_id}" } + + #store_itself() + # + # done by the containing environment itself + end end end