*: Improve MonitoredTasks' error capture
This commit is contained in:
parent
bdc019c7cf
commit
2339e855bb
|
@ -27,4 +27,4 @@ def update_latest_version(self: MonitoredTask):
|
||||||
)
|
)
|
||||||
except (RequestException, IndexError) as exc:
|
except (RequestException, IndexError) as exc:
|
||||||
cache.set(VERSION_CACHE_KEY, "0.0.0", VERSION_CACHE_TIMEOUT)
|
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))
|
||||||
|
|
|
@ -2,6 +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 traceback import format_tb
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Dict, List, Optional
|
||||||
|
|
||||||
from celery import Task
|
from celery import Task
|
||||||
|
@ -25,11 +26,15 @@ class TaskResult:
|
||||||
|
|
||||||
messages: List[str] = field(default_factory=list)
|
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
|
# Optional UID used in cache for tasks that run in different instances
|
||||||
uid: Optional[str] = field(default=None)
|
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
|
@dataclass
|
||||||
class TaskInfo:
|
class TaskInfo:
|
||||||
|
|
|
@ -45,7 +45,7 @@ def outpost_controller(self: MonitoredTask, outpost_pk: str):
|
||||||
logs = ProxyDockerController(outpost).run_with_logs()
|
logs = ProxyDockerController(outpost).run_with_logs()
|
||||||
except ControllerException as exc:
|
except ControllerException as exc:
|
||||||
self.set_status(
|
self.set_status(
|
||||||
TaskResult(TaskResultStatus.ERROR, [str(exc)], exc, uid=outpost.name)
|
TaskResult(TaskResultStatus.ERROR, uid=outpost.name).with_error(exc)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, logs, uid=outpost.name))
|
self.set_status(TaskResult(TaskResultStatus.SUCCESSFUL, logs, uid=outpost.name))
|
||||||
|
|
|
@ -35,4 +35,4 @@ def ldap_sync(self: MonitoredTask, source_pk: int):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except LDAPException as exc:
|
except LDAPException as exc:
|
||||||
self.set_status(TaskResult(TaskResultStatus.ERROR, [str(exc)], exc))
|
self.set_status(TaskResult(TaskResultStatus.ERROR).with_error(exc))
|
||||||
|
|
|
@ -62,5 +62,5 @@ def send_mail(self: MonitoredTask, email_stage_pk: int, message: Dict[Any, Any])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except (SMTPException, ConnectionError) as exc:
|
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
|
raise exc
|
||||||
|
|
Reference in New Issue