-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
chore: Add postgres dependency for server to startup #36585
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
598ef61
chore: Add postgres dependency for server to startup
abhvsn 62fb7d2
chore: Extract the common functionality to utils file
abhvsn 76e95c3
chore: Forgot to add the remaining changes
abhvsn 412240d
Merge branch 'release' of github.com:appsmithorg/appsmith into chore/…
abhvsn c1f0ca4
chore: Add quotes for node script to avoid issues with the spaces in …
abhvsn a1f8630
chore: Add tests
abhvsn 69e64cf
chore: Add tests
abhvsn 78d9310
chore: Minor refactor
abhvsn 9dbbe4b
chore: Minor refactor
abhvsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/bin/bash | ||
|
||
waitForPostgresAvailability() { | ||
if [ -z "$PG_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 | ||
su postgres -c "pg_isready -h '${PG_DB_HOST}' -p '${PG_DB_PORT}'" | ||
status=$? | ||
|
||
case $status in | ||
0) | ||
tlog "PostgreSQL host '$PG_DB_HOST' is ready." | ||
break | ||
;; | ||
1) | ||
tlog "PostgreSQL host '$PG_DB_HOST' is rejecting connections e.g. due to being in recovery mode or not accepting connections eg. connections maxed out." | ||
;; | ||
2) | ||
tlog "PostgreSQL host '$PG_DB_HOST' is not responding or running." | ||
;; | ||
3) | ||
tlog "The connection check failed e.g. due to network issues or incorrect parameters." | ||
;; | ||
*) | ||
tlog "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 PG_DB_USER="$USER" | ||
export PG_DB_PASSWORD="$PASSWORD" | ||
export PG_DB_HOST="$HOST" | ||
export PG_DB_PORT="$PORT" | ||
export PG_DB_NAME="$DB" | ||
} | ||
|
||
# Example usage of the functions | ||
# waitForPostgresAvailability | ||
# extract_postgres_db_params "postgresql://user:password@localhost:5432/dbname" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quote Command Substitution to Prevent Word Splitting
In the
extract_postgres_db_params
function, the output of thenode
command should be enclosed in quotes to prevent word splitting. Without quotes, parameters containing spaces could lead to incorrect parsing of database credentials.Here's how you can fix it:
By quoting the command substitution, you ensure that all the variables are read correctly, even if they contain spaces.
📝 Committable suggestion
🧰 Tools
🪛 Shellcheck
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abhvsn Can you please modify the code to resolve this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohanarpit I am not able to understand the advantage for this. I have written the tests to see the effect of this change but couldn't find anything, tests are written for following
APPSMITH_DB_URL
input for which the response was exactly same with or without quotes:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/coderabbitai can you please check the above comment, if I'm missing something?