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

feat(sipi): add clean temp dir cronjob (DEV-2090) #2656

Merged
merged 8 commits into from
May 15, 2023
9 changes: 8 additions & 1 deletion build.sbt
Expand Up @@ -101,6 +101,10 @@ lazy val sipi: Project = Project(id = "sipi", base = file("sipi"))
Universal / mappings ++= {
directory("sipi/scripts")
},
dockerCommands += Cmd(
"RUN",
"mv /sipi/scripts/entrypoint.sh /sipi/ && chmod +x /sipi/entrypoint.sh && apt-get update && apt-get install -y cron curl && rm -rf /var/lib/apt/lists/*"
), // install cron and curl for periodically cleaning temp dir
dockerCommands += Cmd(
"""HEALTHCHECK --interval=15s --timeout=5s --retries=3 --start-period=30s \
|CMD bash /sipi/scripts/healthcheck.sh || exit 1""".stripMargin
Expand All @@ -119,7 +123,10 @@ lazy val sipi: Project = Project(id = "sipi", base = file("sipi"))

// don't filter the rest; don't filter out anything that doesn't match a pattern
case cmd => false
}
},
// add our own entrypoint and also cmd because it is reset when overriding the entrypoint
dockerCommands += ExecCmd("ENTRYPOINT", "/sipi/entrypoint.sh"),
dockerCommands += ExecCmd("CMD", "--config=/sipi/config/sipi.config.lua")
)

//////////////////////////////////////
Expand Down
23 changes: 23 additions & 0 deletions sipi/scripts/clean_temp_dir.sh
@@ -0,0 +1,23 @@
#!/bin/bash

set -eo pipefail

error_msg() {
echo "$(date): failed cleaning temp dir" >> "$log_file"
}
trap error_msg ERR

# Clear log
log_file="/var/log/cleanTempDir.log"
> "$log_file"

echo "$(date): calling clean_temp_dir route" >> "$log_file"

# Call route
curl -u "${CLEAN_TMP_DIR_USER}:${CLEAN_TMP_DIR_PW}" -sS -L --fail 'http://localhost:1024/clean_temp_dir' >> "$log_file" 2>&1
if [ $? -ne 0 ]; then
echo "$(date): route returned an error status" >> "$log_file"
exit 1
fi

echo "$(date): successfully called clean_temp_dir route" >> "$log_file"
21 changes: 21 additions & 0 deletions sipi/scripts/entrypoint.sh
@@ -0,0 +1,21 @@
#!/bin/bash

set -o pipefail

if [ -n "$CLEAN_TMP_DIR_CRON_SCHEDULE" ]; then
parts=$(echo "$CLEAN_TMP_DIR_CRON_SCHEDULE" | wc -w)
command="/bin/bash /sipi/scripts/clean_temp_dir.sh"

# Validate and install clean temp dir crontab
[ "$parts" -eq 5 ] && (crontab -l 2>/dev/null; echo "$CLEAN_TMP_DIR_CRON_SCHEDULE $command") | crontab - 2>/dev/null
if [ $? -ne 0 ]; then
echo "Invalid clean temp dir cron schedule: $CLEAN_TMP_DIR_CRON_SCHEDULE" >&2
exit 1
fi

# Start cron process in background
cron &
fi

# Start SIPI
cd /sipi && ./sipi "$@"
12 changes: 12 additions & 0 deletions sipi/scripts/healthcheck.sh
@@ -1,3 +1,15 @@
#!/bin/bash

if [ -n "$CLEAN_TMP_DIR_CRON_SCHEDULE" ]; then
pgrep -x "cron" >/dev/null
if [ $? -ne 0 ]; then
echo "Cron is not running"
exit 1
fi
fi

curl -sS --fail 'http://localhost:1024/server/test.html'
if [ $? -ne 0 ]; then
echo "SIPI did not respond to /server/test.html route"
exit 1
fi