Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timestamps to output; enhance missing file error message #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ ENTRYPOINT ["/sbin/tini", "--", "/docker-entrypoint"]
HEALTHCHECK --interval=5s --timeout=3s \
CMD ps aux | grep '[c]rond' || exit 1

ADD echo_timestamped /usr/sbin/echo_timestamped

CMD ["crond", "-f", "-d", "6", "-c", "/etc/crontabs"]
39 changes: 20 additions & 19 deletions docker-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ fi
# for local testing only
#HOME_DIR=.

echo_timestamped "##### Establishing log file #####"

if [ "${LOG_FILE}" == "" ]; then
LOG_DIR=/var/log/crontab
LOG_FILE=${LOG_DIR}/jobs.log
Expand All @@ -16,6 +18,8 @@ if [ "${LOG_FILE}" == "" ]; then
fi

get_config() {
echo_timestamped "##### Reading configuration #####"

if [ -f "${HOME_DIR}/config.json" ]; then
jq 'map(.)' ${HOME_DIR}/config.json > ${HOME_DIR}/config.working.json
elif [ -f "${HOME_DIR}/config.toml" ]; then
Expand Down Expand Up @@ -112,19 +116,11 @@ EOF
fi
}

#make_host_cmd() {
# HOST_BINARY=$(echo ${1} | jq -r .host)
# TMP_COMMAND=$(echo ${1} | jq -r .command)
# echo "${HOST_BINARY} ${TMP_COMMAND}"
#}

make_cmd() {
if [ "$(echo ${1} | jq -r .image)" != "null" ]; then
make_image_cmd "$1"
elif [ "$(echo ${1} | jq -r .container)" != "null" ]; then
make_container_cmd "$1"
#elif [ "$(echo ${1} | jq -r .host)" != "null" ]; then
# make_host_cmd "$1"
else
echo ${1} | jq -r .command
fi
Expand Down Expand Up @@ -183,18 +179,20 @@ function build_crontab() {

rm -rf ${CRONTAB_FILE}

echo_timestamped "##### crontab generation starting #####"

ONSTART=()
while read i ; do

SCHEDULE=$(jq -r .[$i].schedule ${CONFIG} | sed 's/\*/\\*/g')
if [ "${SCHEDULE}" == "null" ]; then
echo "Schedule Missing: $(jq -r .[$i].schedule ${CONFIG})"
echo_timestamped "Schedule Missing: $(jq -r .[$i].schedule ${CONFIG})"
continue
fi
SCHEDULE=$(parse_schedule ${SCHEDULE} | sed 's/\\//g')

if [ "$(jq -r .[$i].command ${CONFIG})" == "null" ]; then
echo "Command Missing: $(jq -r .[$i].command ${CONFIG})"
echo_timestamped "Command Missing: $(jq -r .[$i].command ${CONFIG})"
continue
fi

Expand All @@ -220,7 +218,7 @@ set -e
#exec > >(read message; echo "\${UUID} \$(date -Iseconds) [info] \$message" | tee -a ${LOG_FILE} )
#exec 2> >(read message; echo "\${UUID} \$(date -Iseconds) [error] \$message" | tee -a ${LOG_FILE} >&2)

echo "Start Cronjob **${SCRIPT_NAME}** ${COMMENT}"
echo_timestamped "Start Cronjob **${SCRIPT_NAME}** ${COMMENT}"

$(make_cmd "$(jq -c .[$i] ${CONFIG})")
EOF
Expand All @@ -230,7 +228,7 @@ EOF
if [ "$(jq -r .[$i].trigger ${CONFIG})" != "null" ]; then
while read j ; do
if [ "$(jq .[$i].trigger[$j].command ${CONFIG})" == "null" ]; then
echo "Command Missing: $(jq -r .[$i].trigger[$j].command ${CONFIG})"
echo_timestamped "Command Missing: $(jq -r .[$i].trigger[$j].command ${CONFIG})"
continue
fi
#TRIGGER_COMMAND=$(make_cmd "$(jq -c .[$i].trigger[$j] ${CONFIG})")
Expand All @@ -239,7 +237,7 @@ EOF
done < <(jq -r '.['$i'].trigger|keys[]' ${CONFIG})
fi

echo "echo \"End Cronjob **${SCRIPT_NAME}** ${COMMENT}\"" >> ${HOME_DIR}/jobs/${SCRIPT_NAME}.sh
echo "echo_timestamped \"End Cronjob **${SCRIPT_NAME}** ${COMMENT}\"" >> ${HOME_DIR}/jobs/${SCRIPT_NAME}.sh

echo "${SCHEDULE} ${COMMAND}" >> ${CRONTAB_FILE}

Expand All @@ -248,12 +246,12 @@ EOF
fi
done < <(jq -r '.|keys[]' ${CONFIG})

echo "##### crontab generation complete #####"
echo_timestamped "##### crontab generation complete #####"
cat ${CRONTAB_FILE}

echo "##### run commands with onstart #####"
echo_timestamped "##### run commands with onstart #####"
for COMMAND in "${ONSTART[@]}"; do
echo "${COMMAND}"
echo_timestamped "${COMMAND}"
${COMMAND} &
done
}
Expand All @@ -263,21 +261,24 @@ ensure_docker_socket_accessible

start_app() {
get_config

echo_timestamped "##### Validating configuration #####"

if [ -f "${HOME_DIR}/config.working.json" ]; then
export CONFIG=${HOME_DIR}/config.working.json
elif [ -f "${HOME_DIR}/config.json" ]; then
export CONFIG=${HOME_DIR}/config.json
else
echo "NO CONFIG FILE FOUND"
echo_timestamped "NO CONFIG FILE FOUND IN ${HOME_DIR}"
fi
if [ "$1" = "crond" ]; then
if [ -f ${CONFIG} ]; then
build_crontab
else
echo "Unable to find ${CONFIG}"
echo_timestamped "Unable to find ${CONFIG}"
fi
fi
echo "$@"
echo_timestamped "$@"
exec "$@"
}

Expand Down
6 changes: 6 additions & 0 deletions echo_timestamped
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env sh

set -e

echo "$(date "+%Y-%m-%d %H:%M:%S %Z") $1"