From 0022b6978a461d78de1979e0bab55ad01898bf46 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Wed, 21 Mar 2018 18:03:01 -0400 Subject: [PATCH] Force update checks on startup (#595) --- CTFd/__init__.py | 2 +- CTFd/utils.py | 59 +++++++++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/CTFd/__init__.py b/CTFd/__init__.py index 264ca35b..0e959d0d 100644 --- a/CTFd/__init__.py +++ b/CTFd/__init__.py @@ -139,7 +139,7 @@ def create_app(config='CTFd.config.Config'): cache.init_app(app) app.cache = cache - update_check() + update_check(force=True) version = utils.get_config('ctf_version') diff --git a/CTFd/utils.py b/CTFd/utils.py index d2201178..39e50c89 100644 --- a/CTFd/utils.py +++ b/CTFd/utils.py @@ -730,35 +730,42 @@ def base64decode(s, urldecode=False): def update_check(force=False): - update = app.config.get('UPDATE_CHECK') or force + # If UPDATE_CHECK is disabled don't check for updates at all. + if app.config.get('UPDATE_CHECK') is False: + return + + # Get when we should check for updates next. + next_update_check = get_config('next_update_check') or 0 + + # If we have passed our saved time or we are forcing we should check. + update = (next_update_check < time.time()) or force + if update: - next_update_check = get_config('next_update_check') or 0 - if (next_update_check < time.time()) or force: + try: + params = { + 'current': app.VERSION + } + check = requests.get( + 'https://versioning.ctfd.io/versions/latest', + params=params, + timeout=0.1 + ).json() + except requests.exceptions.RequestException as e: + pass + else: try: - params = { - 'current': app.VERSION - } - check = requests.get( - 'https://versioning.ctfd.io/versions/latest', - params=params, - timeout=0.1 - ).json() - except requests.exceptions.RequestException as e: - pass - else: - try: - latest = check['resource']['tag'] - html_url = check['resource']['html_url'] - if StrictVersion(latest) > StrictVersion(app.VERSION): - set_config('version_latest', html_url) - elif StrictVersion(latest) <= StrictVersion(app.VERSION): - set_config('version_latest', None) - except KeyError: + latest = check['resource']['tag'] + html_url = check['resource']['html_url'] + if StrictVersion(latest) > StrictVersion(app.VERSION): + set_config('version_latest', html_url) + elif StrictVersion(latest) <= StrictVersion(app.VERSION): set_config('version_latest', None) - finally: - # 12 hours later - next_update_check_time = int(time.time() + 43200) - set_config('next_update_check', next_update_check_time) + except KeyError: + set_config('version_latest', None) + finally: + # 12 hours later + next_update_check_time = int(time.time() + 43200) + set_config('next_update_check', next_update_check_time) else: set_config('version_latest', None)