Class: Ruote::Exp::CronExpression

Inherits:
FlowExpression show all
Defined in:
lib/ruote/exp/fe_cron.rb

Overview

This expression executes its children expression according to a cron schedule or at a given frequency.

cron '15 4 * * sun' do # every sunday at 0415
  subprocess :ref => 'refill_the_acid_baths'
end

or

every '10m' do
  send_reminder # subprocess or participant
end

The ‘tab’ or ‘interval’ attributes may be used, this is a bit more verbose, but, for instance, in XML, it is quite necessary :

<cron tab="15 4 * * sun">
  <subprocess ref="refill_the_acid_baths" />
<cron>

Triggered children subprocesses are ‘forgotten’. This implies they will never reply to the cron/every expression and they won’t get cancelled when the cron/every expression gets cancelled (the cron/every schedule gets cancelled though, no new children will get cancelled).

“man 5 crontab” in the command line of your favourite unix system might help you with the semantics of the string expected by the cron expression.

an example use case

The cron/every expression appears often in scenarii like :

concurrence :count => 1 do

  participant 'operator'

  cron '0 9 * * 1-5' do # send a reminder every weekday at 0900
    notify 'operator'
  end
end

With a subprocess, this could become a bit more reusable :

Ruote.process_defintion :name => 'sample' do

  sequence do
    with_reminder :participant => 'operator1'
    with_reminder :participant => 'operator2'
  end

  define 'with_reminder' do
    concurrence :count => 1 do
      participant '${v:participant}'
      cron '0 9 * * 1-5' do # send a reminder every weekday at 0900
        notify '${v:participant}'
      end
    end
  end
end

Constant Summary

Constants inherited from FlowExpression

FlowExpression::COMMON_ATT_KEYS

Instance Attribute Summary

Attributes inherited from FlowExpression

#context, #error, #h

Instance Method Summary collapse

Methods inherited from FlowExpression

#ancestor?, #applied_workitem, #att, #att_text, #attribute, #attribute_text, #attributes, #await, #cancel, #cancel_flanks, #cfei_at, #child_id, #child_ids, #compile_atts, #compile_variables, #debug_id, #deflate, #do, do_action, #do_apply, #do_cancel, #do_fail, #do_pause, #do_persist, #do_reply, #do_reply_to_parent, #do_resume, #do_unpersist, dummy, #fei, fetch, from_h, #handle_on_error, #has_attribute, #initial_persist, #initialize, #is_concurrent?, #iterative_var_lookup, #launch_sub, #lookup_val, #lookup_val_prefix, #lookup_variable, #name, names, #parent, #parent_id, #pause_on_apply, #persist_or_raise, #reply_to_parent, #root, #root_id, #set_variable, #to_h, #tree, #tree_children, #try_persist, #try_unpersist, #unpersist_or_raise, #unset_variable, #update_tree, #variables, #wfid

Methods included from WithMeta

#class_def, included

Methods included from WithH

included

Constructor Details

This class inherits a constructor from Ruote::Exp::FlowExpression

Instance Method Details

#applyObject



94
95
96
97
98
99
# File 'lib/ruote/exp/fe_cron.rb', line 94

def apply

  h.schedule = attribute(:tab) || attribute(:interval) || attribute_text

  reschedule
end

#reply(workitem) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/ruote/exp/fe_cron.rb', line 101

def reply(workitem)

  launch_sub(
    "#{h.fei['expid']}_0",
    tree_children[0],
    :workitem => Ruote.fulldup(h.applied_workitem),
    :forget => true)

  reschedule
end

#rescheduleObject

Note : this method has to be public.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ruote/exp/fe_cron.rb', line 114

def reschedule

  h.schedule_id = @context.storage.put_schedule(
    'cron',
    h.fei,
    h.schedule,
    'action' => 'reply',
    'fei' => h.fei,
    'workitem' => h.applied_workitem)

  @context.storage.delete_schedule(h.schedule_id) if try_persist
    #
    # if the persist failed, immediately unschedule
    # the just scheduled job
    #
    # this is meant to cope with cases where one worker reschedules
    # while another just cancelled
end