From 605c32123f39fb5e4a56311d8278d95cc30fff0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Kwakernaak?= <58462885+andre-aktivconsultancy@users.noreply.github.com> Date: Thu, 23 Dec 2021 10:55:20 +0100 Subject: [PATCH 1/3] ensure postgres database is always started The Postgres database would only get started on first launch. When starting a second time e.g. after a `docker-compose down` and `docker-compose up`, the database was not started resulting in a failure when ThingsBoard is starting. (cherry picked from commit 7d5153f52ee4033c8a7ffab0a678950122b982eb) --- msa/tb/docker-postgres/start-db.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/msa/tb/docker-postgres/start-db.sh b/msa/tb/docker-postgres/start-db.sh index ff2805feab..def5165f7a 100644 --- a/msa/tb/docker-postgres/start-db.sh +++ b/msa/tb/docker-postgres/start-db.sh @@ -22,14 +22,22 @@ PG_CTL=$(find /usr/lib/postgresql/ -name pg_ctl) if [ ! -d ${PGDATA} ]; then mkdir -p ${PGDATA} ${PG_CTL} initdb +else + ${PG_CTL} start fi exec setsid nohup postgres >> ${PGLOG}/postgres.log 2>&1 & if [ ! -f ${firstlaunch} ]; then sleep 2 - while ! psql -U ${pkg.user} -d postgres -c "CREATE DATABASE thingsboard" + while ! psql -U thingsboard -d postgres -c "CREATE DATABASE thingsboard" do sleep 1 done -fi \ No newline at end of file +else + until pg_isready --dbname thingsboard --quiet + do + sleep 1 + echo "Waiting for db" + done +fi From 4e0e1d3caedb2884fbe7b0af8d9907d8847ed52d Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Tue, 14 Jun 2022 19:16:38 +0300 Subject: [PATCH 2/3] Postgres startup script improved. will wait for postgres is ready about 300+ seconds. Postgres may do some recovery stuff on startup. --- msa/tb/docker-cassandra/start-db.sh | 7 +++++++ msa/tb/docker-postgres/start-db.sh | 9 ++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/msa/tb/docker-cassandra/start-db.sh b/msa/tb/docker-cassandra/start-db.sh index 92423de9a8..8f8e4a7c35 100644 --- a/msa/tb/docker-cassandra/start-db.sh +++ b/msa/tb/docker-cassandra/start-db.sh @@ -32,6 +32,13 @@ if [ ! -f ${firstlaunch} ]; then do sleep 1 done +else + RETRIES="${PG_ISREADY_RETRIES:-300}" + until pg_isready -U ${pkg.user} -d thingsboard --quiet || [ $RETRIES -eq 0 ] + do + echo "Connecting to Postgres, $((RETRIES--)) attempts left..." + sleep 1 + done fi cassandra_data_dir=${CASSANDRA_DATA} diff --git a/msa/tb/docker-postgres/start-db.sh b/msa/tb/docker-postgres/start-db.sh index def5165f7a..0962968222 100644 --- a/msa/tb/docker-postgres/start-db.sh +++ b/msa/tb/docker-postgres/start-db.sh @@ -22,22 +22,21 @@ PG_CTL=$(find /usr/lib/postgresql/ -name pg_ctl) if [ ! -d ${PGDATA} ]; then mkdir -p ${PGDATA} ${PG_CTL} initdb -else - ${PG_CTL} start fi exec setsid nohup postgres >> ${PGLOG}/postgres.log 2>&1 & if [ ! -f ${firstlaunch} ]; then sleep 2 - while ! psql -U thingsboard -d postgres -c "CREATE DATABASE thingsboard" + while ! psql -U ${pkg.user} -d postgres -c "CREATE DATABASE thingsboard" do sleep 1 done else - until pg_isready --dbname thingsboard --quiet + RETRIES="${PG_ISREADY_RETRIES:-300}" + until pg_isready -U ${pkg.user} -d thingsboard --quiet || [ $RETRIES -eq 0 ] do + echo "Connecting to Postgres, $((RETRIES--)) attempts left..." sleep 1 - echo "Waiting for db" done fi From 92818934aba5e73d23a6bfd5e2bfc64d5d1969f8 Mon Sep 17 00:00:00 2001 From: Sergey Matvienko Date: Tue, 14 Jun 2022 22:40:01 +0300 Subject: [PATCH 3/3] Postgres startup script improved, Install db script improved. First launch file writes only ofter success install. More verbosity added during Postgresql startup --- msa/tb/docker-cassandra/start-db.sh | 26 +++++++++++++------------- msa/tb/docker-postgres/start-db.sh | 26 +++++++++++++------------- msa/tb/docker/install-tb.sh | 2 ++ msa/tb/docker/start-tb.sh | 17 ++++++++++------- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/msa/tb/docker-cassandra/start-db.sh b/msa/tb/docker-cassandra/start-db.sh index 8f8e4a7c35..d42281d155 100644 --- a/msa/tb/docker-cassandra/start-db.sh +++ b/msa/tb/docker-cassandra/start-db.sh @@ -24,23 +24,23 @@ if [ ! -d ${PGDATA} ]; then ${PG_CTL} initdb fi -exec setsid nohup postgres >> ${PGLOG}/postgres.log 2>&1 & +echo "Starting Postgresql..." +${PG_CTL} start + +RETRIES="${PG_ISREADY_RETRIES:-300}" +until pg_isready -U ${pkg.user} -d postgres --quiet || [ $RETRIES -eq 0 ] +do + echo "Connecting to Postgres, $((RETRIES--)) attempts left..." + sleep 1 +done if [ ! -f ${firstlaunch} ]; then - sleep 2 - while ! psql -U ${pkg.user} -d postgres -c "CREATE DATABASE thingsboard" - do - sleep 1 - done -else - RETRIES="${PG_ISREADY_RETRIES:-300}" - until pg_isready -U ${pkg.user} -d thingsboard --quiet || [ $RETRIES -eq 0 ] - do - echo "Connecting to Postgres, $((RETRIES--)) attempts left..." - sleep 1 - done + echo "Creating database..." + psql -U ${pkg.user} -d postgres -c "CREATE DATABASE thingsboard" fi +echo "Postgresql is ready" + cassandra_data_dir=${CASSANDRA_DATA} cassandra_data_link=/var/lib/cassandra diff --git a/msa/tb/docker-postgres/start-db.sh b/msa/tb/docker-postgres/start-db.sh index 0962968222..917287f35c 100644 --- a/msa/tb/docker-postgres/start-db.sh +++ b/msa/tb/docker-postgres/start-db.sh @@ -24,19 +24,19 @@ if [ ! -d ${PGDATA} ]; then ${PG_CTL} initdb fi -exec setsid nohup postgres >> ${PGLOG}/postgres.log 2>&1 & +echo "Starting Postgresql..." +${PG_CTL} start + +RETRIES="${PG_ISREADY_RETRIES:-300}" +until pg_isready -U ${pkg.user} -d postgres --quiet || [ $RETRIES -eq 0 ] +do + echo "Connecting to Postgres, $((RETRIES--)) attempts left..." + sleep 1 +done if [ ! -f ${firstlaunch} ]; then - sleep 2 - while ! psql -U ${pkg.user} -d postgres -c "CREATE DATABASE thingsboard" - do - sleep 1 - done -else - RETRIES="${PG_ISREADY_RETRIES:-300}" - until pg_isready -U ${pkg.user} -d thingsboard --quiet || [ $RETRIES -eq 0 ] - do - echo "Connecting to Postgres, $((RETRIES--)) attempts left..." - sleep 1 - done + echo "Creating database..." + psql -U ${pkg.user} -d postgres -c "CREATE DATABASE thingsboard" fi + +echo "Postgresql is ready" \ No newline at end of file diff --git a/msa/tb/docker/install-tb.sh b/msa/tb/docker/install-tb.sh index ff9ae7f496..3395e63c84 100644 --- a/msa/tb/docker/install-tb.sh +++ b/msa/tb/docker/install-tb.sh @@ -46,6 +46,8 @@ source "${CONF_FOLDER}/${configfile}" echo "Starting ThingsBoard installation ..." +set -e + java -cp ${jarfile} $JAVA_OPTS -Dloader.main=org.thingsboard.server.ThingsboardInstallApplication \ -Dinstall.load_demo=${loadDemo} \ -Dspring.jpa.hibernate.ddl-auto=none \ diff --git a/msa/tb/docker/start-tb.sh b/msa/tb/docker/start-tb.sh index 5c3b17fb72..30a16413b6 100755 --- a/msa/tb/docker/start-tb.sh +++ b/msa/tb/docker/start-tb.sh @@ -25,15 +25,18 @@ firstlaunch=${DATA_FOLDER}/.firstlaunch source "${CONF_FOLDER}/${configfile}" if [ ! -f ${firstlaunch} ]; then - install-tb.sh --loadDemo - touch ${firstlaunch} + install-tb.sh --loadDemo && touch ${firstlaunch} fi -echo "Starting ThingsBoard ..." +if [ -f ${firstlaunch} ]; then + echo "Starting ThingsBoard ..." -java -cp ${jarfile} $JAVA_OPTS -Dloader.main=org.thingsboard.server.ThingsboardServerApplication \ - -Dspring.jpa.hibernate.ddl-auto=none \ - -Dlogging.config=${CONF_FOLDER}/logback.xml \ - org.springframework.boot.loader.PropertiesLauncher + java -cp ${jarfile} $JAVA_OPTS -Dloader.main=org.thingsboard.server.ThingsboardServerApplication \ + -Dspring.jpa.hibernate.ddl-auto=none \ + -Dlogging.config=${CONF_FOLDER}/logback.xml \ + org.springframework.boot.loader.PropertiesLauncher +else + echo "ERROR: ThingsBoard is not installed" +fi stop-db.sh \ No newline at end of file