Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wrapper script for denominator to prevent overrunning API limits. It works by watching the output file, and every 3 zones that are saved, it will pause for 4 seconds (so at most 45 zones/minute are retrieved).
#!/usr/bin/env bash
#
# Quinn Comendant <quinn@strangecode.com>
# 23 Sep 2016 19:31:05
#
# Functions
#
function usage() {
echo "Usage: $(basename $0) FILENAME
Wrapper for \`denominator zone list\` to save all configured zones to the specified file. It will pause denominator for 25 seconds every 20 zones saved. This is required to avoid API limit violations. See https://github.com/Netflix/denominator/issues/376
";
exit 1;
}
#
# Main
#
# Display help if run with invalid arguments.
[[ $# == 0 ]] && usage && exit 0;
[[ $# != 1 ]] && echo "Invalid arguments. Run with no args for help." && exit 1;
OUTFILE="$1";
if [[ -e "$OUTFILE" ]]; then
echo "Not going to overwrite existing file: $OUTFILE";
exit 1;
fi
if [[ -n "$(pidof java)" ]]; then
echo "Another java program appears to be running (PID $(pidof java)). Is denominator already running?"
exit 0;
fi
EVERY=3; # every number of zones saved
PAUSEFOR=4; # seconds
denominator zone list > "$OUTFILE" & sleep 2; DPID=$(pidof java); echo "I will pause PID '$DPID' for $PAUSEFOR sec every $EVERY zones"; while kill -0 $DPID 2>/dev/null; do if [[ -s "$OUTFILE" && $(($(wc -l < "$OUTFILE") % $EVERY)) -eq 0 ]]; then kill -STOP $DPID; sleep $PAUSEFOR; kill -CONT $DPID; fi; done
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment