Skip to content

Commit

Permalink
quick_setup: extend background exception object with message
Browse files Browse the repository at this point in the history
Change-Id: I1e236128aa245f8217b1c80521f2c89ed92ba8f3
  • Loading branch information
anthonyh209 committed Jan 14, 2025
1 parent 87a48bb commit 37469bc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
24 changes: 17 additions & 7 deletions cmk/gui/openapi/endpoints/quick_setup/response_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@

from cmk import fields

BACKGROUND_JOB_EXCEPTION = fields.String(
example="An exception message",
description="The exception message if the action was run in the background and raised "
"an unexpected exception",
allow_none=True,

class BackgroundJobException(BaseSchema):
message = fields.String(
example="An exception message",
description="The exception message",
)
traceback = fields.String(
example="The traceback of the background job exception",
description="The traceback of the exception",
)


BACKGROUND_JOB_EXCEPTION = fields.Nested(
BackgroundJobException,
description="The exception details if the action was run in the background and raised an "
"unexpected exception",
example={},
)


Expand Down Expand Up @@ -211,7 +223,6 @@ class QuickSetupCompleteResponse(BaseSchema):
example="http://save/url",
description="The url to redirect to after saving the quicksetup",
)

all_stage_errors = fields.List(
fields.Nested(
Errors,
Expand All @@ -222,5 +233,4 @@ class QuickSetupCompleteResponse(BaseSchema):
description="A list of stage errors",
example=[],
)

background_job_exception = BACKGROUND_JOB_EXCEPTION
15 changes: 10 additions & 5 deletions cmk/gui/quick_setup/handlers/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from cmk.gui.quick_setup.handlers.utils import (
Action,
BackgroundJobException,
Button,
form_spec_parse,
get_stage_components_from_widget,
Expand Down Expand Up @@ -232,7 +233,7 @@ def complete_quick_setup(
class CompleteActionResult(BaseModel):
all_stage_errors: Sequence[ValidationErrors] | None = None
redirect_url: str | None = None
background_job_exception: str | None = None
background_job_exception: BackgroundJobException | None = None

@classmethod
def load_from_job_result(cls, job_id: str) -> "CompleteActionResult":
Expand Down Expand Up @@ -325,10 +326,14 @@ def run_quick_setup_stage(self, job_interface: BackgroundProcessInterface) -> No
job_interface.get_logger().debug(
"Exception raised while the Quick setup stage action: %s", e
)
job_interface.send_exception(str(e))
CompleteActionResult(background_job_exception=traceback.format_exc()).save_to_file(
job_interface.get_work_dir()
)
exception_message = str(e)
job_interface.send_exception(exception_message)
CompleteActionResult(
background_job_exception=BackgroundJobException(
message=exception_message,
traceback=traceback.format_exc(),
)
).save_to_file(job_interface.get_work_dir())

def _run_quick_setup_stage(self, job_interface: BackgroundProcessInterface) -> None:
job_interface.send_progress_update(_("Starting Quick setup action..."))
Expand Down
14 changes: 9 additions & 5 deletions cmk/gui/quick_setup/handlers/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from cmk.gui.quick_setup.config_setups import register as register_config_setups
from cmk.gui.quick_setup.handlers.utils import (
Action,
BackgroundJobException,
Button,
form_spec_parse,
get_stage_components_from_widget,
Expand Down Expand Up @@ -199,7 +200,7 @@ class StageActionResult(BaseModel, frozen=False):
# TODO: This should be a list of widgets using only Sequence[Widget] will remove all fields
# when the data is returned (this is a temporary fix)
stage_recap: Sequence[Any] = field(default_factory=list)
background_job_exception: str | None = None
background_job_exception: BackgroundJobException | None = None

@classmethod
def load_from_job_result(cls, job_id: str) -> "StageActionResult":
Expand Down Expand Up @@ -293,10 +294,13 @@ def run_quick_setup_stage_action(self, job_interface: BackgroundProcessInterface
job_interface.get_logger().debug(
"Exception raised while the Quick setup stage action: %s", e
)
job_interface.send_exception(str(e))
StageActionResult(background_job_exception=traceback.format_exc()).save_to_file(
job_interface.get_work_dir()
)
exception_message = str(e)
job_interface.send_exception(exception_message)
StageActionResult(
background_job_exception=BackgroundJobException(
message=exception_message, traceback=traceback.format_exc()
)
).save_to_file(job_interface.get_work_dir())

def _run_quick_setup_stage_action(self, job_interface: BackgroundProcessInterface) -> None:
job_interface.send_progress_update(_("Starting Quick stage action..."))
Expand Down
7 changes: 7 additions & 0 deletions cmk/gui/quick_setup/handlers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from dataclasses import asdict, dataclass, field
from typing import Any, cast, Iterable, Mapping, MutableMapping, MutableSequence, Sequence

from pydantic import BaseModel

from cmk.ccc.i18n import _

from cmk.gui.form_specs.vue.form_spec_visitor import (
Expand Down Expand Up @@ -140,3 +142,8 @@ def validate_custom_validators(
)
)
return errors


class BackgroundJobException(BaseModel):
message: str
traceback: str

0 comments on commit 37469bc

Please sign in to comment.