Top of the minute problem

Maybe you never given it a thought but there is a potential problem with that (time based triggers) tend to occur on “top of the minute”, e.g. exactly at minute:00. There is a similar problem at every whole hour. If you space out your time based triggers evenly by randomizing the second (or even minute if the trigger is hourly) your server might run more smoothly and any online services that you call will likely respond faster.

lucid has this functionality built in in the latest version when using one of the predefined cron expression string constants. E.g. EVERY_10_SECONDS, EVERY_15_SECONDS, EVERY_30_SECONDS, EVERY_MINUTE, EVERY_MINUTE_A, EVERY_MINUTE_B, EVERY_OTHER_MINUTE, EVERY_10_MINUTES, EVERY_30_MINUTES, EVERY_HOUR, EVERY_HOUR_A, EVERY_HOUR_B, EVERY_6_HOURS, EVERY_DAY_AT_NOON. Read more in the lucid documentation.

However that is lucid and Jython. I’m sure it can be made in XTend as well.

lucid users won’t have to bother about how it’s made but if someone else is interested what’s going on under the hood and maybe do the same for Xtend, here is the python code that generates the cron expression strings.

import random
EVERY_10_SECONDS = str(int(round(0.5+random.uniform(1, 9))))+"/10 * * * * ?"
EVERY_15_SECONDS = str(int(round(0.5+random.uniform(1, 14))))+"/15 * * * * ?"
EVERY_30_SECONDS = str(int(round(0.5+random.uniform(1, 29))))+"/30 * * * * ?"
EVERY_MINUTE = str(int(round(0.5+random.uniform(3, 57))))+" 0/1 * 1/1 * ? *"
EVERY_MINUTE_A = str(int(round(0.5+random.uniform(3, 57))))+" 0/1 * 1/1 * ? *"
EVERY_MINUTE_B = str(int(round(0.5+random.uniform(3, 57))))+" 0/1 * 1/1 * ? *"
EVERY_OTHER_MINUTE = str(int(round(0.5+random.uniform(3, 57))))+" 0/2 * 1/1 * ? *"
EVERY_10_MINUTES = str(int(round(0.5+random.uniform(3, 57))))+" "+str(int(round(0.5+random.uniform(1, 9))))+"/10 * 1/1 * ? *"
EVERY_30_MINUTES = str(int(round(0.5+random.uniform(3, 57))))+" "+str(int(round(0.5+random.uniform(1, 29))))+"/30 * 1/1 * ? *"
EVERY_HOUR = str(int(round(0.5+random.uniform(3, 57))))+" "+str(int(round(0.5+random.uniform(3, 57))))+" 0/1 1/1 * ? *"
EVERY_HOUR_A = str(int(round(0.5+random.uniform(3, 57))))+" "+str(int(round(0.5+random.uniform(3, 57))))+" 0/1 1/1 * ? *"
EVERY_HOUR_B = str(int(round(0.5+random.uniform(3, 57))))+" "+str(int(round(0.5+random.uniform(3, 57))))+" 0/1 1/1 * ? *"
EVERY_6_HOURS = str(int(round(0.5+random.uniform(3, 57))))+" "+str(int(round(0.5+random.uniform(3, 57))))+" 0/6 1/1 * ? *"
EVERY_DAY_AT_NOON = str(int(round(0.5+random.uniform(3, 57))))+" "+str(int(round(0.5+random.uniform(3, 57))))+" 12 1/1 * ? *"

Cheers!

3 Likes