add version_check background task

This commit is contained in:
simon 2022-12-21 14:24:24 +07:00
parent fff389359a
commit 5244cddeb3
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
4 changed files with 81 additions and 3 deletions

View File

@ -8,6 +8,7 @@ from home.src.es.connect import ElasticWrap
from home.src.es.index_setup import ElasitIndexWrap from home.src.es.index_setup import ElasitIndexWrap
from home.src.es.snapshot import ElasticSnapshot from home.src.es.snapshot import ElasticSnapshot
from home.src.ta.config import AppConfig as ArchivistConfig 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.helper import clear_dl_cache
from home.src.ta.ta_redis import RedisArchivist from home.src.ta.ta_redis import RedisArchivist
@ -34,6 +35,7 @@ class StartupCheck:
self.make_folders() self.make_folders()
clear_dl_cache(self.config_handler.config) clear_dl_cache(self.config_handler.config)
self.snapshot_check() self.snapshot_check()
self.ta_version_check()
self.set_has_run() self.set_has_run()
def get_has_run(self): def get_has_run(self):
@ -120,6 +122,10 @@ class StartupCheck:
print("elasticsearch version check passed") print("elasticsearch version check passed")
def ta_version_check(self):
"""remove key if updated now"""
ReleaseVersion().is_updated()
class HomeConfig(AppConfig): class HomeConfig(AppConfig):
"""call startup funcs""" """call startup funcs"""

View File

@ -49,6 +49,7 @@
"check_reindex_days": 90, "check_reindex_days": 90,
"thumbnail_check": {"minute": "0", "hour": "17", "day_of_week": "*"}, "thumbnail_check": {"minute": "0", "hour": "17", "day_of_week": "*"},
"run_backup": {"minute": "0", "hour": "8", "day_of_week": "0"}, "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": "*"}
} }
} }

View File

@ -8,7 +8,9 @@ import json
import os import os
import re import re
import requests
from celery.schedules import crontab from celery.schedules import crontab
from django.conf import settings
from home.src.ta.ta_redis import RedisArchivist from home.src.ta.ta_redis import RedisArchivist
@ -268,3 +270,66 @@ class ScheduleBuilder:
schedule_dict.update(to_add) schedule_dict.update(to_add)
return schedule_dict 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)

View File

@ -22,7 +22,7 @@ from home.src.es.index_setup import ElasitIndexWrap
from home.src.index.channel import YoutubeChannel from home.src.index.channel import YoutubeChannel
from home.src.index.filesystem import ImportFolderScanner, scan_filesystem from home.src.index.filesystem import ImportFolderScanner, scan_filesystem
from home.src.index.reindex import Reindex, ReindexManual, ReindexOutdated 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.helper import UrlListParser, clear_dl_cache
from home.src.ta.ta_redis import RedisArchivist, RedisQueue from home.src.ta.ta_redis import RedisArchivist, RedisQueue
@ -290,9 +290,15 @@ def index_channel_playlists(channel_id):
channel.index_channel_playlists() channel.index_channel_playlists()
@shared_task(name="version_check")
def version_check():
"""check for new updates"""
ReleaseVersion().check()
try: try:
app.conf.beat_schedule = ScheduleBuilder().build_schedule() app.conf.beat_schedule = ScheduleBuilder().build_schedule()
except KeyError: 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() StartupCheck().sync_redis_state()
app.conf.beat_schedule = ScheduleBuilder().build_schedule() app.conf.beat_schedule = ScheduleBuilder().build_schedule()