Skip to content

Commit c078468

Browse files
FloLeyinmantaci
authored andcommitted
Added options to compiler service to configure notification behavior (Issue #4803, PR #4829)
# Description - add an option to configure if notification is required - add message option to use when notification is used - add sane defaults for both to ensure backwards compatibility closes #4803 # Self Check: Strike through any lines that are not applicable (`~~line~~`) then check the box - [x] Attached issue to pull request - [x] Changelog entry - [x] Type annotations are present - [x] Code is clear and sufficiently documented - [x] No (preventable) type errors (check using make mypy or make mypy-diff) - [x] Sufficient test cases (reproduces the bug/tests the requested feature) - [x] Correct, in line with design - [x] End user documentation is included or an issue is created for end-user documentation (add ref to issue here: ) # Reviewer Checklist: - [ ] Sufficient test cases (reproduces the bug/tests the requested feature) - [ ] Code is clear and sufficiently documented - [ ] Correct, in line with design
1 parent ddada43 commit c078468

9 files changed

Lines changed: 1302 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
description: Added options to compiler service to configure notification behavior
2+
change-type: minor
3+
destination-branches: [master, iso5]
4+
issue-nr: 4803
5+
sections: {
6+
minor-improvement: "{{description}}"
7+
}

src/inmanta/data/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3484,6 +3484,9 @@ class Compile(BaseDocument):
34843484
to this one that actually got compiled.
34853485
:param partial: True if the compile only contains the entities/resources for the resource sets that should be updated
34863486
:param removed_resource_sets: indicates the resource sets that should be removed from the model
3487+
:param notify_failed_compile: if true use the notification service to notify that a compile has failed.
3488+
By default, notifications are enabled only for exporting compiles.
3489+
:param failed_compile_message: Optional message to use when a notification for a failed compile is created
34873490
"""
34883491

34893492
__primary_key__ = ("id",)
@@ -3513,6 +3516,9 @@ class Compile(BaseDocument):
35133516
partial: bool = False
35143517
removed_resource_sets: list[str] = []
35153518

3519+
notify_failed_compile: Optional[bool] = None
3520+
failed_compile_message: Optional[str] = None
3521+
35163522
@classmethod
35173523
async def get_substitute_by_id(cls, compile_id: uuid.UUID) -> Optional["Compile"]:
35183524
"""

src/inmanta/data/model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ class CompileRunBase(BaseModel):
151151
partial: bool = False
152152
removed_resource_sets: list[str] = []
153153

154+
notify_failed_compile: Optional[bool] = None
155+
failed_compile_message: Optional[str] = None
156+
154157

155158
class CompileRun(CompileRunBase):
156159
compile_data: Optional[CompileData]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Copyright 2022 Inmanta
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
Contact: code@inmanta.com
17+
"""
18+
19+
from asyncpg import Connection
20+
21+
22+
async def update(connection: Connection) -> None:
23+
schema = """
24+
ALTER TABLE public.compile
25+
ADD COLUMN notify_failed_compile boolean DEFAULT NULL,
26+
ADD COLUMN failed_compile_message varchar DEFAULT NULL;
27+
"""
28+
async with connection.transaction():
29+
await connection.execute(schema)

src/inmanta/server/services/compilerservice.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,11 +570,19 @@ async def request_recompile(
570570
env_vars: Optional[Mapping[str, str]] = None,
571571
partial: bool = False,
572572
removed_resource_sets: Optional[List[str]] = None,
573+
notify_failed_compile: Optional[bool] = None,
574+
failed_compile_message: Optional[str] = None,
573575
) -> Tuple[Optional[uuid.UUID], Warnings]:
574576
"""
575577
Recompile an environment in a different thread and taking wait time into account.
576578
579+
:param notify_failed_compile: if set to True, errors during compilation will be notified using the
580+
"failed_compile_message".
581+
if set to false, nothing will be notified. If not set then the default notifications are
582+
sent (failed pull stage and errors during the do_export)
583+
:param failed_compile_message: the message used in notifications if notify_failed_compile is set to True.
577584
:return: the compile id of the requested compile and any warnings produced during the request
585+
578586
"""
579587
if removed_resource_sets is None:
580588
removed_resource_sets = []
@@ -600,6 +608,8 @@ async def request_recompile(
600608
environment_variables=env_vars,
601609
partial=partial,
602610
removed_resource_sets=removed_resource_sets,
611+
notify_failed_compile=notify_failed_compile,
612+
failed_compile_message=failed_compile_message,
603613
)
604614
await compile.insert()
605615
await self._queue(compile)

src/inmanta/server/services/notificationservice.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,18 @@ async def compile_done(self, compile: data.Compile) -> None:
7070
failed_pull_stage = next(
7171
(report for report in reports if report["name"] == "Pulling updates" and report["returncode"] != 0), None
7272
)
73-
if failed_pull_stage:
73+
if compile.notify_failed_compile is False:
74+
return
75+
elif compile.notify_failed_compile and compile.failed_compile_message:
76+
# Use specific message provided in request
77+
await self.notify(
78+
compile.environment,
79+
title="Compilation failed",
80+
message=compile.failed_compile_message,
81+
severity=const.NotificationSeverity.error,
82+
uri=f"/api/v2/compilereport/{compile.id}",
83+
)
84+
elif failed_pull_stage:
7485
await self.notify(
7586
compile.environment,
7687
title="Pulling updates during compile failed",
@@ -86,6 +97,15 @@ async def compile_done(self, compile: data.Compile) -> None:
8697
severity=const.NotificationSeverity.error,
8798
uri=f"/api/v2/compilereport/{compile.id}",
8899
)
100+
elif compile.notify_failed_compile:
101+
# Send notification with generic message as fallback
102+
await self.notify(
103+
compile.environment,
104+
title="Compilation failed",
105+
message="A compile has failed",
106+
severity=const.NotificationSeverity.error,
107+
uri=f"/api/v2/compilereport/{compile.id}",
108+
)
89109

90110
async def notify(
91111
self,

0 commit comments

Comments
 (0)