From 8fd666e6629653fe3675708d2cc2e5c122dc3c0d Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 22 Mar 2021 11:20:09 +1100 Subject: [PATCH] Improvements for "check for updates" task - Let it throw an error if something fails - Errors are caught as "unsuccessful tasks" --- InvenTree/InvenTree/apps.py | 2 +- InvenTree/InvenTree/tasks.py | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/InvenTree/InvenTree/apps.py b/InvenTree/InvenTree/apps.py index 1022a75301..6a2e363789 100644 --- a/InvenTree/InvenTree/apps.py +++ b/InvenTree/InvenTree/apps.py @@ -29,7 +29,7 @@ class InvenTreeConfig(AppConfig): InvenTree.tasks.schedule_task( 'InvenTree.tasks.delete_successful_tasks', - schedule_type=Schedule.WEEKLY, + schedule_type=Schedule.DAILY, ) InvenTree.tasks.schedule_task( diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index 4ba34ca7ba..eba145c660 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -21,6 +21,9 @@ def schedule_task(taskname, **kwargs): If the task has already been scheduled, ignore! """ + # If unspecified, repeat indefinitely + repeats = kwargs.pop('repeats', -1) + try: from django_q.models import Schedule except (AppRegistryNotReady): @@ -34,7 +37,9 @@ def schedule_task(taskname, **kwargs): logger.info(f"Creating scheduled task '{taskname}'") Schedule.objects.create( + name=taskname, func=taskname, + repeats=repeats, **kwargs ) except (OperationalError, ProgrammingError): @@ -98,21 +103,20 @@ def check_for_updates(): try: import common.models except AppRegistryNotReady: + # Apps not yet loaded! return response = requests.get('https://api.github.com/repos/inventree/inventree/releases/latest') if not response.status_code == 200: - logger.warning(f'Unexpected status code from GitHub API: {response.status_code}') - return + raise ValueError(f'Unexpected status code from GitHub API: {response.status_code}') data = json.loads(response.text) tag = data.get('tag_name', None) if not tag: - logger.warning("'tag_name' missing from GitHub response") - return + raise ValueError("'tag_name' missing from GitHub response") match = re.match(r"^.*(\d+)\.(\d+)\.(\d+).*$", tag) @@ -120,15 +124,10 @@ def check_for_updates(): logger.warning(f"Version '{tag}' did not match expected pattern") return - try: - latest_version = [int(x) for x in match.groups()] - except (ValueError): - logger.warning(f"Version '{tag}' not integer format") - return + latest_version = [int(x) for x in match.groups()] if not len(latest_version) == 3: - logger.warning(f"Version '{tag}' is not correct format") - return + raise ValueError(f"Version '{tag}' is not correct format") logger.info(f"Latest InvenTree version: '{tag}'")