diff --git a/cmd/root.go b/cmd/root.go index 31f5270d2f919c78c6f15743e088b801473f6cd3..3b5c0939eada3797d5b59b97a771e695a242b74c 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 ff8a61e2f4d455bbdd9eb8034b0481be6b3d4308..e329989648a3fa828e5704faea2021be3c674dec 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 1aeb44482444caa84d6331ae5c9dedba69ebc189..4ef079eceb39c93012910c5b5893ad06a3bca1e4 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