| Class | OpenWFE::CronExpression |
| In: |
lib/openwfe/expressions/fe_cron.rb
|
| Parent: | TimeExpression |
Scheduling subprocesses for repeating execution
<cron tab="0 9-17 * * mon-fri">
<send-reminder/>
</cron>
In this short process definition snippet, the subprocess "send-reminder" will get triggered once per hour (minute 0) from 0900 to 1700 and this, from monday to friday.
It‘s possible to specify ‘every’ instead of ‘tab’ :
cron :every => "10m3s" do
send_reminder
end
The subprocess ‘send_reminder’ will thus be triggered every ten minutes and three seconds.
The cron expression never replies [to its parent expression]. The classical usage for it is with a concurrence set to expect only one reply :
concurrence :count => 1 do
participant :toto
cron :every => "10m" do
send_reminder_email :target => "[email protected]"
end
end
The sub process ‘send_reminder_email’ will thus be triggered every 10 minutes while concurrence is waiting for the answer (reply) of the participant :toto.
If the process instance containing a cron is paused, the cron won‘t get triggered until the process is resumed.
| counter | [RW] | Keeping track of how many times the cron fired. |
| every | [RW] | If ‘tab’ is not, then ‘every’ should, the expression will trigger at the frequency specified here (like for example "10m3s). |
| tab | [RW] | The cron ‘tab’, something like "0 9-17 * * mon-fri" |
# File lib/openwfe/expressions/fe_cron.rb, line 117
117: def apply (workitem)
118:
119: return reply_to_parent(workitem) \
120: if @children.size < 1
121:
122: @counter = 0
123: #@engine_cron = false
124:
125: @applied_workitem = workitem.dup
126: @applied_workitem.flow_expression_id = nil
127:
128: @tab = lookup_attribute :tab, workitem
129: @every = lookup_attribute :every, workitem
130:
131: #@name = lookup_attribute :name, workitem
132: #@name = fei.to_s unless @name
133:
134: #if @name[0, 2] == '//'
135: # @name = @name[1..-1]
136: # @engine_cron = true
137: #end
138:
139: #forget = lookup_boolean_attribute :forget, workitem
140:
141: #@raw_child, _fei = get_expression_pool.fetch @children[0]
142: #@raw_child.parent_id = nil
143: #clean_children
144: #@children = nil
145:
146: determine_scheduler_tags
147:
148: #
149: # schedule self
150:
151: reschedule get_scheduler
152:
153: #
154: # store self as a variable
155: # (have to do it after the reschedule, so that the schedule
156: # info is stored within the variable (within self))
157:
158: #set_variable @name, self
159: #set_variable @name, @fei
160:
161: #
162: # resume flow (if not a engine level cron)
163:
164: #reply_to_parent(workitem) unless @engine_cron
165: #reply_to_parent(workitem) if forget
166: end
# File lib/openwfe/expressions/fe_cron.rb, line 168
168: def reply (workitem)
169: # discard silently... should never get called though
170: end
This method is called at the first schedule of this expression or each time the engine is restarted and this expression has to be rescheduled.
# File lib/openwfe/expressions/fe_cron.rb, line 220
220: def reschedule (scheduler)
221:
222: #return unless @applied_workitem
223:
224: #@scheduler_job_id = if @engine_cron
225: # "/" + @name
226: #else
227: # "#{@fei.wfid}__#{@scheduler_job_id}"
228: #end
229: @scheduler_job_id = "#{@fei.wfid}__#{@scheduler_job_id}"
230:
231: method, arg0 = if @tab
232: [ :schedule, @tab ]
233: else
234: [ :schedule_every, @every ]
235: end
236:
237: get_scheduler.send(
238: method,
239: arg0,
240: {
241: :schedulable => self,
242: :job_id => @scheduler_job_id,
243: :tags => @scheduler_tags })
244:
245: #ldebug { "reschedule() name is '#{@name}'" }
246: ldebug { "reschedule() job id is '#{@scheduler_job_id}'" }
247: end
This is the method called each time the scheduler triggers this cron. The contained segment of process will get executed.
# File lib/openwfe/expressions/fe_cron.rb, line 177
177: def trigger (params)
178:
179: return if paused?
180:
181: ldebug { "trigger() cron : #{@fei.to_debug_s}" }
182:
183: #@raw_child.application_context = @application_context
184: # done in expool.launch_template()
185:
186: begin
187:
188: #template = @raw_child
189: template, _fei = get_expression_pool.fetch @children[0]
190:
191: get_expression_pool.launch_template(
192: @fei.wfid, nil, @counter, template, @applied_workitem.dup)
193:
194: #
195: # update count and store self
196:
197: @counter += 1
198:
199: #if is_engine_cron?
200: # set_variable @name, self
201: #else
202: # store_itself
203: #end
204: store_itself
205:
206: rescue
207:
208: lerror do
209: "trigger() cron caught exception\n"+
210: OpenWFE::exception_to_s($!)
211: end
212: end
213: end