<commit_before>#!/bin/sh

ME=$(basename $0)

if [ $# -lt 4 ]; then
	echo "Usage: $ME <marathon-host:port> <consul-host:port> <prefix> <command>"
	exit 1
fi

MARATHON=$1
CONSUL=$2
CONSUL_KV_WAIT=60s
PREFIX=$3
shift 3
APP="$@"

restart() {
	echo "$ME: Restarting ourselves via Marathon"
	curl -s -X POST -H "Content-Type: application/json" http://$MARATHON/v2/apps/$PREFIX/restart
}

terminate_child() {
	echo "$ME: Sending SIGTERM to child process"
	kill -TERM $CHILD_PID
}

quit() {
	kill -KILL $WATCHER_PID
	exit $1
}

watch() {
	local LAST_INDEX=$1
	local HEADERS
	local CURRENT_INDEX
	while :
	do
		HEADERS=$(curl -sS -o /dev/null -D - http://$CONSUL/v1/kv/$PREFIX/?recurse\&wait=$CONSUL_KV_WAIT\&index=$LAST_INDEX)
		CURRENT_INDEX=$(echo "$HEADERS" | grep -i X-Consul-Index: | awk {'print $2'} | tr -d '[[:space:]]')

		# Trigger restart if Consul KV chnges detected
		if [ "$CURRENT_INDEX" != "$LAST_INDEX" ]; then
			restart
			LAST_INDEX=$CURRENT_INDEX
		fi
	done
}

# Forward SIGTERM to underlying process for graceful shutdown
trap 'terminate_child' TERM
trap 'kill -INT $CHILD_PID; quit 0' INT

# Get environment from Consul
RESPONSE=$(curl -sS -i http://$CONSUL/v1/kv/$PREFIX/?recurse)
HEADERS=$(echo "$RESPONSE" | awk '{if(length($0)<2)exit;print}')
INDEX=$(echo "$HEADERS" | grep -i X-Consul-Index: | awk '{print $2}' | tr -d '[[:space:]]')
BODY=$(echo "$RESPONSE" | awk '{if(body)print;if(length($0)<2)body=1}')
ENV=$(echo "$BODY" | jq -r '.[] | [.Key, .Value] | join(" ")' \
| sed "s|$PREFIX/||" \
| grep -v '^\s*$' \
| \
while read KEY VALUE; do
	printf "export $KEY=%s\n" "$(echo "$VALUE" | base64 -d)"
done)

echo "$ME: Adding new variables to the environment:"
echo "$ENV"
eval "$ENV"

echo "$ME: Running $APP"
$APP &
CHILD_PID=$!

echo "$ME: Running Consul watcher"
watch $INDEX &
WATCHER_PID=$!
echo "$ME: Watcher PID $WATCHER_PID"

echo "$ME: Waiting for child PID $CHILD_PID"
while :
do
	wait $CHILD_PID
	WAIT_STATUS=$?
	if [ "$WAIT_STATUS" -le 128 ]; then
		echo "$ME: Child process completed. Exiting with $WAIT_STATUS"
		quit $WAIT_STATUS
	fi<commit_msg>Don't print ENV as it may contain sensitive info<commit_after>#!/bin/sh

ME=$(basename $0)

if [ $# -lt 4 ]; then
	echo "Usage: $ME <marathon-host:port> <consul-host:port> <prefix> <command>"
	exit 1
fi

MARATHON=$1
CONSUL=$2
CONSUL_KV_WAIT=60s
PREFIX=$3
shift 3
APP="$@"

restart() {
	echo "$ME: Restarting ourselves via Marathon"
	curl -s -X POST -H "Content-Type: application/json" http://$MARATHON/v2/apps/$PREFIX/restart
}

terminate_child() {
	echo "$ME: Sending SIGTERM to child process"
	kill -TERM $CHILD_PID
}

quit() {
	kill -KILL $WATCHER_PID
	exit $1
}

watch() {
	local LAST_INDEX=$1
	local HEADERS
	local CURRENT_INDEX
	while :
	do
		HEADERS=$(curl -sS -o /dev/null -D - http://$CONSUL/v1/kv/$PREFIX/?recurse\&wait=$CONSUL_KV_WAIT\&index=$LAST_INDEX)
		CURRENT_INDEX=$(echo "$HEADERS" | grep -i X-Consul-Index: | awk {'print $2'} | tr -d '[[:space:]]')

		# Trigger restart if Consul KV chnges detected
		if [ "$CURRENT_INDEX" != "$LAST_INDEX" ]; then
			restart
			LAST_INDEX=$CURRENT_INDEX
		fi
	done
}

# Forward SIGTERM to underlying process for graceful shutdown
trap 'terminate_child' TERM
trap 'kill -INT $CHILD_PID; quit 0' INT

# Get environment from Consul
RESPONSE=$(curl -sS -i http://$CONSUL/v1/kv/$PREFIX/?recurse)
HEADERS=$(echo "$RESPONSE" | awk '{if(length($0)<2)exit;print}')
INDEX=$(echo "$HEADERS" | grep -i X-Consul-Index: | awk '{print $2}' | tr -d '[[:space:]]')
BODY=$(echo "$RESPONSE" | awk '{if(body)print;if(length($0)<2)body=1}')
ENV=$(echo "$BODY" | jq -r '.[] | [.Key, .Value] | join(" ")' \
| sed "s|$PREFIX/||" \
| grep -v '^\s*$' \
| \
while read KEY VALUE; do
	printf "export $KEY=%s\n" "$(echo "$VALUE" | base64 -d)"
done)

echo "$ME: Adding new variables to the environment"
eval "$ENV"

echo "$ME: Running $APP"
$APP &
CHILD_PID=$!

echo "$ME: Running Consul watcher"
watch $INDEX &
WATCHER_PID=$!
echo "$ME: Watcher PID $WATCHER_PID"

echo "$ME: Waiting for child PID $CHILD_PID"
while :
do
	wait $CHILD_PID
	WAIT_STATUS=$?
	if [ "$WAIT_STATUS" -le 128 ]; then
		echo "$ME: Child process completed. Exiting with $WAIT_STATUS"
		quit $WAIT_STATUS
	fi
{'fraction_non_alphanumeric': 0.18149210903873744, 'fraction_numerical': 0.01291248206599713, 'mean_word_length': 4.35595390524968, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1412530', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1671, 'n_words': 457}