backend.workers.check_updates
1import packaging.version 2import requests 3import json 4 5from common.config_manager import config 6from common.lib.helpers import add_notification, get_github_version 7from backend.lib.worker import BasicWorker 8from pathlib import Path 9 10 11class UpdateChecker(BasicWorker): 12 """ 13 Check for updates 14 15 Checks the configured Github repository (if any) for the latest packaged 16 release. If the tag of that release is newer than the current version (per 17 the .current-version file), a notification is shown to 4CAT admins in the 18 web interface. Once the current version is updated the notification is 19 automatically removed. 20 """ 21 type = "check-for-updates" 22 max_workers = 1 23 24 # check once every three hours 25 ensure_job = {"remote_id": "", "interval": 10800} 26 27 def work(self): 28 versionfile = Path(config.get("PATH_ROOT"), "config/.current-version") 29 repo_url = config.get("4cat.github_url") 30 31 if not versionfile.exists() or not repo_url: 32 # need something to compare against... 33 return 34 35 timeout = 15 36 try: 37 (latest_tag, release_url) = get_github_version(timeout) 38 if latest_tag == "unknown": 39 raise ValueError() 40 except ValueError: 41 self.log.warning("'4cat.github_url' may be misconfigured - repository does not exist or is private") 42 return 43 except requests.Timeout: 44 self.log.warning(f"GitHub URL '4cat.github_url' did not respond within {timeout} seconds - not checking for new version") 45 return 46 except (requests.RequestException, json.JSONDecodeError): 47 # some issue with the data, or the GitHub API, but not something we 48 # can fix from this end, so just silently fail 49 return 50 51 with versionfile.open() as infile: 52 current_version = infile.readline().strip() 53 54 if packaging.version.parse(latest_tag) > packaging.version.parse(current_version): 55 # update available! 56 # show a notification for all admins (normal users can't update 57 # after all) 58 add_notification(self.db, "!admin", 59 "A new version of 4CAT is [available](%s). The latest version is %s; you are running version %s." % ( 60 release_url, latest_tag, current_version 61 ), allow_dismiss=True) 62 63 else: 64 # up to date? dismiss any notifications about new versions 65 self.db.execute("DELETE FROM users_notifications WHERE username = '!admin' " 66 "AND notification LIKE 'A new version of 4CAT%'")
12class UpdateChecker(BasicWorker): 13 """ 14 Check for updates 15 16 Checks the configured Github repository (if any) for the latest packaged 17 release. If the tag of that release is newer than the current version (per 18 the .current-version file), a notification is shown to 4CAT admins in the 19 web interface. Once the current version is updated the notification is 20 automatically removed. 21 """ 22 type = "check-for-updates" 23 max_workers = 1 24 25 # check once every three hours 26 ensure_job = {"remote_id": "", "interval": 10800} 27 28 def work(self): 29 versionfile = Path(config.get("PATH_ROOT"), "config/.current-version") 30 repo_url = config.get("4cat.github_url") 31 32 if not versionfile.exists() or not repo_url: 33 # need something to compare against... 34 return 35 36 timeout = 15 37 try: 38 (latest_tag, release_url) = get_github_version(timeout) 39 if latest_tag == "unknown": 40 raise ValueError() 41 except ValueError: 42 self.log.warning("'4cat.github_url' may be misconfigured - repository does not exist or is private") 43 return 44 except requests.Timeout: 45 self.log.warning(f"GitHub URL '4cat.github_url' did not respond within {timeout} seconds - not checking for new version") 46 return 47 except (requests.RequestException, json.JSONDecodeError): 48 # some issue with the data, or the GitHub API, but not something we 49 # can fix from this end, so just silently fail 50 return 51 52 with versionfile.open() as infile: 53 current_version = infile.readline().strip() 54 55 if packaging.version.parse(latest_tag) > packaging.version.parse(current_version): 56 # update available! 57 # show a notification for all admins (normal users can't update 58 # after all) 59 add_notification(self.db, "!admin", 60 "A new version of 4CAT is [available](%s). The latest version is %s; you are running version %s." % ( 61 release_url, latest_tag, current_version 62 ), allow_dismiss=True) 63 64 else: 65 # up to date? dismiss any notifications about new versions 66 self.db.execute("DELETE FROM users_notifications WHERE username = '!admin' " 67 "AND notification LIKE 'A new version of 4CAT%'")
Check for updates
Checks the configured Github repository (if any) for the latest packaged release. If the tag of that release is newer than the current version (per the .current-version file), a notification is shown to 4CAT admins in the web interface. Once the current version is updated the notification is automatically removed.
def
work(self):
28 def work(self): 29 versionfile = Path(config.get("PATH_ROOT"), "config/.current-version") 30 repo_url = config.get("4cat.github_url") 31 32 if not versionfile.exists() or not repo_url: 33 # need something to compare against... 34 return 35 36 timeout = 15 37 try: 38 (latest_tag, release_url) = get_github_version(timeout) 39 if latest_tag == "unknown": 40 raise ValueError() 41 except ValueError: 42 self.log.warning("'4cat.github_url' may be misconfigured - repository does not exist or is private") 43 return 44 except requests.Timeout: 45 self.log.warning(f"GitHub URL '4cat.github_url' did not respond within {timeout} seconds - not checking for new version") 46 return 47 except (requests.RequestException, json.JSONDecodeError): 48 # some issue with the data, or the GitHub API, but not something we 49 # can fix from this end, so just silently fail 50 return 51 52 with versionfile.open() as infile: 53 current_version = infile.readline().strip() 54 55 if packaging.version.parse(latest_tag) > packaging.version.parse(current_version): 56 # update available! 57 # show a notification for all admins (normal users can't update 58 # after all) 59 add_notification(self.db, "!admin", 60 "A new version of 4CAT is [available](%s). The latest version is %s; you are running version %s." % ( 61 release_url, latest_tag, current_version 62 ), allow_dismiss=True) 63 64 else: 65 # up to date? dismiss any notifications about new versions 66 self.db.execute("DELETE FROM users_notifications WHERE username = '!admin' " 67 "AND notification LIKE 'A new version of 4CAT%'")
This is where the actual work happens
Whatever the worker is supposed to do, it should happen (or be initiated from) this method. By default it does nothing, descending classes should implement this method.