mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-13 00:32:58 -07:00
Close websocket cleanly on shutdown
This commit is contained in:
parent
9905ebc144
commit
7362dd0bf4
2 changed files with 33 additions and 20 deletions
|
@ -46,6 +46,7 @@ import notifiers
|
||||||
import plextv
|
import plextv
|
||||||
import users
|
import users
|
||||||
import versioncheck
|
import versioncheck
|
||||||
|
import web_socket
|
||||||
import plexpy.config
|
import plexpy.config
|
||||||
|
|
||||||
PROG_DIR = None
|
PROG_DIR = None
|
||||||
|
@ -95,6 +96,7 @@ HTTP_ROOT = None
|
||||||
|
|
||||||
DEV = False
|
DEV = False
|
||||||
|
|
||||||
|
WEBSOCKET = None
|
||||||
WS_CONNECTED = False
|
WS_CONNECTED = False
|
||||||
PLEX_SERVER_UP = None
|
PLEX_SERVER_UP = None
|
||||||
|
|
||||||
|
@ -1622,6 +1624,10 @@ def upgrade():
|
||||||
def shutdown(restart=False, update=False, checkout=False):
|
def shutdown(restart=False, update=False, checkout=False):
|
||||||
cherrypy.engine.exit()
|
cherrypy.engine.exit()
|
||||||
|
|
||||||
|
# Shutdown the websocket connection
|
||||||
|
if WEBSOCKET:
|
||||||
|
web_socket.shutdown()
|
||||||
|
|
||||||
if SCHED.running:
|
if SCHED.running:
|
||||||
SCHED.shutdown(wait=False)
|
SCHED.shutdown(wait=False)
|
||||||
if activity_handler.ACTIVITY_SCHED.running:
|
if activity_handler.ACTIVITY_SCHED.running:
|
||||||
|
|
|
@ -29,7 +29,7 @@ import logger
|
||||||
|
|
||||||
name = 'websocket'
|
name = 'websocket'
|
||||||
opcode_data = (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY)
|
opcode_data = (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY)
|
||||||
ws_reconnect = False
|
ws_shutdown = False
|
||||||
|
|
||||||
|
|
||||||
def start_thread():
|
def start_thread():
|
||||||
|
@ -65,8 +65,18 @@ def on_disconnect():
|
||||||
|
|
||||||
|
|
||||||
def reconnect():
|
def reconnect():
|
||||||
global ws_reconnect
|
shutdown()
|
||||||
ws_reconnect = True
|
logger.info(u"Tautulli WebSocket :: Reconnecting websocket...")
|
||||||
|
start_thread()
|
||||||
|
|
||||||
|
|
||||||
|
def shutdown():
|
||||||
|
global ws_shutdown
|
||||||
|
ws_shutdown = True
|
||||||
|
|
||||||
|
logger.info(u"Tautulli WebSocket :: Disconnecting websocket...")
|
||||||
|
plexpy.WEBSOCKET.close()
|
||||||
|
plexpy.WS_CONNECTED = False
|
||||||
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
|
@ -88,8 +98,8 @@ def run():
|
||||||
else:
|
else:
|
||||||
header = []
|
header = []
|
||||||
|
|
||||||
global ws_reconnect
|
global ws_shutdown
|
||||||
ws_reconnect = False
|
ws_shutdown = False
|
||||||
reconnects = 0
|
reconnects = 0
|
||||||
|
|
||||||
# Try an open the websocket connection
|
# Try an open the websocket connection
|
||||||
|
@ -106,7 +116,7 @@ def run():
|
||||||
logger.info(u"Tautulli WebSocket :: Connection attempt %s." % str(reconnects))
|
logger.info(u"Tautulli WebSocket :: Connection attempt %s." % str(reconnects))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ws = create_connection(uri, header=header)
|
plexpy.WEBSOCKET = create_connection(uri, header=header)
|
||||||
logger.info(u"Tautulli WebSocket :: Ready")
|
logger.info(u"Tautulli WebSocket :: Ready")
|
||||||
plexpy.WS_CONNECTED = True
|
plexpy.WS_CONNECTED = True
|
||||||
except (websocket.WebSocketException, IOError, Exception) as e:
|
except (websocket.WebSocketException, IOError, Exception) as e:
|
||||||
|
@ -117,12 +127,15 @@ def run():
|
||||||
|
|
||||||
while plexpy.WS_CONNECTED:
|
while plexpy.WS_CONNECTED:
|
||||||
try:
|
try:
|
||||||
process(*receive(ws))
|
process(*receive(plexpy.WEBSOCKET))
|
||||||
|
|
||||||
# successfully received data, reset reconnects counter
|
# successfully received data, reset reconnects counter
|
||||||
reconnects = 0
|
reconnects = 0
|
||||||
|
|
||||||
except websocket.WebSocketConnectionClosedException:
|
except websocket.WebSocketConnectionClosedException:
|
||||||
|
if ws_shutdown:
|
||||||
|
break
|
||||||
|
|
||||||
if reconnects == 0:
|
if reconnects == 0:
|
||||||
logger.warn(u"Tautulli WebSocket :: Connection has closed.")
|
logger.warn(u"Tautulli WebSocket :: Connection has closed.")
|
||||||
|
|
||||||
|
@ -136,31 +149,25 @@ def run():
|
||||||
logger.warn(u"Tautulli WebSocket :: Reconnection attempt %s." % str(reconnects))
|
logger.warn(u"Tautulli WebSocket :: Reconnection attempt %s." % str(reconnects))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ws = create_connection(uri, header=header)
|
plexpy.WEBSOCKET = create_connection(uri, header=header)
|
||||||
logger.info(u"Tautulli WebSocket :: Ready")
|
logger.info(u"Tautulli WebSocket :: Ready")
|
||||||
plexpy.WS_CONNECTED = True
|
plexpy.WS_CONNECTED = True
|
||||||
except (websocket.WebSocketException, IOError, Exception) as e:
|
except (websocket.WebSocketException, IOError, Exception) as e:
|
||||||
logger.error(u"Tautulli WebSocket :: %s." % e)
|
logger.error(u"Tautulli WebSocket :: %s." % e)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
ws.shutdown()
|
shutdown()
|
||||||
plexpy.WS_CONNECTED = False
|
|
||||||
break
|
break
|
||||||
|
|
||||||
except (websocket.WebSocketException, Exception) as e:
|
except (websocket.WebSocketException, Exception) as e:
|
||||||
|
if ws_shutdown:
|
||||||
|
break
|
||||||
|
|
||||||
logger.error(u"Tautulli WebSocket :: %s." % e)
|
logger.error(u"Tautulli WebSocket :: %s." % e)
|
||||||
ws.shutdown()
|
shutdown()
|
||||||
plexpy.WS_CONNECTED = False
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# Check if we recieved a restart notification and close websocket connection cleanly
|
if not plexpy.WS_CONNECTED and not ws_shutdown:
|
||||||
if ws_reconnect:
|
|
||||||
logger.info(u"Tautulli WebSocket :: Reconnecting websocket...")
|
|
||||||
ws.shutdown()
|
|
||||||
plexpy.WS_CONNECTED = False
|
|
||||||
start_thread()
|
|
||||||
|
|
||||||
if not plexpy.WS_CONNECTED and not ws_reconnect:
|
|
||||||
on_disconnect()
|
on_disconnect()
|
||||||
|
|
||||||
logger.debug(u"Tautulli WebSocket :: Leaving thread.")
|
logger.debug(u"Tautulli WebSocket :: Leaving thread.")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue