cache: reconnect plex websocket on failures

This commit is contained in:
remusb 2018-06-12 22:58:15 +03:00
parent 13ccb39819
commit 339fbf0df5
2 changed files with 9 additions and 5 deletions

View File

@ -147,10 +147,8 @@ func (r *Handle) scaleWorkers(desired int) {
func (r *Handle) confirmExternalReading() { func (r *Handle) confirmExternalReading() {
// if we have a max value of workers // if we have a max value of workers
// or there's no external confirmation available
// then we skip this step // then we skip this step
if len(r.workers) > 1 || if len(r.workers) > 1 {
!r.cacheFs().plexConnector.isConnected() {
return return
} }
if !r.cacheFs().plexConnector.isPlaying(r.cachedObject) { if !r.cacheFs().plexConnector.isPlaying(r.cachedObject) {

10
backend/cache/plex.go vendored
View File

@ -107,6 +107,9 @@ func (p *plexConnector) closeWebsocket() {
} }
func (p *plexConnector) listenWebsocket() { func (p *plexConnector) listenWebsocket() {
p.runningMu.Lock()
defer p.runningMu.Unlock()
u := strings.Replace(p.url.String(), "http://", "ws://", 1) u := strings.Replace(p.url.String(), "http://", "ws://", 1)
u = strings.Replace(u, "https://", "wss://", 1) u = strings.Replace(u, "https://", "wss://", 1)
conn, err := websocket.Dial(fmt.Sprintf(defPlexNotificationURL, strings.TrimRight(u, "/"), p.token), 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) err := websocket.JSON.Receive(conn, notif)
if err != nil { if err != nil {
fs.Debugf("plex", "%v", err) fs.Debugf("plex", "%v", err)
time.Sleep(time.Second) p.closeWebsocket()
continue break
} }
// we're only interested in play events // we're only interested in play events
if notif.Container.Type == "playing" { if notif.Container.Type == "playing" {
@ -229,6 +232,9 @@ func (p *plexConnector) isConfigured() bool {
func (p *plexConnector) isPlaying(co *Object) bool { func (p *plexConnector) isPlaying(co *Object) bool {
var err error var err error
if !p.isConnected() {
p.listenWebsocket()
}
remote := co.Remote() remote := co.Remote()
if cr, yes := p.f.isWrappedByCrypt(); yes { if cr, yes := p.f.isWrappedByCrypt(); yes {