mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-07 21:51:14 -07:00
Combine connection function for cloud and non-cloud servers
This commit is contained in:
parent
a53afe05a2
commit
e0109ed179
5 changed files with 45 additions and 62 deletions
|
@ -33,7 +33,7 @@ import signal
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import plexpy
|
import plexpy
|
||||||
from plexpy import config, database, logger, web_socket, webstart
|
from plexpy import config, database, logger, webstart
|
||||||
|
|
||||||
|
|
||||||
# Register signals, such as CTRL + C
|
# Register signals, such as CTRL + C
|
||||||
|
@ -194,13 +194,6 @@ def main():
|
||||||
# Start the background threads
|
# Start the background threads
|
||||||
plexpy.start()
|
plexpy.start()
|
||||||
|
|
||||||
# Open connection for websocket
|
|
||||||
try:
|
|
||||||
web_socket.start_thread(startup=True)
|
|
||||||
except:
|
|
||||||
logger.warn(u"Websocket :: Unable to open connection.")
|
|
||||||
plexpy.initialize_scheduler()
|
|
||||||
|
|
||||||
# Force the http port if neccessary
|
# Force the http port if neccessary
|
||||||
if args.port:
|
if args.port:
|
||||||
http_port = args.port
|
http_port = args.port
|
||||||
|
|
|
@ -397,12 +397,8 @@ def initialize_scheduler():
|
||||||
schedule_job(libraries.refresh_libraries, 'Refresh libraries list',
|
schedule_job(libraries.refresh_libraries, 'Refresh libraries list',
|
||||||
hours=library_hours, minutes=0, seconds=0)
|
hours=library_hours, minutes=0, seconds=0)
|
||||||
|
|
||||||
if plexpy.CONFIG.PMS_IS_CLOUD:
|
schedule_job(activity_pinger.connect_server, 'Check for server response',
|
||||||
schedule_job(activity_pinger.check_cloud_status, 'Check for server response',
|
hours=0, minutes=0, seconds=0)
|
||||||
hours=0, minutes=0, seconds=0)
|
|
||||||
else:
|
|
||||||
schedule_job(activity_pinger.check_server_response, 'Check for server response',
|
|
||||||
hours=0, minutes=0, seconds=0)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Cancel all jobs
|
# Cancel all jobs
|
||||||
|
@ -419,15 +415,9 @@ def initialize_scheduler():
|
||||||
schedule_job(libraries.refresh_libraries, 'Refresh libraries list',
|
schedule_job(libraries.refresh_libraries, 'Refresh libraries list',
|
||||||
hours=0, minutes=0, seconds=0)
|
hours=0, minutes=0, seconds=0)
|
||||||
|
|
||||||
if plexpy.CONFIG.PMS_IS_CLOUD:
|
# Schedule job to reconnect server
|
||||||
# Schedule job to check for cloud server status
|
schedule_job(activity_pinger.connect_server, 'Check for server response',
|
||||||
schedule_job(activity_pinger.check_cloud_status, 'Check for server response',
|
hours=0, minutes=0, seconds=60, args=(False,))
|
||||||
hours=0, minutes=0, seconds=60)
|
|
||||||
|
|
||||||
else:
|
|
||||||
# Schedule job to reconnect websocket
|
|
||||||
schedule_job(activity_pinger.check_server_response, 'Check for server response',
|
|
||||||
hours=0, minutes=0, seconds=60)
|
|
||||||
|
|
||||||
# Start scheduler
|
# Start scheduler
|
||||||
if start_jobs and len(SCHED.get_jobs()):
|
if start_jobs and len(SCHED.get_jobs()):
|
||||||
|
@ -471,6 +461,8 @@ def start():
|
||||||
notification_handler.start_threads(num_threads=CONFIG.NOTIFICATION_THREADS)
|
notification_handler.start_threads(num_threads=CONFIG.NOTIFICATION_THREADS)
|
||||||
notifiers.check_browser_enabled()
|
notifiers.check_browser_enabled()
|
||||||
|
|
||||||
|
activity_pinger.connect_server(log=True, startup=True)
|
||||||
|
|
||||||
_STARTED = True
|
_STARTED = True
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -264,12 +264,33 @@ def check_recently_added():
|
||||||
plexpy.NOTIFY_QUEUE.put({'timeline_data': item.copy(), 'notify_action': 'on_created'})
|
plexpy.NOTIFY_QUEUE.put({'timeline_data': item.copy(), 'notify_action': 'on_created'})
|
||||||
|
|
||||||
|
|
||||||
def check_server_response():
|
def connect_server(log=True, startup=False):
|
||||||
logger.info(u"Tautulli Monitor :: Attempting to reconnect Plex server...")
|
if plexpy.CONFIG.PMS_IS_CLOUD:
|
||||||
try:
|
if log:
|
||||||
web_socket.start_thread()
|
logger.info(u"Tautulli Monitor :: Checking for Plex Cloud server status...")
|
||||||
except:
|
|
||||||
logger.warn(u"Websocket :: Unable to open connection.")
|
plex_tv = plextv.PlexTV()
|
||||||
|
status = plex_tv.get_cloud_server_status()
|
||||||
|
|
||||||
|
if status:
|
||||||
|
logger.info(u"Tautulli Monitor :: Plex Cloud server is active.")
|
||||||
|
else:
|
||||||
|
if log:
|
||||||
|
logger.info(u"Tautulli Monitor :: Plex Cloud server is sleeping.")
|
||||||
|
if startup:
|
||||||
|
web_socket.on_disconnect()
|
||||||
|
|
||||||
|
else:
|
||||||
|
status = True
|
||||||
|
|
||||||
|
if status:
|
||||||
|
if log and not startup:
|
||||||
|
logger.info(u"Tautulli Monitor :: Attempting to reconnect Plex server...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
web_socket.start_thread()
|
||||||
|
except:
|
||||||
|
logger.error(u"Websocket :: Unable to open connection.")
|
||||||
|
|
||||||
|
|
||||||
def check_server_access():
|
def check_server_access():
|
||||||
|
@ -326,22 +347,3 @@ def check_server_updates():
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.info(u"Tautulli Monitor :: No PMS update available.")
|
logger.info(u"Tautulli Monitor :: No PMS update available.")
|
||||||
|
|
||||||
|
|
||||||
def check_cloud_status(log=False, return_status=False):
|
|
||||||
if log:
|
|
||||||
logger.info(u"Tautulli Monitor :: Checking for Plex Cloud server status...")
|
|
||||||
|
|
||||||
plex_tv = plextv.PlexTV()
|
|
||||||
cloud_status = plex_tv.get_cloud_server_status()
|
|
||||||
|
|
||||||
if cloud_status:
|
|
||||||
logger.info(u"Tautulli Monitor :: Plex Cloud server is active.")
|
|
||||||
if return_status:
|
|
||||||
return cloud_status
|
|
||||||
check_server_response()
|
|
||||||
else:
|
|
||||||
if log:
|
|
||||||
logger.info(u"Tautulli Monitor :: Plex Cloud server is sleeping.")
|
|
||||||
if return_status:
|
|
||||||
return cloud_status
|
|
||||||
|
|
|
@ -32,13 +32,7 @@ opcode_data = (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY)
|
||||||
ws_reconnect = False
|
ws_reconnect = False
|
||||||
|
|
||||||
|
|
||||||
def start_thread(startup=False):
|
def start_thread():
|
||||||
if startup and plexpy.CONFIG.PMS_IS_CLOUD and plexpy.PLEX_SERVER_UP is None:
|
|
||||||
plexpy.PLEX_SERVER_UP = activity_pinger.check_cloud_status(log=True, return_status=True)
|
|
||||||
if not plexpy.PLEX_SERVER_UP:
|
|
||||||
on_disconnect()
|
|
||||||
return
|
|
||||||
|
|
||||||
if plexpy.CONFIG.FIRST_RUN_COMPLETE:
|
if plexpy.CONFIG.FIRST_RUN_COMPLETE:
|
||||||
# Check for any existing sessions on start up
|
# Check for any existing sessions on start up
|
||||||
activity_pinger.check_active_sessions(ws_request=True)
|
activity_pinger.check_active_sessions(ws_request=True)
|
||||||
|
@ -81,7 +75,7 @@ def run():
|
||||||
|
|
||||||
if plexpy.CONFIG.PMS_SSL and plexpy.CONFIG.PMS_URL[:5] == 'https':
|
if plexpy.CONFIG.PMS_SSL and plexpy.CONFIG.PMS_URL[:5] == 'https':
|
||||||
uri = plexpy.CONFIG.PMS_URL.replace('https://', 'wss://') + '/:/websockets/notifications'
|
uri = plexpy.CONFIG.PMS_URL.replace('https://', 'wss://') + '/:/websockets/notifications'
|
||||||
secure = ' secure'
|
secure = 'secure '
|
||||||
else:
|
else:
|
||||||
uri = 'ws://%s:%s/:/websockets/notifications' % (
|
uri = 'ws://%s:%s/:/websockets/notifications' % (
|
||||||
plexpy.CONFIG.PMS_IP,
|
plexpy.CONFIG.PMS_IP,
|
||||||
|
@ -101,13 +95,16 @@ def run():
|
||||||
|
|
||||||
# Try an open the websocket connection
|
# Try an open the websocket connection
|
||||||
while not plexpy.WS_CONNECTED and reconnects < plexpy.CONFIG.WEBSOCKET_CONNECTION_ATTEMPTS:
|
while not plexpy.WS_CONNECTED and reconnects < plexpy.CONFIG.WEBSOCKET_CONNECTION_ATTEMPTS:
|
||||||
|
if reconnects == 0:
|
||||||
|
logger.info(u"Tautulli WebSocket :: Opening %swebsocket." % secure)
|
||||||
|
|
||||||
reconnects += 1
|
reconnects += 1
|
||||||
|
|
||||||
# Sleep 5 between connection attempts
|
# Sleep 5 between connection attempts
|
||||||
if reconnects > 1:
|
if reconnects > 1:
|
||||||
time.sleep(plexpy.CONFIG.WEBSOCKET_CONNECTION_TIMEOUT)
|
time.sleep(plexpy.CONFIG.WEBSOCKET_CONNECTION_TIMEOUT)
|
||||||
|
|
||||||
logger.info(u"Tautulli WebSocket :: Opening%s websocket, connection attempt %s." % (secure, str(reconnects)))
|
logger.info(u"Tautulli WebSocket :: Connection attempt %s." % str(reconnects))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ws = create_connection(uri, header=header)
|
ws = create_connection(uri, header=header)
|
||||||
|
@ -127,9 +124,8 @@ def run():
|
||||||
reconnects = 0
|
reconnects = 0
|
||||||
|
|
||||||
except websocket.WebSocketConnectionClosedException:
|
except websocket.WebSocketConnectionClosedException:
|
||||||
if plexpy.CONFIG.PMS_IS_CLOUD:
|
if reconnects == 0:
|
||||||
logger.warn(u"Tautulli WebSocket :: Connection has closed.")
|
logger.warn(u"Tautulli WebSocket :: Connection has closed.")
|
||||||
activity_pinger.check_cloud_status(log=True)
|
|
||||||
|
|
||||||
if not plexpy.CONFIG.PMS_IS_CLOUD and reconnects < plexpy.CONFIG.WEBSOCKET_CONNECTION_ATTEMPTS:
|
if not plexpy.CONFIG.PMS_IS_CLOUD and reconnects < plexpy.CONFIG.WEBSOCKET_CONNECTION_ATTEMPTS:
|
||||||
reconnects += 1
|
reconnects += 1
|
||||||
|
@ -138,7 +134,7 @@ def run():
|
||||||
if reconnects > 1:
|
if reconnects > 1:
|
||||||
time.sleep(plexpy.CONFIG.WEBSOCKET_CONNECTION_TIMEOUT)
|
time.sleep(plexpy.CONFIG.WEBSOCKET_CONNECTION_TIMEOUT)
|
||||||
|
|
||||||
logger.warn(u"Tautulli WebSocket :: Connection has closed, reconnection attempt %s." % str(reconnects))
|
logger.warn(u"Tautulli WebSocket :: Reconnection attempt %s." % str(reconnects))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ws = create_connection(uri, header=header)
|
ws = create_connection(uri, header=header)
|
||||||
|
@ -166,7 +162,6 @@ def run():
|
||||||
start_thread()
|
start_thread()
|
||||||
|
|
||||||
if not plexpy.WS_CONNECTED and not ws_reconnect:
|
if not plexpy.WS_CONNECTED and not ws_reconnect:
|
||||||
logger.error(u"Tautulli WebSocket :: Connection unavailable.")
|
|
||||||
on_disconnect()
|
on_disconnect()
|
||||||
|
|
||||||
logger.debug(u"Tautulli WebSocket :: Leaving thread.")
|
logger.debug(u"Tautulli WebSocket :: Leaving thread.")
|
||||||
|
|
|
@ -28,6 +28,7 @@ from mako.lookup import TemplateLookup
|
||||||
from mako import exceptions
|
from mako import exceptions
|
||||||
|
|
||||||
import plexpy
|
import plexpy
|
||||||
|
import activity_pinger
|
||||||
import common
|
import common
|
||||||
import config
|
import config
|
||||||
import database
|
import database
|
||||||
|
@ -2747,7 +2748,7 @@ class WebInterface(object):
|
||||||
|
|
||||||
# If first run, start websocket
|
# If first run, start websocket
|
||||||
if first_run:
|
if first_run:
|
||||||
web_socket.start_thread()
|
activity_pinger.connect_server()
|
||||||
|
|
||||||
# Reconfigure scheduler if intervals changed
|
# Reconfigure scheduler if intervals changed
|
||||||
if reschedule:
|
if reschedule:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue