Take care of ST connection lost in ST event monitor.
[src/xds/xds-server.git] / lib / syncthing / st.go
index 9bdb48f..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
@@ -42,6 +43,7 @@ type SyncThing struct {
        conf        *xdsconfig.Config
        client      *common.HTTPClient
        log         *logrus.Logger
+       Events      *Events
 }
 
 // ExitChan Channel used for process exit
@@ -126,6 +128,9 @@ func NewSyncThing(conf *xdsconfig.Config, log *logrus.Logger) *SyncThing {
                conf:    conf,
        }
 
+       // Create Events monitoring
+       s.Events = s.NewEventListener()
+
        return &s
 }
 
@@ -297,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",
@@ -313,9 +319,20 @@ func (s *SyncThing) Connect() error {
                return fmt.Errorf("ERROR: cannot connect to Syncthing (null client)")
        }
 
-       s.client.SetLogger(s.log)
+       // Redirect HTTP log into a file
+       s.client.SetLogLevel(s.conf.Log.Level.String())
+       s.client.LoggerPrefix = "SYNCTHING: "
+       s.client.LoggerOut = s.conf.LogVerboseOut
 
        s.MyID, err = s.IDGet()
+       if err != nil {
+               return fmt.Errorf("ERROR: cannot retrieve ID")
+       }
+
+       s.Connected = true
+
+       // Start events monitoring
+       err = s.Events.Start()
 
        return err
 }