*: Improve MonitoredTasks' error capture

This commit is contained in:
Jens Langhammer 2020-10-16 16:00:24 +02:00
parent bdc019c7cf
commit 2339e855bb
5 changed files with 11 additions and 6 deletions

View File

@ -27,4 +27,4 @@ def update_latest_version(self: MonitoredTask):
)
except (RequestException, IndexError) as exc:
cache.set(VERSION_CACHE_KEY, "0.0.0", VERSION_CACHE_TIMEOUT)
self.set_status(TaskResult(TaskResultStatus.ERROR, [str(exc)]))
self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc))

View File

@ -2,6 +2,7 @@
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
from traceback import format_tb
from typing import Any, Dict, List, Optional
from celery import Task
@ -25,11 +26,15 @@ class TaskResult:
messages: List[str] = field(default_factory=list)
error: Optional[Exception] = field(default=None)
# Optional UID used in cache for tasks that run in different instances
uid: Optional[str] = field(default=None)
def with_error(self, exc: Exception) -> "TaskResult":
"""Since errors might not always be pickle-able, set the traceback"""
self.messages.extend(format_tb(exc.__traceback__))
self.messages.append(str(exc))
return self
@dataclass
class TaskInfo:

View File

@ -45,7 +45,7 @@ def outpost_controller(self: MonitoredTask, outpost_pk: str):
logs = ProxyDockerController(outpost).run_with_logs()
except ControllerException as exc:
self.set_status(
TaskResult(TaskResultStatus.ERROR, [str(exc)], exc, uid=outpost.name)
TaskResult(TaskResultStatus.ERROR, uid=outpost.name).with_error(exc)
)
else:
self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, logs, uid=outpost.name))

View File

@ -35,4 +35,4 @@ def ldap_sync(self: MonitoredTask, source_pk: int):
)
)
except LDAPException as exc:
self.set_status(TaskResult(TaskResultStatus.ERROR, [str(exc)], exc))
self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc))

View File

@ -62,5 +62,5 @@ def send_mail(self: MonitoredTask, email_stage_pk: int, message: Dict[Any, Any])
)
)
except (SMTPException, ConnectionError) as exc:
self.set_status(TaskResult(TaskResultStatus.ERROR, [str(exc)], exc))
self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc))
raise exc