Class: OpenWFE::SleepExpression

Inherits:
TimeExpression show all
Defined in:
lib/openwfe/expressions/fe_sleep.rb

Overview

The ‘sleep’ expression expects one attribute, either ‘for’, either ‘until’.

<sequence>
    <sleep for="10m12s" />
    <participant ref="alpha" />
</sequence>

will wait for 10 minutes and 12 seconds before sending a workitem to participant ‘alpha’.

In a Ruby process definition, that might look like :

sleep :for => "3m"
sleep "3m"
    #
    # both meaning 'sleep for 3 minutes'

sleep :until => "Mon Dec 03 10:41:58 +0900 2007"
    #
    # sleep until the given point in time

If the ‘until’ attribute points to a time in the past, the sleep expression will simply let the process resume.

Instance Attribute Summary collapse

Attributes inherited from TimeExpression

#applied_workitem, #scheduler_job_id, #scheduler_tags

Attributes inherited from FlowExpression

#apply_time, #attributes, #children, #environment_id, #fei, #parent_id, #raw_representation

Attributes included from Contextual

#application_context

Instance Method Summary collapse

Methods inherited from TimeExpression

#cancel, #unschedule

Methods inherited from FlowExpression

#cancel, #clean_children, #delete_variable, #dup_environment, #fetch_environment, #fetch_text_content, #get_binding, #get_environment, #get_parent, #get_root_environment, #has_attribute, #initialize, is_definition, is_definition?, #lookup_attribute, #lookup_attributes, #lookup_boolean_attribute, #lookup_comma_list_attribute, #lookup_downcase_attribute, #lookup_ref, #lookup_string_attribute, #lookup_sym_attribute, #lookup_value, #lookup_variable, #lookup_vf_attribute, names, #new_environment, new_exp, #owns_its_environment?, #paused?, #remove_child, #reply, #reply_to_parent, #set_variable, #store_itself, #synchronize, #to_s, #to_yaml_properties

Methods included from Contextual

#get_work_directory, #init_service, #lookup

Methods included from Logging

#ldebug, #ldebug_callstack, #lerror, #lfatal, #linfo, #llog, #lunknown, #lwarn

Methods included from OwfeServiceLocator

#get_engine, #get_error_journal, #get_expool, #get_expression_map, #get_expression_pool, #get_expression_storage, #get_expression_storages, #get_journal, #get_participant_map, #get_scheduler, #get_wfid_generator

Methods inherited from ObjectWithMeta

#class_def, meta_def, meta_eval, metaclass

Constructor Details

This class inherits a constructor from OpenWFE::FlowExpression

Instance Attribute Details

#awakening_timeObject

Returns the value of attribute awakening_time.



77
78
79
# File 'lib/openwfe/expressions/fe_sleep.rb', line 77

def awakening_time
  @awakening_time
end

Instance Method Details

#apply(workitem) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/openwfe/expressions/fe_sleep.rb', line 79

def apply (workitem)

    sfor = lookup_string_attribute(:for, workitem)
    suntil = lookup_string_attribute(:until, workitem)

    sfor = fetch_text_content(workitem) \
        if sfor == nil and suntil == nil

    #ldebug { "apply() sfor is '#{sfor}'" }
    #ldebug { "apply() suntil is '#{suntil}'" }

    tuntil = nil

    if suntil

        tuntil = suntil

    elsif sfor

        tfor = Rufus::parse_time_string(sfor)
        #ldebug { "apply() tfor is '#{tfor}'" }
        tuntil = Time.new.to_f + tfor
    end

    #ldebug { "apply() tuntil is '#{tuntil}'" }

    return reply_to_parent(workitem) \
        if not tuntil

    @awakening_time = tuntil
    @applied_workitem = workitem.dup

    determine_scheduler_tags

    reschedule(get_scheduler)
end

#reschedule(scheduler) ⇒ Object

[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.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/openwfe/expressions/fe_sleep.rb', line 137

def reschedule (scheduler)

    return unless @awakening_time

    ldebug do 
        "[re]schedule() " + 
        "will sleep until '#{@awakening_time}' " +
        "(#{Rufus::to_iso8601_date(@awakening_time)})"
    end

    @scheduler_job_id = "sleep_#{self.fei.to_s}"
    
    scheduler.schedule_at(
        @awakening_time,
        { 
            :schedulable => self, 
            :job_id => @scheduler_job_id,
            :tags => @scheduler_tags })

    ldebug do 
        "[re]schedule() @scheduler_job_id is '#{@scheduler_job_id}' "+
        " (scheduler #{scheduler.object_id})"
    end

    store_itself
end

#trigger(params) ⇒ Object

This is the method called by the Scheduler instance attached to the workflow engine when the ‘sleep’ of this expression is over



121
122
123
124
125
126
127
128
129
# File 'lib/openwfe/expressions/fe_sleep.rb', line 121

def trigger (params)

    ldebug do 
        "trigger() #{@fei.to_debug_s} waking up (#{Time.new.to_f}) "+
        "(scheduler #{get_scheduler.object_id})"
    end

    reply_to_parent(@applied_workitem)
end