From 7806cff96fbc01dfc2b2878dab132d049511e434 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Fri, 16 Oct 2020 12:55:10 +0200 Subject: [PATCH] lib: save task's call arguments for manual retry --- passbook/admin/views/tasks.py | 5 ++- passbook/lib/tasks.py | 31 +++++++++++++++++-- passbook/outposts/views.py | 2 +- .../migrations/0007_auto_20201016_1107.py | 20 ++++++++++++ passbook/providers/oauth2/models.py | 4 +-- 5 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 passbook/providers/oauth2/migrations/0007_auto_20201016_1107.py diff --git a/passbook/admin/views/tasks.py b/passbook/admin/views/tasks.py index dc8cc7586..796616993 100644 --- a/passbook/admin/views/tasks.py +++ b/passbook/admin/views/tasks.py @@ -1,11 +1,10 @@ """passbook Tasks List""" from typing import Any, Dict -from django.core.cache import cache from django.views.generic.base import TemplateView from passbook.admin.mixins import AdminRequiredMixin -from passbook.lib.tasks import TaskResultStatus +from passbook.lib.tasks import TaskInfo, TaskResultStatus class TaskListView(AdminRequiredMixin, TemplateView): @@ -15,7 +14,7 @@ class TaskListView(AdminRequiredMixin, TemplateView): def get_context_data(self, **kwargs: Any) -> Dict[str, Any]: 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_warning"] = TaskResultStatus.WARNING kwargs["task_error"] = TaskResultStatus.ERROR diff --git a/passbook/lib/tasks.py b/passbook/lib/tasks.py index bc95f320c..0960d24e9 100644 --- a/passbook/lib/tasks.py +++ b/passbook/lib/tasks.py @@ -2,7 +2,7 @@ from dataclasses import dataclass, field from datetime import datetime from enum import Enum -from typing import List, Optional +from typing import Any, Dict, List, Optional from celery import Task from django.core.cache import cache @@ -40,15 +40,30 @@ class TaskInfo: 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) + @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): """Save task into cache""" key = f"task_{self.task_name}" if self.result.uid: key += 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): @@ -65,12 +80,18 @@ class MonitoredTask(Task): self._result = result # 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( task_name=self.__name__, task_description=self.__doc__, finish_timestamp=datetime.now(), result=self._result, + task_call_module=self.__module__, + task_call_func=self.__name__, + task_call_args=args, + task_call_kwargs=kwargs, ).save() return super().after_return(status, retval, task_id, args, kwargs, einfo=einfo) @@ -81,6 +102,10 @@ class MonitoredTask(Task): task_description=self.__doc__, finish_timestamp=datetime.now(), result=self._result, + task_call_module=self.__module__, + task_call_func=self.__name__, + task_call_args=args, + task_call_kwargs=kwargs, ).save() return super().on_failure(exc, task_id, args, kwargs, einfo=einfo) diff --git a/passbook/outposts/views.py b/passbook/outposts/views.py index 990b96608..3fab9055d 100644 --- a/passbook/outposts/views.py +++ b/passbook/outposts/views.py @@ -53,7 +53,7 @@ class KubernetesManifestView(LoginRequiredMixin, View): ) manifest = "" if outpost.type == OutpostType.PROXY: - controller = ProxyKubernetesController(outpost_pk) + controller = ProxyKubernetesController(outpost) manifest = controller.get_static_deployment() return HttpResponse(manifest, content_type="text/vnd.yaml") diff --git a/passbook/providers/oauth2/migrations/0007_auto_20201016_1107.py b/passbook/providers/oauth2/migrations/0007_auto_20201016_1107.py new file mode 100644 index 000000000..d8575aa36 --- /dev/null +++ b/passbook/providers/oauth2/migrations/0007_auto_20201016_1107.py @@ -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", + }, + ), + ] diff --git a/passbook/providers/oauth2/models.py b/passbook/providers/oauth2/models.py index 96004dac8..141d4e333 100644 --- a/passbook/providers/oauth2/models.py +++ b/passbook/providers/oauth2/models.py @@ -417,8 +417,8 @@ class RefreshToken(ExpiringModel, BaseGrantModel): _id_token = models.TextField(verbose_name=_("ID Token")) class Meta: - verbose_name = _("Token") - verbose_name_plural = _("Tokens") + verbose_name = _("OAuth2 Token") + verbose_name_plural = _("OAuth2 Tokens") @property def id_token(self) -> IDToken: