Skip to content

Commit

Permalink
TASK-23980 #6 Warmup the server after a restart (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Sellier authored May 4, 2020
1 parent a6693b4 commit d333483
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
3 changes: 3 additions & 0 deletions srv/exo/bin/backup_platform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ ${SSH_COMMAND} ${SCRIPT_DIR}/_startElasticsearch.sh
# Start it
${SSH_COMMAND} ${SCRIPT_DIR}/_starteXo.sh

# Warmup the server
${SSH_COMMAND} ${SCRIPT_DIR}/warmup.sh

DOWNTIME_END_TIME=$(date +%s)
if [ "${REMOTE_BACKUP}" == true ]; then
rsync -av ${EXO_USER}@${EXO_PLF_SERVER}:${BACKUP_WORKING_DIR}/tmp_data/* ${BACKUP_DIR}
Expand Down
9 changes: 9 additions & 0 deletions srv/exo/bin/setenv-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ REMOTE_BACKUP=false
BACKUP_ON_RESTORE=true
# Number of backups to keep
BACKUP_RETENTION=7

## Server Warmup
# This configuration allow to perform
# several requests on the server to initiate
# the js compilation and initial cache loading
WARMUP_ACTIVATED=false
WARMUP_URL=http://localhost:8080
WARMUP_USER=root
WARMUP_PASSWORD=gtn
2 changes: 2 additions & 0 deletions srv/exo/bin/start_platform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ ${SSH_COMMAND} ${SCRIPT_DIR}/_startMongo.sh
${SSH_COMMAND} ${SCRIPT_DIR}/_startElasticsearch.sh
${SSH_COMMAND} ${SCRIPT_DIR}/_starteXo.sh

${SSH_COMMAND} ${SCRIPT_DIR}/warmup.sh

echo "[INFO] $(display_date)"
echo "[INFO] Plateform started"
72 changes: 72 additions & 0 deletions srv/exo/bin/warmup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash -eu

# #############################################################################
# Initialize
# #############################################################################
SCRIPT_NAME="${0##*/}"
SCRIPT_DIR="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

set -o pipefail

# Load env settings
source ${SCRIPT_DIR}/setenv.sh
# Load common functions
source ${SCRIPT_DIR}/_functions.sh

echo ""
echo "[INFO] ======================================="
echo "[INFO] = $(display_date) Starting server warmup ..."
echo "[INFO] ======================================="

set +ue
if [ "${WARMUP_ACTIVATED}" != "true" ]; then
echo "[INFO] Warmup script is disabled"
exit 0
fi
set -ue

## Test if curl is installed
set +e
CURL_CMD=$(which curl)
RET=$?
set -e

if [ $RET -ne 0 ]; then
echo "[ERROR] curl command is not installed on the system"
exit 1
fi

COOKIE_TEMP=$(tempfile -m 600 -d /tmp -p warmup)
ASSET_LIST=$(tempfile -m 600 -d /tmp -p warmupassets)

trap "rm -fv ${COOKIE_TEMP} ${ASSET_LIST}" EXIT

echo "[INFO] = $(display_date) Login into ${WARMUP_URL} with user ${WARMUP_USER} ..."

set +e
curl -s -L -c ${COOKIE_TEMP} -b ${COOKIE_TEMP} -XPOST -d "username=${WARMUP_USER}&password=${WARMUP_PASSWORD}" --post302 ${WARMUP_URL} | grep -E -o -e "=[ \"']+/[.a-zA-Z0-9\/-]+\.css[\"']{1}" -e "=[ \"']+/[.a-zA-Z0-9\/-]+\.js[\"']{1}" | tr -d " " | tr -d "\"" | tr -d "=" > ${ASSET_LIST}
RET=$?
set -e

if [ $RET -ne 0 ]; then
echo "[ERROR] Login failed"
exit 1
fi

echo "[INFO] = $(display_date) Login done ..."
echo "[INFO] = $(display_date) $(cat ${ASSET_LIST} | wc -l) assets found ..."
cat ${ASSET_LIST}
echo "[INFO] = $(display_date) Loading them ..."

set +e
for i in $(cat ${ASSET_LIST}); do
${CURL_CMD} -s -L -c /tmp/cookies -b /tmp/cookies ${WARMUP_URL}$i > /dev/null
done
set -e

echo "[INFO] = $(display_date) All assets called ..."

echo ""
echo "[INFO] ======================================="
echo "[INFO] = $(display_date) Warmup done ..."
echo "[INFO] ======================================="

0 comments on commit d333483

Please sign in to comment.