From 5244cddeb3b929c7c90134d6db72d1da69e338c6 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 21 Dec 2022 14:24:24 +0700 Subject: [PATCH] add version_check background task --- tubearchivist/home/apps.py | 6 +++ tubearchivist/home/config.json | 3 +- tubearchivist/home/src/ta/config.py | 65 +++++++++++++++++++++++++++++ tubearchivist/home/tasks.py | 10 ++++- 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/tubearchivist/home/apps.py b/tubearchivist/home/apps.py index 657c0e2..7a70d6e 100644 --- a/tubearchivist/home/apps.py +++ b/tubearchivist/home/apps.py @@ -8,6 +8,7 @@ from home.src.es.connect import ElasticWrap from home.src.es.index_setup import ElasitIndexWrap from home.src.es.snapshot import ElasticSnapshot from home.src.ta.config import AppConfig as ArchivistConfig +from home.src.ta.config import ReleaseVersion from home.src.ta.helper import clear_dl_cache from home.src.ta.ta_redis import RedisArchivist @@ -34,6 +35,7 @@ class StartupCheck: self.make_folders() clear_dl_cache(self.config_handler.config) self.snapshot_check() + self.ta_version_check() self.set_has_run() def get_has_run(self): @@ -120,6 +122,10 @@ class StartupCheck: print("elasticsearch version check passed") + def ta_version_check(self): + """remove key if updated now""" + ReleaseVersion().is_updated() + class HomeConfig(AppConfig): """call startup funcs""" diff --git a/tubearchivist/home/config.json b/tubearchivist/home/config.json index c8450d0..19539f8 100644 --- a/tubearchivist/home/config.json +++ b/tubearchivist/home/config.json @@ -49,6 +49,7 @@ "check_reindex_days": 90, "thumbnail_check": {"minute": "0", "hour": "17", "day_of_week": "*"}, "run_backup": {"minute": "0", "hour": "8", "day_of_week": "0"}, - "run_backup_rotate": 5 + "run_backup_rotate": 5, + "version_check": {"minute": "0", "hour": "11", "day_of_week": "*"} } } diff --git a/tubearchivist/home/src/ta/config.py b/tubearchivist/home/src/ta/config.py index e6d4eb3..27e0cf1 100644 --- a/tubearchivist/home/src/ta/config.py +++ b/tubearchivist/home/src/ta/config.py @@ -8,7 +8,9 @@ import json import os import re +import requests from celery.schedules import crontab +from django.conf import settings from home.src.ta.ta_redis import RedisArchivist @@ -268,3 +270,66 @@ class ScheduleBuilder: schedule_dict.update(to_add) return schedule_dict + + +class ReleaseVersion: + """compare local version with remote version""" + + REMOTE_URL = "https://www.tubearchivist.com/api/release/latest/" + NEW_KEY = "versioncheck:new" + + def __init__(self): + self.local_version = self._parse_version(settings.TA_VERSION) + self.is_unstable = settings.TA_VERSION.endswith("-unstable") + self.remote_version = False + self.is_breaking = False + self.response = False + + def check(self): + """check version""" + print(f"[{self.local_version}]: look for updates") + self.get_remote_version() + new_version, is_breaking = self._has_update() + if new_version: + message = { + "status": True, + "version": new_version, + "is_breaking": is_breaking, + } + RedisArchivist().set_message(self.NEW_KEY, message) + print(f"[{self.local_version}]: found new version {new_version}") + + def get_remote_version(self): + """read version from remote""" + self.response = requests.get(self.REMOTE_URL, timeout=20).json() + remote_version_str = self.response["release_version"] + self.remote_version = self._parse_version(remote_version_str) + self.is_breaking = self.response["breaking_changes"] + + def _has_update(self): + """check if there is an update""" + for idx, number in enumerate(self.local_version): + is_newer = self.remote_version[idx] > number + if is_newer: + return self.response["release_version"], self.is_breaking + + if self.is_unstable and self.local_version == self.remote_version: + return self.response["release_version"], self.is_breaking + + return False, False + + @staticmethod + def _parse_version(version): + """return version parts""" + clean = version.rstrip("-unstable").lstrip("v") + return tuple((int(i) for i in clean.split("."))) + + def is_updated(self): + """check if update happened in the mean time""" + message = RedisArchivist().get_message(self.NEW_KEY) + if not message.get("status"): + return + + if self._parse_version(message.get("version")) == self.local_version: + print(f"[{self.local_version}]: update completed") + RedisArchivist().del_message(self.NEW_KEY) diff --git a/tubearchivist/home/tasks.py b/tubearchivist/home/tasks.py index ee830e9..f638350 100644 --- a/tubearchivist/home/tasks.py +++ b/tubearchivist/home/tasks.py @@ -22,7 +22,7 @@ from home.src.es.index_setup import ElasitIndexWrap from home.src.index.channel import YoutubeChannel from home.src.index.filesystem import ImportFolderScanner, scan_filesystem from home.src.index.reindex import Reindex, ReindexManual, ReindexOutdated -from home.src.ta.config import AppConfig, ScheduleBuilder +from home.src.ta.config import AppConfig, ReleaseVersion, ScheduleBuilder from home.src.ta.helper import UrlListParser, clear_dl_cache from home.src.ta.ta_redis import RedisArchivist, RedisQueue @@ -290,9 +290,15 @@ def index_channel_playlists(channel_id): channel.index_channel_playlists() +@shared_task(name="version_check") +def version_check(): + """check for new updates""" + ReleaseVersion().check() + + try: app.conf.beat_schedule = ScheduleBuilder().build_schedule() except KeyError: - # update path from v0.0.8 to v0.0.9 to load new defaults + # update path to load new defaults StartupCheck().sync_redis_state() app.conf.beat_schedule = ScheduleBuilder().build_schedule()