From f1083a8259b3d2f560b5f3ccb8b47c94e297d7fa Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Thu, 4 Jan 2018 23:42:31 +0100 Subject: Fixed Xds-server re-connection. Add a callback on XDS-Server connect in order to properly register to XDS Server events in all cases (IOW on reconnection). Signed-off-by: Sebastien Douheret --- lib/agent/apiv1.go | 42 ++++++++++++++++++++++++------------------ lib/agent/projects.go | 1 + lib/agent/xdsserver.go | 37 ++++++++++++++++++++++++------------- lib/xaapiv1/sdks.go | 9 +++++---- 4 files changed, 54 insertions(+), 35 deletions(-) diff --git a/lib/agent/apiv1.go b/lib/agent/apiv1.go index d0e5a1c..a66f451 100644 --- a/lib/agent/apiv1.go +++ b/lib/agent/apiv1.go @@ -117,6 +117,30 @@ func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, erro // Declare passthrough routes s.sdksPassthroughInit(svr) + + // Register callback on Connection + svr.ConnectOn(func(server *XdsServer) error { + + // Add server to list + s.xdsServers[server.ID] = svr + + // Register event forwarder + if err := s.sdksEventsForwardInit(server); err != nil { + s.Log.Errorf("XDS Server %v - sdk event forwarding error: %v", server.ID, err) + } + + // Load projects + if err := s.projects.Init(server); err != nil { + s.Log.Errorf("XDS Server %v - project init error: %v", server.ID, err) + } + + // Registered to all events + if err := server.EventRegister(xsapiv1.EVTAll, ""); err != nil { + s.Log.Errorf("XDS Server %v - register all events error: %v", server.ID, err) + } + + return nil + }) } // Established connection @@ -127,24 +151,6 @@ func (s *APIService) AddXdsServer(cfg xdsconfig.XDSServerConf) (*XdsServer, erro delete(s.xdsServers, cfg.ID) } - // Add to map - s.xdsServers[svr.ID] = svr - - // Register event forwarder - s.sdksEventsForwardInit(svr) - - // Load projects - if err == nil && svr.Connected { - err = s.projects.Init(svr) - } - - // Registered to all events - if err == nil && svr.Connected { - if err = svr.EventRegister(xsapiv1.EVTAll, ""); err != nil { - s.Log.Errorf("XDS Server %v - register all events error: %v", svr.ID, err) - } - } - return svr, err } diff --git a/lib/agent/projects.go b/lib/agent/projects.go index 7393364..4e8b0f6 100644 --- a/lib/agent/projects.go +++ b/lib/agent/projects.go @@ -54,6 +54,7 @@ func NewProjects(ctx *Context, st *st.SyncThing) *Projects { // Init Load Projects configuration func (p *Projects) Init(server *XdsServer) error { + svrList := make(map[string]*XdsServer) // If server not set, load for all servers if server == nil { diff --git a/lib/agent/xdsserver.go b/lib/agent/xdsserver.go index 3ec6123..346bdb9 100644 --- a/lib/agent/xdsserver.go +++ b/lib/agent/xdsserver.go @@ -54,16 +54,20 @@ type XdsServer struct { sockEventsLock *sync.Mutex // Private fields - client *common.HTTPClient - ioSock *sio_client.Client - logOut io.Writer - apiRouter *gin.RouterGroup - cmdList map[string]interface{} + client *common.HTTPClient + ioSock *sio_client.Client + logOut io.Writer + apiRouter *gin.RouterGroup + cmdList map[string]interface{} + cbOnConnect OnConnectedCB } // EventCB Event emitter callback type EventCB func(privData interface{}, evtData interface{}) error +// OnConnectedCB connect callback +type OnConnectedCB func(svr *XdsServer) error + // caller Used to chain event listeners type caller struct { id uuid.UUID @@ -135,6 +139,12 @@ func (xs *XdsServer) Connect() error { return err } +// ConnectOn Register a callback on events reception +func (xs *XdsServer) ConnectOn(f OnConnectedCB) error { + xs.cbOnConnect = f + return nil +} + // IsTempoID returns true when server as a temporary id func (xs *XdsServer) IsTempoID() bool { return strings.HasPrefix(xs.ID, _IDTempoPrefix) @@ -536,15 +546,10 @@ func (xs *XdsServer) _CreateConnectHTTP() error { // _Reconnect Re-established connection func (xs *XdsServer) _Reconnect() error { + + // Note that ConnectOn callback will be called (see apiv1.go file) err := xs._Connect(true) - if err == nil { - // Reload projects list for this server - err = xs.projects.Init(xs) - } - if err == nil { - // Register again to all events - err = xs.EventRegister(xsapiv1.EVTAll, "") - } + return err } @@ -575,6 +580,12 @@ func (xs *XdsServer) _Connect(reConn bool) error { } xs.Connected = true + + // Call OnConnect callback + if xs.cbOnConnect != nil { + xs.cbOnConnect(xs) + } + xs._NotifyState() return nil } diff --git a/lib/xaapiv1/sdks.go b/lib/xaapiv1/sdks.go index 589f748..5a20571 100644 --- a/lib/xaapiv1/sdks.go +++ b/lib/xaapiv1/sdks.go @@ -46,10 +46,11 @@ type SDK struct { // SDKInstallArgs JSON parameters of POST /sdks or /sdks/abortinstall commands type SDKInstallArgs struct { - ID string `json:"id" binding:"required"` // install by ID (must be part of GET /sdks result) - Filename string `json:"filename"` // install by using a file - Force bool `json:"force"` // force SDK install when already existing - Timeout int `json:"timeout"` // 1800 == default 30 minutes + ID string `json:"id"` // install by ID (must be part of GET /sdks result) + Filename string `json:"filename"` // install by using a file + Force bool `json:"force"` // force SDK install when already existing + Timeout int `json:"timeout"` // 1800 == default 30 minutes + InstallArgs []string `json:"installArgs"` // args directly passed to add/install script } // SDKManagementMsg Message send during SDK installation or when installation is complete -- cgit 1.2.3-korg