2020-09-26 00:16:35 +00:00
|
|
|
"""passbook admin tasks"""
|
|
|
|
from django.core.cache import cache
|
|
|
|
from requests import RequestException, get
|
|
|
|
from structlog import get_logger
|
|
|
|
|
2020-10-16 09:28:54 +00:00
|
|
|
from passbook.lib.tasks import MonitoredTask, TaskResult, TaskResultStatus
|
2020-09-26 00:16:35 +00:00
|
|
|
from passbook.root.celery import CELERY_APP
|
|
|
|
|
|
|
|
LOGGER = get_logger()
|
|
|
|
VERSION_CACHE_KEY = "passbook_latest_version"
|
|
|
|
VERSION_CACHE_TIMEOUT = 2 * 60 * 60 # 2 hours
|
|
|
|
|
|
|
|
|
2020-10-16 09:28:54 +00:00
|
|
|
@CELERY_APP.task(bind=True, base=MonitoredTask)
|
|
|
|
def update_latest_version(self: MonitoredTask):
|
2020-09-26 00:16:35 +00:00
|
|
|
"""Update latest version info"""
|
|
|
|
try:
|
|
|
|
data = get(
|
|
|
|
"https://api.github.com/repos/beryju/passbook/releases/latest"
|
|
|
|
).json()
|
|
|
|
tag_name = data.get("tag_name")
|
|
|
|
cache.set(VERSION_CACHE_KEY, tag_name.split("/")[1], VERSION_CACHE_TIMEOUT)
|
2020-10-16 09:28:54 +00:00
|
|
|
self.set_status(
|
|
|
|
TaskResult(
|
|
|
|
TaskResultStatus.SUCCESSFUL, ["Successfully updated latest Version"]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except (RequestException, IndexError) as exc:
|
2020-09-26 00:16:35 +00:00
|
|
|
cache.set(VERSION_CACHE_KEY, "0.0.0", VERSION_CACHE_TIMEOUT)
|
2020-10-16 14:00:24 +00:00
|
|
|
self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc))
|