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?, #att, #attribute, #attribute_text, #attributes, #compile_atts, #compile_variables, do_action, #do_apply, #do_cancel, #do_fail, #do_pause, #do_persist, #do_reply, #do_resume, #do_unpersist, #expand_atts, #fei, fetch, from_h, #handle_on_error, #has_attribute, #initial_persist, #initialize, #iterative_var_lookup, #launch_sub, #lookup_on_error, #lookup_val, #lookup_val_prefix, #lookup_variable, #name, names, #parent, #parent_id, #persist_or_raise, #reply_to_parent, #set_variable, #to_h, #tree, #tree_children, #try_persist, #try_unpersist, #unpersist_or_raise, #unset_variable, #update_tree, #variables

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
100
# File 'lib/ruote/exp/fe_cron.rb', line 94

def apply

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

  reschedule
end

#cancel(flavour) ⇒ Object



113
114
115
116
117
# File 'lib/ruote/exp/fe_cron.rb', line 113

def cancel(flavour)

  @context.storage.delete_schedule(h.job_id)
  reply_to_parent(h.applied_workitem)
end

#reply(workitem) ⇒ Object



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

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruote/exp/fe_cron.rb', line 121

def reschedule

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

  @context.storage.delete_schedule(h.job_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