Improvements for "check for updates" task

- Let it throw an error if something fails
- Errors are caught as "unsuccessful tasks"
This commit is contained in:
Oliver Walters 2021-03-22 11:20:09 +11:00
parent b7718d9c6c
commit 8fd666e662
2 changed files with 11 additions and 12 deletions

View File

@ -29,7 +29,7 @@ class InvenTreeConfig(AppConfig):
InvenTree.tasks.schedule_task( InvenTree.tasks.schedule_task(
'InvenTree.tasks.delete_successful_tasks', 'InvenTree.tasks.delete_successful_tasks',
schedule_type=Schedule.WEEKLY, schedule_type=Schedule.DAILY,
) )
InvenTree.tasks.schedule_task( InvenTree.tasks.schedule_task(

View File

@ -21,6 +21,9 @@ def schedule_task(taskname, **kwargs):
If the task has already been scheduled, ignore! If the task has already been scheduled, ignore!
""" """
# If unspecified, repeat indefinitely
repeats = kwargs.pop('repeats', -1)
try: try:
from django_q.models import Schedule from django_q.models import Schedule
except (AppRegistryNotReady): except (AppRegistryNotReady):
@ -34,7 +37,9 @@ def schedule_task(taskname, **kwargs):
logger.info(f"Creating scheduled task '{taskname}'") logger.info(f"Creating scheduled task '{taskname}'")
Schedule.objects.create( Schedule.objects.create(
name=taskname,
func=taskname, func=taskname,
repeats=repeats,
**kwargs **kwargs
) )
except (OperationalError, ProgrammingError): except (OperationalError, ProgrammingError):
@ -98,21 +103,20 @@ def check_for_updates():
try: try:
import common.models import common.models
except AppRegistryNotReady: except AppRegistryNotReady:
# Apps not yet loaded!
return return
response = requests.get('https://api.github.com/repos/inventree/inventree/releases/latest') response = requests.get('https://api.github.com/repos/inventree/inventree/releases/latest')
if not response.status_code == 200: if not response.status_code == 200:
logger.warning(f'Unexpected status code from GitHub API: {response.status_code}') raise ValueError(f'Unexpected status code from GitHub API: {response.status_code}')
return
data = json.loads(response.text) data = json.loads(response.text)
tag = data.get('tag_name', None) tag = data.get('tag_name', None)
if not tag: if not tag:
logger.warning("'tag_name' missing from GitHub response") raise ValueError("'tag_name' missing from GitHub response")
return
match = re.match(r"^.*(\d+)\.(\d+)\.(\d+).*$", tag) 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") logger.warning(f"Version '{tag}' did not match expected pattern")
return return
try:
latest_version = [int(x) for x in match.groups()] latest_version = [int(x) for x in match.groups()]
except (ValueError):
logger.warning(f"Version '{tag}' not integer format")
return
if not len(latest_version) == 3: if not len(latest_version) == 3:
logger.warning(f"Version '{tag}' is not correct format") raise ValueError(f"Version '{tag}' is not correct format")
return
logger.info(f"Latest InvenTree version: '{tag}'") logger.info(f"Latest InvenTree version: '{tag}'")