Cache GitHub update check on startup

* Fixes Tautulli/Tautulli-Issues#184
This commit is contained in:
JonnyWong16 2020-10-25 11:39:48 -07:00
commit 8157ee7811
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
3 changed files with 59 additions and 19 deletions

View file

@ -300,7 +300,7 @@ def initialize(config_file):
# Check for new versions # Check for new versions
if CONFIG.CHECK_GITHUB_ON_STARTUP and CONFIG.CHECK_GITHUB: if CONFIG.CHECK_GITHUB_ON_STARTUP and CONFIG.CHECK_GITHUB:
try: try:
versioncheck.check_update() versioncheck.check_update(use_cache=True)
except: except:
logger.exception("Unhandled exception") logger.exception("Unhandled exception")
LATEST_VERSION = CURRENT_VERSION LATEST_VERSION = CURRENT_VERSION

View file

@ -89,6 +89,7 @@ _CONFIG_DEFINITIONS = {
'CHECK_GITHUB': (int, 'General', 1), 'CHECK_GITHUB': (int, 'General', 1),
'CHECK_GITHUB_INTERVAL': (int, 'General', 360), 'CHECK_GITHUB_INTERVAL': (int, 'General', 360),
'CHECK_GITHUB_ON_STARTUP': (int, 'General', 1), 'CHECK_GITHUB_ON_STARTUP': (int, 'General', 1),
'CHECK_GITHUB_CACHE_SECONDS': (int, 'Advanced', 3600),
'CLEANUP_FILES': (int, 'General', 0), 'CLEANUP_FILES': (int, 'General', 0),
'CLOUDINARY_CLOUD_NAME': (str, 'Cloudinary', ''), 'CLOUDINARY_CLOUD_NAME': (str, 'Cloudinary', ''),
'CLOUDINARY_API_KEY': (str, 'Cloudinary', ''), 'CLOUDINARY_API_KEY': (str, 'Cloudinary', ''),

View file

@ -20,6 +20,7 @@ from __future__ import unicode_literals
from future.builtins import next from future.builtins import next
from future.builtins import str from future.builtins import str
import json
import os import os
import platform import platform
import re import re
@ -29,10 +30,12 @@ import tarfile
import plexpy import plexpy
if plexpy.PYTHON2: if plexpy.PYTHON2:
import common import common
import helpers
import logger import logger
import request import request
else: else:
from plexpy import common from plexpy import common
from plexpy import helpers
from plexpy import logger from plexpy import logger
from plexpy import request from plexpy import request
@ -154,8 +157,8 @@ def get_version_from_file():
return current_version, current_branch return current_version, current_branch
def check_update(scheduler=False, notify=False): def check_update(scheduler=False, notify=False, use_cache=False):
check_github(scheduler=scheduler, notify=notify) check_github(scheduler=scheduler, notify=notify, use_cache=use_cache)
if not plexpy.CURRENT_VERSION: if not plexpy.CURRENT_VERSION:
plexpy.UPDATE_AVAILABLE = None plexpy.UPDATE_AVAILABLE = None
@ -173,7 +176,7 @@ def check_update(scheduler=False, notify=False):
plexpy.MAC_SYS_TRAY_ICON.change_tray_update_icon() plexpy.MAC_SYS_TRAY_ICON.change_tray_update_icon()
def check_github(scheduler=False, notify=False): def check_github(scheduler=False, notify=False, use_cache=False):
plexpy.COMMITS_BEHIND = 0 plexpy.COMMITS_BEHIND = 0
if plexpy.CONFIG.GIT_TOKEN: if plexpy.CONFIG.GIT_TOKEN:
@ -181,12 +184,16 @@ def check_github(scheduler=False, notify=False):
else: else:
headers = {} headers = {}
# Get the latest version available from github version = github_cache('version', use_cache=use_cache)
logger.info('Retrieving latest version information from GitHub') if not version:
url = 'https://api.github.com/repos/%s/%s/commits/%s' % (plexpy.CONFIG.GIT_USER, # Get the latest version available from github
plexpy.CONFIG.GIT_REPO, logger.info('Retrieving latest version information from GitHub')
plexpy.CONFIG.GIT_BRANCH) url = 'https://api.github.com/repos/%s/%s/commits/%s' % (plexpy.CONFIG.GIT_USER,
version = request.request_json(url, headers=headers, timeout=20, validator=lambda x: type(x) == dict) plexpy.CONFIG.GIT_REPO,
plexpy.CONFIG.GIT_BRANCH)
version = request.request_json(url, headers=headers, timeout=20,
validator=lambda x: type(x) == dict)
github_cache('version', github_data=version)
if version is None: if version is None:
logger.warn('Could not get the latest version from GitHub. Are you running a local development version?') logger.warn('Could not get the latest version from GitHub. Are you running a local development version?')
@ -204,13 +211,16 @@ def check_github(scheduler=False, notify=False):
logger.info('Tautulli is up to date') logger.info('Tautulli is up to date')
return plexpy.LATEST_VERSION return plexpy.LATEST_VERSION
logger.info('Comparing currently installed version with latest GitHub version') commits = github_cache('commits', use_cache=use_cache)
url = 'https://api.github.com/repos/%s/%s/compare/%s...%s' % (plexpy.CONFIG.GIT_USER, if not commits:
plexpy.CONFIG.GIT_REPO, logger.info('Comparing currently installed version with latest GitHub version')
plexpy.LATEST_VERSION, url = 'https://api.github.com/repos/%s/%s/compare/%s...%s' % (plexpy.CONFIG.GIT_USER,
plexpy.CURRENT_VERSION) plexpy.CONFIG.GIT_REPO,
commits = request.request_json(url, headers=headers, timeout=20, whitelist_status_code=404, plexpy.LATEST_VERSION,
validator=lambda x: type(x) == dict) plexpy.CURRENT_VERSION)
commits = request.request_json(url, headers=headers, timeout=20, whitelist_status_code=404,
validator=lambda x: type(x) == dict)
github_cache('commits', github_data=commits)
if commits is None: if commits is None:
logger.warn('Could not get commits behind from GitHub.') logger.warn('Could not get commits behind from GitHub.')
@ -226,8 +236,13 @@ def check_github(scheduler=False, notify=False):
if plexpy.COMMITS_BEHIND > 0: if plexpy.COMMITS_BEHIND > 0:
logger.info('New version is available. You are %s commits behind' % plexpy.COMMITS_BEHIND) logger.info('New version is available. You are %s commits behind' % plexpy.COMMITS_BEHIND)
url = 'https://api.github.com/repos/%s/%s/releases' % (plexpy.CONFIG.GIT_USER, plexpy.CONFIG.GIT_REPO) releases = github_cache('releases', use_cache=use_cache)
releases = request.request_json(url, timeout=20, whitelist_status_code=404, validator=lambda x: type(x) == list) if not releases:
url = 'https://api.github.com/repos/%s/%s/releases' % (plexpy.CONFIG.GIT_USER,
plexpy.CONFIG.GIT_REPO)
releases = request.request_json(url, timeout=20, whitelist_status_code=404,
validator=lambda x: type(x) == list)
github_cache('releases', github_data=releases)
if releases is None: if releases is None:
logger.warn('Could not get releases from GitHub.') logger.warn('Could not get releases from GitHub.')
@ -391,6 +406,30 @@ def checkout_git_branch():
plexpy.CONFIG.GIT_BRANCH)) plexpy.CONFIG.GIT_BRANCH))
def github_cache(cache, github_data=None, use_cache=True):
timestamp = helpers.timestamp()
cache_filepath = os.path.join(plexpy.CONFIG.CACHE_DIR, 'github_{}.json'.format(cache))
if github_data:
cache_data = {'github_data': github_data, '_cache_time': timestamp}
try:
with open(cache_filepath, 'w', encoding='utf-8') as cache_file:
json.dump(cache_data, cache_file)
except:
pass
else:
if not use_cache:
return
try:
with open(cache_filepath, 'r', encoding='utf-8') as cache_file:
cache_data = json.load(cache_file)
if timestamp - cache_data['_cache_time'] < plexpy.CONFIG.CHECK_GITHUB_CACHE_SECONDS:
logger.debug('Using cached GitHub %s data', cache)
return cache_data['github_data']
except:
pass
def read_changelog(latest_only=False, since_prev_release=False): def read_changelog(latest_only=False, since_prev_release=False):
changelog_file = os.path.join(plexpy.PROG_DIR, 'CHANGELOG.md') changelog_file = os.path.join(plexpy.PROG_DIR, 'CHANGELOG.md')