lib: save task's call arguments for manual retry

This commit is contained in:
Jens Langhammer 2020-10-16 12:55:10 +02:00
parent fa504e4bf9
commit 7806cff96f
5 changed files with 53 additions and 9 deletions

View File

@ -1,11 +1,10 @@
"""passbook Tasks List""" """passbook Tasks List"""
from typing import Any, Dict from typing import Any, Dict
from django.core.cache import cache
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from passbook.admin.mixins import AdminRequiredMixin from passbook.admin.mixins import AdminRequiredMixin
from passbook.lib.tasks import TaskResultStatus from passbook.lib.tasks import TaskInfo, TaskResultStatus
class TaskListView(AdminRequiredMixin, TemplateView): class TaskListView(AdminRequiredMixin, TemplateView):
@ -15,7 +14,7 @@ class TaskListView(AdminRequiredMixin, TemplateView):
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]: def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
kwargs = super().get_context_data(**kwargs) kwargs = super().get_context_data(**kwargs)
kwargs["object_list"] = cache.get_many(cache.keys("task_*")) kwargs["object_list"] = TaskInfo.all()
kwargs["task_successful"] = TaskResultStatus.SUCCESSFUL kwargs["task_successful"] = TaskResultStatus.SUCCESSFUL
kwargs["task_warning"] = TaskResultStatus.WARNING kwargs["task_warning"] = TaskResultStatus.WARNING
kwargs["task_error"] = TaskResultStatus.ERROR kwargs["task_error"] = TaskResultStatus.ERROR

View File

@ -2,7 +2,7 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime from datetime import datetime
from enum import Enum from enum import Enum
from typing import List, Optional from typing import Any, Dict, List, Optional
from celery import Task from celery import Task
from django.core.cache import cache from django.core.cache import cache
@ -40,15 +40,30 @@ class TaskInfo:
result: TaskResult result: TaskResult
task_call_module: str
task_call_func: str
task_call_args: List[Any] = field(default_factory=list)
task_call_kwargs: Dict[str, Any] = field(default_factory=dict)
task_description: Optional[str] = field(default=None) task_description: Optional[str] = field(default=None)
@staticmethod
def all() -> Dict[str, "TaskInfo"]:
"""Get all TaskInfo objects"""
return cache.get_many(cache.keys("task_*"))
@staticmethod
def by_name(name: str) -> Optional["TaskInfo"]:
"""Get TaskInfo Object by name"""
return cache.get(f"task_{name}")
def save(self): def save(self):
"""Save task into cache""" """Save task into cache"""
key = f"task_{self.task_name}" key = f"task_{self.task_name}"
if self.result.uid: if self.result.uid:
key += f"_{self.result.uid}" key += f"_{self.result.uid}"
self.task_name += f"_{self.result.uid}" self.task_name += f"_{self.result.uid}"
cache.set(key, self) cache.set(key, self, timeout=6 * 60 * 60)
class MonitoredTask(Task): class MonitoredTask(Task):
@ -65,12 +80,18 @@ class MonitoredTask(Task):
self._result = result self._result = result
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def after_return(self, status, retval, task_id, args, kwargs, einfo): def after_return(
self, status, retval, task_id, args: List[Any], kwargs: Dict[str, Any], einfo
):
TaskInfo( TaskInfo(
task_name=self.__name__, task_name=self.__name__,
task_description=self.__doc__, task_description=self.__doc__,
finish_timestamp=datetime.now(), finish_timestamp=datetime.now(),
result=self._result, result=self._result,
task_call_module=self.__module__,
task_call_func=self.__name__,
task_call_args=args,
task_call_kwargs=kwargs,
).save() ).save()
return super().after_return(status, retval, task_id, args, kwargs, einfo=einfo) return super().after_return(status, retval, task_id, args, kwargs, einfo=einfo)
@ -81,6 +102,10 @@ class MonitoredTask(Task):
task_description=self.__doc__, task_description=self.__doc__,
finish_timestamp=datetime.now(), finish_timestamp=datetime.now(),
result=self._result, result=self._result,
task_call_module=self.__module__,
task_call_func=self.__name__,
task_call_args=args,
task_call_kwargs=kwargs,
).save() ).save()
return super().on_failure(exc, task_id, args, kwargs, einfo=einfo) return super().on_failure(exc, task_id, args, kwargs, einfo=einfo)

View File

@ -53,7 +53,7 @@ class KubernetesManifestView(LoginRequiredMixin, View):
) )
manifest = "" manifest = ""
if outpost.type == OutpostType.PROXY: if outpost.type == OutpostType.PROXY:
controller = ProxyKubernetesController(outpost_pk) controller = ProxyKubernetesController(outpost)
manifest = controller.get_static_deployment() manifest = controller.get_static_deployment()
return HttpResponse(manifest, content_type="text/vnd.yaml") return HttpResponse(manifest, content_type="text/vnd.yaml")

View File

@ -0,0 +1,20 @@
# Generated by Django 3.1.2 on 2020-10-16 11:07
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("passbook_providers_oauth2", "0006_remove_oauth2provider_name"),
]
operations = [
migrations.AlterModelOptions(
name="refreshtoken",
options={
"verbose_name": "OAuth2 Token",
"verbose_name_plural": "OAuth2 Tokens",
},
),
]

View File

@ -417,8 +417,8 @@ class RefreshToken(ExpiringModel, BaseGrantModel):
_id_token = models.TextField(verbose_name=_("ID Token")) _id_token = models.TextField(verbose_name=_("ID Token"))
class Meta: class Meta:
verbose_name = _("Token") verbose_name = _("OAuth2 Token")
verbose_name_plural = _("Tokens") verbose_name_plural = _("OAuth2 Tokens")
@property @property
def id_token(self) -> IDToken: def id_token(self) -> IDToken: