/random_airflow_schedule.py Secret
Created
May 23, 2022 13:18
Lessons Learned From Running Apache Airflow at Scale - Deterministically-random round crontab generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from hashlib import md5 | |
from random import randint, seed | |
def compute_schedule(dag_id, interval): | |
# create deterministic randomness by seeding PRNG with a hash of the table name: | |
seed(md5(dag_id.encode()).hexdigest()) | |
if interval.endswith("h"): | |
val = int(interval[:-1]) | |
if 24 % val != 0: | |
raise ValueError("Must use a number which evenly divides 24.") | |
offset = randint(0, val-1) | |
minutes = randint(0, 59) | |
return f"{minutes} {offset}/{val} * * *" | |
elif interval.endswith("m"): | |
val = int(interval[:-1]) | |
if 60 % val != 0: | |
raise ValueError("Minutes must use a number which evenly divides 60.") | |
offset = randint(0, val-1) | |
return f"{offset}/{val} * * * *" | |
elif interval == "1d": | |
return f"{randint(0, 59)} {randint(0, 23)} * * *" | |
raise ValueError("Interval must be (1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30)m, (1, 2, 3, 4, 6, 12)h or 1d") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment