twright-msft / mssql-node-docker-demo-app

Demonstration application using Microsoft SQL Server on Linux and Node in a Docker container.
MIT License
298 stars 231 forks source link

An alternative to the 90 second sleep #11

Closed fh-sholodak closed 4 years ago

fh-sholodak commented 6 years ago

Thanks for the sample! I was able to eliminate your 90s delay using the following & figured I'd share it.

https://github.com/twright-msft/mssql-node-docker-demo-app/blob/f24cccb955284dadd96cfbe576c7c92c84b08ad6/import-data.sh#L2

while [ ! -f /var/opt/mssql/log/errorlog ]
do
  sleep 2
done

tail -f /var/opt/mssql/log/errorlog | while read LOGLINE
do
   [[ "${LOGLINE}" == *"Using 'xpstar.dll' version"* ]] && pkill -P $$ tail
done

Feel free to upvote these 2 if this is useful to you: https://superuser.com/a/449307 https://stackoverflow.com/a/2379904

nicksterx commented 5 years ago

In recent updates the xpstar.dll no longer appears in the log lines. I've modified it now to this:

do
   [[ "${LOGLINE}" == *"SQL Server is now ready for client connections."* ]] && pkill -P $$ tail
done
twright-msft commented 4 years ago

I tried this approach but found that parsing the log for "SQL Server is now ready for client connections" wasnt a reliable predictor of when SQL Server is actually ready for connections. I've implemented a different approach which repeatedly tries to run the sqlcmd command until it succeeds or gives up after 50 attempts. Still not exactly elegant but at least it is not an arbitrary sleep command that adds unnecessary wait time.

run the setup script to create the DB and the schema in the DB

for i in {1..50}; do /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Yukon900 -d master -i setup.sql if [ $? -eq 0 ] then echo "setup.sql completed" break else echo "not ready yet..." sleep 1 fi done