Skip to end of metadata
Go to start of metadata

Contents

Introduction

One of the main features of hcron is split+join combined with indexing. Using these we will set up a rotor event.

Event

We need a mechanism that will drive the rotor. The most obvious is to use one of the hcron datetime variables. We will use HCRON_SCHEDULE_DATETIME.

The event will select a text string based on the minute value. We could do this for each of the sixty minutes, but we'll just use the ones value of the minute and ignore the tens (e.g., 11-> 1, 23 -> 3). Each time the event fires, we'll send out an email.

WORDS=zero:one:two:three:four:five:six:seven:eight:nine
MIN=$HCRON_SCHEDULE_DATETIME[4]
IDX=$MIN[?!-1]
WORD=$WORDS[$IDX]
 
as_user=
host=<host>
command=/bin/true
notify_email=<emailaddr>
notify_subject=DATETIME ($HCRON_SCHEDULE_DATETIME) MIN ($MIN) IDX ($IDX) WORD ($WORD)
notify_message=
when_month=*
when_day=*
when_hour=*
when_minute=*
when_dow=*

Notes:

  • WORDS contains our list of text strings; one for each rotor position
  • MIN is the minute value (always 2-digits; leading 0 if necessary)
  • IDX is the ones minute value; the "?!" is used to split using an empty separator
  • WORD is the text string selected from WORDS using IDX as the index
  • the event fires each minute

The subject lines of the emails received will look something like:

DATETIME (2015:12:19:14:10:00:50:6) MIN (10) IDX (0) WORD (zero)
DATETIME (2015:12:19:14:11:00:50:6) MIN (11) IDX (1) WORD (one)
DATETIME (2015:12:19:14:12:00:50:6) MIN (12) IDX (2) WORD (two)
DATETIME (2015:12:19:14:13:00:50:6) MIN (13) IDX (3) WORD (three)
DATETIME (2015:12:19:14:14:00:50:6) MIN (14) IDX (4) WORD (four)
DATETIME (2015:12:19:14:15:00:50:6) MIN (15) IDX (5) WORD (five)
DATETIME (2015:12:19:14:16:00:50:6) MIN (16) IDX (6) WORD (six)
DATETIME (2015:12:19:14:17:00:50:6) MIN (17) IDX (7) WORD (seven)
DATETIME (2015:12:19:14:18:00:50:6) MIN (18) IDX (8) WORD (eight)
DATETIME (2015:12:19:14:19:00:50:6) MIN (19) IDX (9) WORD (nine)

Instead of using the ones minute value as an index, we can use the tens minute value. The range would then be 0-5 (for 00, 10, 20, 30, 40, and 50). The event file would be modified with:

WORDS=zero:one:two:three:four:five
MIN=$HCRON_SCHEDULE_DATETIME[4]
IDX=$MIN[?!1]

Notes:

  • keep only required strings in WORDS; extra words will not be a problem, though
  • IDX is set to the tens minute value

Conclusion

Although, the example above is simple, it shows how time can be used as a rotor to select from a list of strings. Instead of sending out emails, the select value could have been a hostname, a username, a command, or a combination. In effect, this method provides a time-based round-robin selection mechanism.

  • No labels