diff --git a/bazarr/app/signalr_client.py b/bazarr/app/signalr_client.py index 0aa85633f..47ac30fba 100644 --- a/bazarr/app/signalr_client.py +++ b/bazarr/app/signalr_client.py @@ -11,6 +11,7 @@ from requests.exceptions import ConnectionError from signalrcore.hub_connection_builder import HubConnectionBuilder from collections import deque from time import sleep +from websocket._exceptions import WebSocketBadStatusException from constants import headers from app.event_handler import event_stream @@ -66,11 +67,15 @@ class SonarrSignalrClientLegacy: "permissions on that directory and restart Sonarr. Also, if you're a Docker image " "user, you should make sure you properly defined PUID/PGID environment variables. " "Otherwise, please contact Sonarr support.") + except WebSocketBadStatusException: + logging.debug("BAZARR cannot connect to Sonarr SignalR feed using websocket. We'll fall back to " + "SSE.") + self.configure(force_sse=True) + self.restart() else: self.connected = True event_stream(type='badges') logging.info('BAZARR SignalR client for Sonarr is connected and waiting for events.') - finally: if not args.dev: scheduler.add_job(update_series, kwargs={'send_event': True}, max_instances=1) scheduler.add_job(sync_episodes, kwargs={'send_event': True}, max_instances=1) @@ -97,9 +102,9 @@ class SonarrSignalrClientLegacy: logging.error('BAZARR connection to Sonarr SignalR feed has been lost.') self.restart() - def configure(self): + def configure(self, force_sse=False): self.apikey_sonarr = settings.sonarr.apikey - self.connection = Connection(url_sonarr() + "/signalr", self.session) + self.connection = Connection(url_sonarr() + "/signalr", self.session, force_sse=force_sse) self.connection.qs = {'apikey': self.apikey_sonarr} sonarr_hub = self.connection.register_hub('') # Sonarr doesn't use named hub diff --git a/libs/signalr/_connection.py b/libs/signalr/_connection.py index 6471ba670..5ce5e1649 100644 --- a/libs/signalr/_connection.py +++ b/libs/signalr/_connection.py @@ -4,12 +4,13 @@ from threading import Thread from signalr.events import EventHook from signalr.hubs import Hub from signalr.transports import AutoTransport +from signalr.transports._sse_transport import ServerSentEventsTransport class Connection: protocol_version = '1.5' - def __init__(self, url, session): + def __init__(self, url, session, force_sse=False): self.url = url self.__hubs = {} self.qs = {} @@ -23,7 +24,7 @@ class Connection: self.stopping = EventHook() self.exception = EventHook() self.is_open = False - self.__transport = AutoTransport(session, self) + self.__transport = ServerSentEventsTransport(session, self) if force_sse else AutoTransport(session, self) self.__listener_thread = None self.started = False