From aae6b9bd4de9b2eb5dbce09d17820f5ee6e01dfa Mon Sep 17 00:00:00 2001 From: Andrej Babic Date: Thu, 22 Jul 2021 11:32:51 +0200 Subject: [PATCH] Improve service points for entry --- docker/docker-entrypoint.sh | 8 ++++---- docker/redis_status.sh | 25 +++++++++++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index 7d61edc..ee003bd 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -11,13 +11,14 @@ if [[ -z "${SERVICE_NAME}" ]]; then exit 1; fi -REDIS_HOST="${REDIS_HOST:-127.0.0.1}" REDIS_SKIP="${REDIS_SKIP:-false}" -REDIS_CONFIG_KEY=config."${PIPELINE_NAME}" -REDIS_STATUS_KEY=status."${PIPELINE_NAME}.${SERVICE_NAME}" # Download config from Redis to redis_config.json and start status reporting. if [ "${REDIS_SKIP}" = false ]; then + REDIS_HOST="${REDIS_HOST:-127.0.0.1}" + REDIS_CONFIG_KEY=config."${PIPELINE_NAME}" + REDIS_STATUS_KEY=status."${PIPELINE_NAME}.${SERVICE_NAME}" + redis-cli -h "${REDIS_HOST}" get "${REDIS_CONFIG_KEY}" > redis_config.json CONFIG_BYTES="$(stat -c %s redis_config.json)" @@ -30,6 +31,5 @@ if [ "${REDIS_SKIP}" = false ]; then redis_status.sh & fi - EXECUTABLE="${@:1}" exec "$EXECUTABLE" \ No newline at end of file diff --git a/docker/redis_status.sh b/docker/redis_status.sh index beec419..970914b 100755 --- a/docker/redis_status.sh +++ b/docker/redis_status.sh @@ -1,26 +1,43 @@ #!/bin/sh set -e +# How long the status record should be kept in the database. +EXPIRE_SECONDS=15 +# At which interval should records be refreshed in the database. +STATUS_INTERVAL_SECONDS=10 + if [[ -z "${REDIS_STATUS_KEY}" ]]; then echo "Environment variable REDIS_STATUS_KEY not defined." exit 1; fi -STATUS="$(redis-cli -x set "${REDIS_STATUS_KEY}:config" < redis_config.json)" -if [ ! "${STATUS}" = "OK" ]; then +STATUS="$(redis-cli -x hset "${REDIS_STATUS_KEY} config" < redis_config.json)" +if [ "${STATUS}" != 0 ] && [ "${STATUS}" != 1 ]; then echo "Cound not set service status in Redis: ${STATUS}" exit 1; fi +STATUS="$(redis-cli expire "${REDIS_STATUS_KEY}" ${EXPIRE_SECONDS})" +if [ "${STATUS}" != 1 ]; then + echo "Could not set status expire: ${STATUS}" + exit 1; +fi + while true; do TIMESTAMP="$(date +%s%N)" - STATUS="$(redis-cli set "${REDIS_STATUS_KEY}:heartbeat" ${TIMESTAMP} )" + STATUS="$(redis-cli hset "${REDIS_STATUS_KEY} heartbeat" "${TIMESTAMP}" )" if [ ! "${STATUS}" = "OK" ]; then echo "Cound not set service hearbeat in Redis: ${STATUS}" exit 1; fi + STATUS="$(redis-cli expire "${REDIS_STATUS_KEY}" ${EXPIRE_SECONDS})" + if [ "${STATUS}" != 1 ]; then + echo "Could not set status expire: ${STATUS}" + exit 1; + fi + # Update heartbeat every 10 seconds. - sleep 10 + sleep "${STATUS_INTERVAL_SECONDS}" done