Skip to content
Snippets Groups Projects
Commit c5d4b73a authored by Jonah Husson's avatar Jonah Husson
Browse files

change how reconnecting to gateway works

parent 01c9b68f
No related branches found
No related tags found
No related merge requests found
......@@ -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:
}
},
}
......
......@@ -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")
}
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment