Skip to main content Link Search Menu Expand Document (external link)

every

A simplified cron trigger with human-readable syntax.

ValueDescriptionExample
:secondExecute rule every second:second
:minuteExecute rule every minute:minute
:hourExecute rule every hour:hour
:dayExecute rule every day:day
:weekExecute rule every week (on Monday):week
:monthExecute rule every month:month
:yearExecute rule once a year:year
:mondayExecute rule every Monday at midnight:monday
:tuesdayExecute rule every Tuesday at midnight:tuesday
:wednesdayExecute rule every Wednesday at midnight:wednesday
:thursdayExecute rule every Thursday at midnight:thursday
:fridayExecute rule every Friday at midnight:friday
:saturdayExecute rule every Saturday at midnight:saturday
:sundayExecute rule every Sunday at midnight:sunday
[Numeric].secondsExecute rule every X seconds5.seconds
[Numeric].minutesExecute rule every X minutes3.minutes, 1.5.minutes
[Numeric].hoursExecute rule every X hours10.hours
MonthDayExecute rule at midnight on the given MonthDay. The value can either be a MonthDay object or a StringMonthDay.of(3,14), '03-14'
OptionDescriptionExample
:atLimit the execution to specific times of day. The value can either be a String or a TimeOfDay objectat: ‘16:45’ or at: TimeOfDay.new(h: 16, m: 45)

Note: The [Numeric].seconds specifies a Duration / interval. A floating point can also be used to specify a fractional unit of time, e.g. 1.5.hours

Examples

rule 'Log the rule name every minute' do |rule|
  description 'This rule will create a log every minute'
  every :minute
  run { logger.info "Rule '#{rule.name}' executed" }
end
rule 'Log an entry at 11:21' do |rule|
  every :day, at: '11:21'
  run { logger.info("Rule #{rule.name} run at #{TimeOfDay.now}") }
end

# The above rule could also be expressed using TimeOfDay class as below

rule 'Log an entry at 11:21' do |rule|
  every :day, at: TimeOfDay.new(h: 11, m: 21)
  run { logger.info("Rule #{rule.name} run at #{TimeOfDay.now}") }
end
rule 'Log an entry Wednesdays at 11:21' do |rule|
  every :wednesday, at: '11:21'
  run { logger.info("Rule #{rule.name} run at #{TimeOfDay.now}") }
end
rule 'Every 5 seconds' do |rule|
  every 5.seconds
  run { logger.info "Rule #{rule.name} executed" }
end
rule 'Every 14th of Feb at 2pm' do 
  every '02-14', at: '2pm'
  run { logger.info "Happy Valentine's Day!" }
end