every: Run a code block periodically

This command runs the code inside a block at a regular interval as long as the block returns true. If the block returns nil, no further calls are made. It is important to ensure that a non-nil value is returned until this command is required to be called.

Syntax

every(name, interval = 60, initialValue = nil) {|context|}

where:

name A name used in debugging messages.
interval Time, in seconds, between checks.
initialValue Optional value passed to the first call. On subsequent calls, the return value of the previous call is passed instead. This allows for context to be maintained across invocations.
context Context across invocations. Will be initialValue first, and will subsequently be the return of the previous invocation for all following invocations.

Usage

 1 # Report every 10 seconds the number of nodes still down
 2 #
 3 every('checkDownNodes', 10) {
 4   count = allNodes.inject(0) { |i, node|
 5     i += 1 if !node.isUp
 6   }
 7   info "#{count} node(s) still down." if count > 0
 8   true if count > 0  # Stop if all up
 9 }
10