From 339fbf0df56965b8b928d2fcc5174372f8c35d49 Mon Sep 17 00:00:00 2001 From: remusb Date: Tue, 12 Jun 2018 22:58:15 +0300 Subject: [PATCH] cache: reconnect plex websocket on failures --- backend/cache/handle.go | 4 +--- backend/cache/plex.go | 10 ++++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/backend/cache/handle.go b/backend/cache/handle.go index 0f32194da..0cb0d6851 100644 --- a/backend/cache/handle.go +++ b/backend/cache/handle.go @@ -147,10 +147,8 @@ func (r *Handle) scaleWorkers(desired int) { func (r *Handle) confirmExternalReading() { // if we have a max value of workers - // or there's no external confirmation available // then we skip this step - if len(r.workers) > 1 || - !r.cacheFs().plexConnector.isConnected() { + if len(r.workers) > 1 { return } if !r.cacheFs().plexConnector.isPlaying(r.cachedObject) { diff --git a/backend/cache/plex.go b/backend/cache/plex.go index 3a8dd5d19..765b9fb5d 100644 --- a/backend/cache/plex.go +++ b/backend/cache/plex.go @@ -107,6 +107,9 @@ func (p *plexConnector) closeWebsocket() { } func (p *plexConnector) listenWebsocket() { + p.runningMu.Lock() + defer p.runningMu.Unlock() + u := strings.Replace(p.url.String(), "http://", "ws://", 1) u = strings.Replace(u, "https://", "wss://", 1) conn, err := websocket.Dial(fmt.Sprintf(defPlexNotificationURL, strings.TrimRight(u, "/"), p.token), @@ -127,8 +130,8 @@ func (p *plexConnector) listenWebsocket() { err := websocket.JSON.Receive(conn, notif) if err != nil { fs.Debugf("plex", "%v", err) - time.Sleep(time.Second) - continue + p.closeWebsocket() + break } // we're only interested in play events if notif.Container.Type == "playing" { @@ -229,6 +232,9 @@ func (p *plexConnector) isConfigured() bool { func (p *plexConnector) isPlaying(co *Object) bool { var err error + if !p.isConnected() { + p.listenWebsocket() + } remote := co.Remote() if cr, yes := p.f.isWrappedByCrypt(); yes {