diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 876c5147..9809d18e 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -412,7 +412,7 @@ def dbcheck(): 'audio_channels INTEGER, transcode_protocol TEXT, transcode_container TEXT, ' 'transcode_video_codec TEXT, transcode_audio_codec TEXT, transcode_audio_channels INTEGER,' 'transcode_width INTEGER, transcode_height INTEGER, buffer_count INTEGER DEFAULT 0, ' - 'buffer_last_triggered INTEGER, last_paused INTEGER)' + 'buffer_last_triggered INTEGER, last_paused INTEGER, write_attempts INTEGER DEFAULT 0)' ) # session_history table :: This is a history table which logs essential stream details @@ -653,6 +653,15 @@ def dbcheck(): 'ALTER TABLE sessions ADD COLUMN transcode_key TEXT' ) + # Upgrade sessions table from earlier versions + try: + c_db.execute('SELECT write_attempts FROM sessions') + except sqlite3.OperationalError: + logger.debug(u"Altering database. Updating database table sessions.") + c_db.execute( + 'ALTER TABLE sessions ADD COLUMN write_attempts INTEGER DEFAULT 0' + ) + # Upgrade session_history table from earlier versions try: c_db.execute('SELECT reference_id FROM session_history') diff --git a/plexpy/activity_pinger.py b/plexpy/activity_pinger.py index 3206dd76..6a9ba8b1 100644 --- a/plexpy/activity_pinger.py +++ b/plexpy/activity_pinger.py @@ -206,8 +206,24 @@ def check_active_sessions(ws_request=False): monitor_db.action('DELETE FROM sessions WHERE session_key = ? AND rating_key = ?', [stream['session_key'], stream['rating_key']]) else: - logger.warn(u"PlexPy Monitor :: Failed to write sessionKey %s ratingKey %s to the database. " \ - "Will try again on the next pass." % (stream['session_key'], stream['rating_key'])) + stream['write_attempts'] += 1 + + if stream['write_attempts'] < plexpy.CONFIG.SESSION_DB_WRITE_ATTEMPTS: + logger.warn(u"PlexPy Monitor :: Failed to write sessionKey %s ratingKey %s to the database. " \ + "Will try again on the next pass. Write attempt %s." + % (stream['session_key'], stream['rating_key'], str(stream['write_attempts']))) + monitor_db.action('UPDATE sessions SET write_attempts = ? ' + 'WHERE session_key = ? AND rating_key = ?', + [stream['write_attempts'], stream['session_key'], stream['rating_key']]) + else: + logger.warn(u"PlexPy Monitor :: Failed to write sessionKey %s ratingKey %s to the database. " \ + "Removing session from the database. Write attempt %s." + % (stream['session_key'], stream['rating_key'], str(stream['write_attempts']))) + logger.debug(u"PlexPy Monitor :: Removing sessionKey %s ratingKey %s from session queue" + % (stream['session_key'], stream['rating_key'])) + monitor_db.action('DELETE FROM sessions WHERE session_key = ? AND rating_key = ?', + [stream['session_key'], stream['rating_key']]) + # Process the newly received session data for session in media_container: diff --git a/plexpy/config.py b/plexpy/config.py index bb03fdf3..d53e8554 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -374,6 +374,7 @@ _CONFIG_DEFINITIONS = { 'REFRESH_LIBRARIES_ON_STARTUP': (int, 'Monitoring', 1), 'REFRESH_USERS_INTERVAL': (int, 'Monitoring', 12), 'REFRESH_USERS_ON_STARTUP': (int, 'Monitoring', 1), + 'SESSION_DB_WRITE_ATTEMPTS': (int, 'Monitoring', 5), 'SLACK_ENABLED': (int, 'Slack', 0), 'SLACK_HOOK': (str, 'Slack', ''), 'SLACK_CHANNEL': (str, 'Slack', ''),