Skip to content
Merged
73 changes: 61 additions & 12 deletions test/bin/start_api_test_server.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
# shellcheck disable=SC2181

set -x
set -u
#set -x
set -eu

export DEBUG=1
export OSMT_STACK_NAME=osmt_api_test
Expand All @@ -15,13 +15,12 @@ export ELASTICSEARCH_TRANSPORT_PORT="29300"
export ELASTICSEARCH_URI="localhost:${ELASTICSEARCH_HTTP_PORT}"
export REDIS_PORT="26379"
export REDIS_URI="localhost:${REDIS_PORT}"
export APP_PORT="28080"
export APP_PORT="8080"
export BASE_DOMAIN="localhost:${APP_PORT}"
export BASE_URL="http://${BASE_DOMAIN}"
export OSMT_FRONT_END_PORT="${APP_PORT}"


declare project_dir
declare -ri LOAD_CI_DATASET="${LOAD_CI_DATASET:-0}"

_get_osmt_project_dir() {
local project_dir; project_dir="$(git rev-parse --show-toplevel 2> /dev/null)"
Expand All @@ -33,17 +32,67 @@ _get_osmt_project_dir() {
echo "${project_dir}"
}

project_dir="$(_get_osmt_project_dir)" || exit 135
curl_with_retry() {
local -i rc=-1
local -i retry_limit=12
until [ ${rc} -eq 0 ] && [ ${retry_limit} -eq 0 ]; do
echo_info "Attempting to request the index page of the OSMT Spring app with curl..."
curl -s "${BASE_URL}" 1>/dev/null 2>/dev/null
rc=$?
if [[ ${rc} -eq 0 ]]; then
echo_info "Index page loaded. Proceeding..."
return 0
fi
if [[ ${retry_limit} -eq 0 ]]; then
echo_err "Could not load the index page."
return 1
fi
if [[ ${rc} -ne 0 ]]; then
echo_info "Could not load the index page. Will retry ${retry_limit} more times. Retrying in 10 seconds..."
fi
let retry_limit-=1
sleep 10
done
}


# start the API test Docker compose stack and Spring app server
"${project_dir}/osmt_cli.sh" -s
echo_info() {
echo "INFO: $*"
}

# load CI static dataset
#"${project_dir}/osmt_cli.sh" -l
echo_err() {
echo "ERROR: $*" 1>&2;
}

# reindex ElasticSearch
#"${project_dir}/osmt_cli.sh" -r
error_handler() {
echo_err "Trapping at error_handler. Exiting"
# clean up API test Docker resources
"${project_dir}/osmt_cli.sh" -e || exit 135
}

main() {
local project_dir; project_dir="$(_get_osmt_project_dir)" || exit 135
local log_file; log_file="${project_dir}/api/target/osmt_spring_app.log"

# start the API test Docker compose stack and Spring app server, detached. Send log files to 'osmt_spring_app.log'
echo_info "Starting OSMT Spring app for ${OSMT_STACK_NAME}. Output is suppressed, because console is detached."
echo_info "See 'osmt_spring_app.log' for console output. Proceeding..."

touch "$log_file"
"${project_dir}/osmt_cli.sh" -s 1>"$log_file" 2>"$log_file" & disown || exit 135

# curl the Spring app and retry for 2 minutes
curl_with_retry || exit 135

if [[ ${LOAD_CI_DATASET} -eq 0 ]]; then
# load CI static dataset
"${project_dir}/osmt_cli.sh" -l || exit 135

# reindex ElasticSearch
"${project_dir}/osmt_cli.sh" -r || exit 135
fi
}

trap error_handler ERR SIGINT SIGTERM

main
45 changes: 43 additions & 2 deletions test/bin/stop_api_test_server.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash
# shellcheck disable=SC2181

set -x
set -u
set -eu

export DEBUG=1
export OSMT_STACK_NAME=osmt_api_test

declare project_dir
declare -r OSMT_APP_CLASS='edu.wgu.osmt.ApplicationKt'

_get_osmt_project_dir() {
local project_dir; project_dir="$(git rev-parse --show-toplevel 2> /dev/null)"
Expand All @@ -19,10 +19,51 @@ _get_osmt_project_dir() {
echo "${project_dir}"
}


get_app_pid(){
local app_pid; app_pid="$(jps -l | grep ${OSMT_APP_CLASS} | awk '{print $1}')"

echo "${app_pid}"
}

check_pid_status_retry(){
for retry_limit in {12..0}; do
if [[ -z "$(get_app_pid)" ]]; then
echo "INFO: Application ${OSMT_APP_CLASS} successfully stopped. Exiting..."
exit 0
fi
echo "INFO: Could not stop application ${OSMT_APP_CLASS}. Will retry ${retry_limit} more times. Retrying in 3 seconds..."
local app_pid; app_pid="$(jps -l | grep ${OSMT_APP_CLASS} | awk '{print $1}')"
echo "${app_pid}"
sleep 3
done

echo "ERROR: Could not stop ${OSMT_APP_CLASS} after 12 retries. Exiting..." 1>&2;
exit 135
}

shutdown_osmt_app(){
echo "INFO: Attempting to stop application ${OSMT_APP_CLASS}."
local app_pid; app_pid="$(get_app_pid)"

if [[ -z "${app_pid}" ]]; then
echo "ERROR: Application ${OSMT_APP_CLASS} not running" 1>&2;
exit 135
else
echo "INFO: Application ${OSMT_APP_CLASS} found PID ${app_pid}."
fi

kill -SIGTERM "${app_pid}"

check_pid_status_retry
}

project_dir="$(_get_osmt_project_dir)" || exit 135

# stop the API test server
shutdown_osmt_app

# clean up API test Docker resources
"${project_dir}/osmt_cli.sh" -e || exit 135