From c5d4b73a10e76455da5800a56450c4a6cce239ad Mon Sep 17 00:00:00 2001
From: jbhusson <jonah@elixxir.io>
Date: Mon, 10 Feb 2020 13:08:46 -0800
Subject: [PATCH] change how reconnecting to gateway works

---
 cmd/root.go                         | 13 +++++++++----
 notifications/notifications.go      | 24 +++++-------------------
 notifications/notifications_test.go |  6 ++----
 3 files changed, 16 insertions(+), 27 deletions(-)

diff --git a/cmd/root.go b/cmd/root.go
index 31f5270..3b5c093 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -87,7 +87,7 @@ var rootCmd = &cobra.Command{
 		if err != nil {
 			jww.FATAL.Panicf("Failed to Create permissioning host: %+v", err)
 		}
-
+	Setup:
 		ndf, err := notifications.PollNdf(nil, impl.Comms)
 		if err != nil {
 			jww.FATAL.Panicf("Failed to get NDF: %+v", err)
@@ -100,12 +100,17 @@ var rootCmd = &cobra.Command{
 
 		// Start notification loop
 		killChan := make(chan struct{})
+		errChan := make(chan error)
 		go impl.RunNotificationLoop(viper.GetString("firebaseCredentialsPath"),
-			loopDelay,
-			killChan)
+			loopDelay, killChan, errChan)
 
 		// Wait forever to prevent process from ending
-		select {}
+		select {
+		case err := <-errChan:
+			jww.ERROR.Println(err)
+			goto Setup
+		default:
+		}
 	},
 }
 
diff --git a/notifications/notifications.go b/notifications/notifications.go
index ff8a61e..e329989 100644
--- a/notifications/notifications.go
+++ b/notifications/notifications.go
@@ -59,7 +59,7 @@ type NotificationComms interface {
 
 // Main function for this repo accepts credentials and an impl
 // loops continuously, polling for notifications and notifying the relevant users
-func (nb *Impl) RunNotificationLoop(fbCreds string, loopDuration int, killChan chan struct{}) {
+func (nb *Impl) RunNotificationLoop(fbCreds string, loopDuration int, killChan chan struct{}, errChan chan error) {
 	fc := firebase.NewFirebaseComm()
 	for {
 		// Stop execution if killed by channel
@@ -71,13 +71,15 @@ func (nb *Impl) RunNotificationLoop(fbCreds string, loopDuration int, killChan c
 
 		UIDs, err := nb.pollFunc(nb)
 		if err != nil {
-			jww.ERROR.Printf("Failed to poll gateway for users to notify: %+v", err)
+			errChan <- errors.Errorf("Failed to poll gateway for users to notify: %+v", err)
+			return
 		}
 
 		for _, id := range UIDs {
 			_, err := nb.notifyFunc(id, fbCreds, fc, nb.Storage)
 			if err != nil {
-				jww.ERROR.Printf("Failed to notify user with ID %+v: %+v", id, err)
+				errChan <- errors.Errorf("Failed to notify user with ID %+v: %+v", id, err)
+				return
 			}
 		}
 
@@ -169,24 +171,8 @@ func notifyUser(uid string, serviceKeyPath string, fc *firebase.FirebaseComm, db
 // pollForNotifications accepts a gateway host and a RequestInterface (a comms object)
 // It retrieves a list of user ids to be notified from the gateway
 func pollForNotifications(nb *Impl) (strings []string, e error) {
-	updateNdf := func() error {
-		ndf, err := PollNdf(nil, nb.Comms)
-		if err != nil {
-			return errors.Errorf("Could not poll for new NDF: %+v", err)
-		}
-
-		err = nb.UpdateNdf(ndf)
-		if err != nil {
-			return errors.Errorf("Could not update ndf: %+v", err)
-		}
-		return nil
-	}
 	h, ok := nb.Comms.GetHost("gw")
 	if !ok {
-		err := updateNdf()
-		if err != nil {
-			return nil, errors.Errorf("Could not find gateway host & failed to poll for new NDF: %+v", err)
-		}
 		return nil, errors.New("Could not find gateway host")
 	}
 
diff --git a/notifications/notifications_test.go b/notifications/notifications_test.go
index 1aeb444..4ef079e 100644
--- a/notifications/notifications_test.go
+++ b/notifications/notifications_test.go
@@ -29,17 +29,15 @@ func TestRunNotificationLoop(t *testing.T) {
 		return []string{"test1", "test2"}, nil
 	}
 	impl.notifyFunc = func(s3 string, s2 string, comm *firebase.FirebaseComm, storage storage.Storage) (s string, e error) {
-		if s3 == "test1" {
-			return "", errors.New("Failed to notify")
-		}
 		return "good", nil
 	}
 	killChan := make(chan struct{})
+	errChan := make(chan error)
 	go func() {
 		time.Sleep(10 * time.Second)
 		killChan <- struct{}{}
 	}()
-	impl.RunNotificationLoop("", 3, killChan)
+	impl.RunNotificationLoop("", 3, killChan, errChan)
 }
 
 // Test notificationbot's notifyuser function
-- 
GitLab