Skip to content

Commit

Permalink
chore: Add postgres dependency for server to startup
Browse files Browse the repository at this point in the history
  • Loading branch information
abhvsn committed Sep 27, 2024
1 parent 91bea3e commit 598ef61
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions deploy/docker/fs/opt/appsmith/run-java.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,93 @@ cd "/opt/appsmith/server/$mode"
declare -a extra_args
proxy_configured=0

waitForPostgresAvailability() {
if [ -z "$DB_HOST" ]; then
tlog "PostgreSQL host name is empty. Check env variables. Error. Exiting java setup"
exit 2
else

MAX_RETRIES=50
RETRYSECONDS=10
retry_count=0
while true; do
pg_isready -h "${DB_HOST}" -p "${DB_PORT}"
status=$?

case $status in
0)
echo "PostgreSQL host '${DB_HOST}' is ready."
break
;;
1)
echo "PostgreSQL host '${DB_HOST}' is rejecting connections (e.g., due to being in recovery mode or not accepting connections eg. connections maxed out)."
;;
2)
echo "PostgreSQL host '${DB_HOST}' is not responding or running."
;;
3)
echo "The connection check failed (e.g., due to network issues or incorrect parameters)."
;;
*)
echo "pg_isready exited with unexpected status code: $status"
break
;;
esac

retry_count=$((retry_count + 1))
if [ $retry_count -le $MAX_RETRIES ]; then
tlog "PostgreSQL connection failed. Retrying attempt $retry_count/$MAX_RETRIES in $RETRYSECONDS seconds..."
sleep $RETRYSECONDS
else
tlog "Exceeded maximum retry attempts ($MAX_RETRIES). Exiting."
# use exit code 2 to indicate that the script failed to connect to postgres and supervisor conf is set not to restart the program for 2.
exit 2
fi

done
fi
}

# for PostgreSQL, we use APPSMITH_DB_URL=postgresql://username:password@postgresserver:5432/dbname
# Args:
# conn_string (string): PostgreSQL connection string
# Returns:
# None
# Example:
# postgres syntax
# "postgresql://user:password@localhost:5432/appsmith"
# "postgresql://user:password@localhost/appsmith"
# "postgresql://user@localhost:5432/appsmith"
# "postgresql://user@localhost/appsmith"
extract_postgres_db_params() {
local conn_string=$1

# Use node to parse the URI and extract components
IFS=' ' read -r USER PASSWORD HOST PORT DB <<<$(node -e "
const connectionString = process.argv[1];
const pgUri = connectionString.startsWith(\"postgresql://\")
? connectionString
: 'http://' + connectionString; //Prepend a fake scheme for URL parsing
const url = require('url');
const parsedUrl = new url.URL(pgUri);
// Extract the pathname and remove the leading '/'
const db = parsedUrl.pathname.substring(1);
// Default the port to 5432 if it's empty
const port = parsedUrl.port || '5432';
console.log(\`\${parsedUrl.username || '-'} \${parsedUrl.password || '-'} \${parsedUrl.hostname} \${port} \${db}\`);
" "$conn_string")

# Now, set the environment variables
export DB_USER="$USER"
export DB_PASSWORD="$PASSWORD"
export DB_HOST="$HOST"
export DB_PORT="$PORT"
export DB_NAME="$DB"
}

match-proxy-url() {
# Examples:
# http://proxy.example.com:8080/
Expand All @@ -29,6 +116,12 @@ match-proxy-url() {
[[ -n $proxy_host ]]
}
# Extract the database parameters from the APPSMITH_DB_URL and wait for the database to be available
if [[ "$mode" == "pg" ]]; then
extract_postgres_db_params "$APPSMITH_DB_URL"
waitForPostgresAvailability
fi
if match-proxy-url "${HTTP_PROXY-}"; then
extra_args+=(-Dhttp.proxyHost="$proxy_host" -Dhttp.proxyPort="$proxy_port")
if [[ -n $proxy_user ]]; then
Expand Down

0 comments on commit 598ef61

Please sign in to comment.