Take care of ST connection lost in ST event monitor.
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Thu, 24 Aug 2017 19:45:32 +0000 (21:45 +0200)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Thu, 24 Aug 2017 19:45:32 +0000 (21:45 +0200)
lib/syncthing/st.go
lib/syncthing/stEvent.go

index b622970..5086994 100644 (file)
@@ -27,12 +27,13 @@ import (
 
 // SyncThing .
 type SyncThing struct {
-       BaseURL string
-       APIKey  string
-       Home    string
-       STCmd   *exec.Cmd
-       STICmd  *exec.Cmd
-       MyID    string
+       BaseURL   string
+       APIKey    string
+       Home      string
+       STCmd     *exec.Cmd
+       STICmd    *exec.Cmd
+       MyID      string
+       Connected bool
 
        // Private fields
        binDir      string
@@ -301,6 +302,7 @@ func (s *SyncThing) StopInotify() {
 // Connect Establish HTTP connection with Syncthing
 func (s *SyncThing) Connect() error {
        var err error
+       s.Connected = false
        s.client, err = common.HTTPNewClient(s.BaseURL,
                common.HTTPClientConfig{
                        URLPrefix:           "/rest",
@@ -327,6 +329,8 @@ func (s *SyncThing) Connect() error {
                return fmt.Errorf("ERROR: cannot retrieve ID")
        }
 
+       s.Connected = true
+
        // Start events monitoring
        err = s.Events.Start()
 
index bf2a809..9ca8b78 100644 (file)
@@ -148,6 +148,8 @@ func (e *Events) getEvents(since int) ([]STEvent, error) {
 func (e *Events) monitorLoop() {
        e.log.Infof("Event monitoring running...")
        since := 0
+       cntErrConn := 0
+       cntErrRetry := 1
        for {
                select {
                case <-e.stop:
@@ -155,11 +157,32 @@ func (e *Events) monitorLoop() {
                        return
 
                case <-time.After(e.MonitorTime * time.Millisecond):
+
+                       if !e.st.Connected {
+                               cntErrConn++
+                               time.Sleep(time.Second)
+                               if cntErrConn > cntErrRetry {
+                                       e.log.Error("ST Event monitor: ST connection down")
+                                       cntErrConn = 0
+                                       cntErrRetry *= 2
+                                       if _, err := e.getEvents(since); err == nil {
+                                               e.st.Connected = true
+                                               cntErrRetry = 1
+                                               // XXX - should we reset since value ?
+                                               goto readEvent
+                                       }
+                               }
+                               continue
+                       }
+
+               readEvent:
                        stEvArr, err := e.getEvents(since)
                        if err != nil {
                                e.log.Errorf("Syncthing Get Events: %v", err)
+                               e.st.Connected = false
                                continue
                        }
+
                        // Process events
                        for _, stEv := range stEvArr {
                                since = stEv.SubscriptionID