From 48d62fe68f595e3d353bc0e16266c9d3ef7cb324 Mon Sep 17 00:00:00 2001
From: roadscape <roadscape@users.noreply.github.com>
Date: Wed, 16 Jan 2019 10:17:01 -0600
Subject: [PATCH] support for external postgres

---
 Dockerfile                  |  2 ++
 scripts/entrypoint.sh       | 45 +++++++++++++++++++++++++------------
 scripts/hivesynccontinue.sh | 24 ++++++++++++++++++++
 3 files changed, 57 insertions(+), 14 deletions(-)
 create mode 100644 scripts/hivesynccontinue.sh

diff --git a/Dockerfile b/Dockerfile
index b2ccbc737..e0c7f5b38 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -50,8 +50,10 @@ WORKDIR /app
 
 ADD scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
 ADD scripts/hivesync.sh /usr/local/bin/hivesync.sh
+ADD scripts/hivesynccontinue.sh /usr/local/bin/hivesynccontinue.sh
 RUN chmod +x /usr/local/bin/entrypoint.sh
 RUN chmod +x /usr/local/bin/hivesync.sh
+RUN chmod +x /usr/local/bin/hivesynccontinue.sh
 
 RUN \
     pip3 install . && \
diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh
index c5793c55a..95cfa3e12 100644
--- a/scripts/entrypoint.sh
+++ b/scripts/entrypoint.sh
@@ -5,7 +5,9 @@
 # a non-micro-fork was encountered. Hive has a startup routine which attempts
 # to recover automatically, so database should be kept intact between restarts.
 
-# eb need to set: RUN_IN_EB, S3_BUCKET, SYNC_TO_S3 (boolean) if a syncer
+# eb with self-contained postgres need to set: RUN_IN_EB, S3_BUCKET, and SYNC_TO_S3 (boolean) if a syncer
+# eb with external postgres do not require RUN_IN_EB, S3_BUCKET, or SYNC_TO_S3
+# with external postgres need to set: SYNC_SERVICE if a syncer
 # hive expects: DATABASE_URL, LOG_LEVEL, STEEMD_URL, JUSSI_URL
 # default DATABASE_URL should be postgresql://postgres:postgres@localhost:5432/postgres
 
@@ -65,24 +67,39 @@ if [[ "$RUN_IN_EB" ]]; then
 
     service postgresql restart
   fi
-fi
-
-cd $APP_ROOT
-
-# startup hive
-echo hivemind: starting sync
-exec "${POPULATE_CMD}" sync 2>&1&
+  cd $APP_ROOT
+  # startup hive
+  echo hivemind: starting sync
+  exec "${POPULATE_CMD}" sync 2>&1&
 
-echo hivemind: starting server
-if [[ ! "$SYNC_TO_S3" ]]; then
-    exec "${POPULATE_CMD}" server
+  echo hivemind: starting server
+  if [[ ! "$SYNC_TO_S3" ]]; then
+      exec "${POPULATE_CMD}" server
+  else
+      exec "${POPULATE_CMD}" server --log-level=warning 2>&1&
+      mkdir -p /etc/service/hivesync
+      cp /usr/local/bin/hivesync.sh /etc/service/hivesync/run
+      chmod +x /etc/service/hivesync/run
+      echo hivemind: starting hivesync service
+      runsv /etc/service/hivesync
+  fi
 else
-    exec "${POPULATE_CMD}" server --log-level=warning 2>&1&
+  # start hive with an external postgres
+  cd $APP_ROOT
+  if [[ "$SYNC_SERVICE" ]]; then
+    echo hivemind: starting sync
+    exec "${POPULATE_CMD}" sync 2>&1&
+    echo hivemind: starting server
+    exec "${POPULATE_CMD}" server 2>&1&
+    # make sure hive sync and server continually run
     mkdir -p /etc/service/hivesync
-    cp /usr/local/bin/hivesync.sh /etc/service/hivesync/run
+    cp /usr/local/bin/hivesynccontinue.sh /etc/service/hivesync/run
     chmod +x /etc/service/hivesync/run
-    echo hivemind: starting hivesync service
     runsv /etc/service/hivesync
+  else
+    echo hivemind: starting server
+    exec "${POPULATE_CMD}" server
+  fi
 fi
 
 echo hivemind: application has stopped, see log for errors
diff --git a/scripts/hivesynccontinue.sh b/scripts/hivesynccontinue.sh
new file mode 100644
index 000000000..2e1f68dca
--- /dev/null
+++ b/scripts/hivesynccontinue.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+POPULATE_CMD="$(which hive)"
+
+HIVESYNC_PID=`pgrep -f 'hive sync'`
+
+if [[ ! $? -eq 0 ]]; then
+    echo NOTIFYALERT! hivemindsync quit unexpectedly, restarting hive sync...
+    cd $APP_ROOT
+    exec "${POPULATE_CMD}" sync 2>&1&
+fi
+
+sleep 30
+
+HIVESERVER_PID=`pgrep -f 'hive server'`
+
+if [[ ! $? -eq 0 ]]; then
+    echo NOTIFYALERT! hivemindserver quit unexpectedly, restarting hive server...
+    cd $APP_ROOT
+    exec "${POPULATE_CMD}" server 2>&1&
+fi
+
+# prevent flapping
+sleep 120
\ No newline at end of file
-- 
GitLab