Skip to content

Commit

Permalink
Remove sys.exit() that breaks the promote endpoint (#66)
Browse files Browse the repository at this point in the history
* Remove sys.exit() that breaks the promote endpoint
* Improve error message representation
  • Loading branch information
andrejvelichkovski authored Dec 1, 2023
1 parent 8a08727 commit efb4211
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
41 changes: 22 additions & 19 deletions backend/test_observer/controllers/promoter/promoter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,21 @@ def promote_artefacts(db: Session = Depends(get_db)):
external source
"""
try:
processed_artefacts = promoter_controller(db)
logger.info("INFO: Processed artefacts %s", processed_artefacts)
if False in processed_artefacts.values():
(
processed_artefacts_status,
processed_artefacts_error_messages,
) = promoter_controller(db)
logger.info("INFO: Processed artefacts %s", processed_artefacts_status)
if False in processed_artefacts_status.values():
return JSONResponse(
status_code=500,
content={
"detail": (
"Got some errors while processing the next artefacts: "
", ".join(
[k for k, v in processed_artefacts.items() if v is False]
)
)
artefact_key: processed_artefacts_error_messages[artefact_key]
for (
artefact_key,
artefact_status,
) in processed_artefacts_status.items()
if artefact_status is False
},
)
return JSONResponse(
Expand All @@ -85,32 +88,32 @@ def promote_artefacts(db: Session = Depends(get_db)):
return JSONResponse(status_code=500, content={"detail": str(exc)})


def promoter_controller(session: Session) -> dict:
def promoter_controller(session: Session) -> tuple[dict, dict]:
"""
Orchestrate the snap promoter job
:session: DB connection session
:return: dict with the processed cards and the status of execution
:return: tuple of dicts, the first the processed cards and the status of execution
the second only for the processed cards with the corresponding error message
"""
family_mapping = {
FamilyName.SNAP: run_snap_promoter,
FamilyName.DEB: run_deb_promoter,
}
processed_artefacts_status = {}
processed_artefacts_error_messages = {}
for family_name, promoter_function in family_mapping.items():
artefacts = get_artefacts_by_family(session, family_name, load_stage=True)
processed_artefacts = {}
for artefact in artefacts:
artefact_key = f"{family_name} - {artefact.name} - {artefact.version}"
try:
processed_artefacts[
f"{family_name} - {artefact.name} - {artefact.version}"
] = True
processed_artefacts_status[artefact_key] = True
promoter_function(session, artefact)
except Exception as exc:
processed_artefacts[
f"{family_name} - {artefact.name} - {artefact.version}"
] = False
processed_artefacts_status[artefact_key] = False
processed_artefacts_error_messages[artefact_key] = str(exc)
logger.warning("WARNING: %s", str(exc), exc_info=True)
return processed_artefacts
return processed_artefacts_status, processed_artefacts_error_messages


def run_snap_promoter(session: Session, artefact: Artefact) -> None:
Expand Down
3 changes: 1 addition & 2 deletions backend/test_observer/external_apis/snapcraft.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import sys

import requests

Expand Down Expand Up @@ -30,7 +29,7 @@ def get_channel_map_from_snapcraft(arch: str, snapstore: str, snap_name: str):
json_resp = req.json()
if not req.ok:
logger.error(json_resp["error-list"][0]["message"])
sys.exit(1)
req.raise_for_status()

snap_info = SnapInfo(**rename_keys(json_resp))
return snap_info.channel_map

0 comments on commit efb4211

Please sign in to comment.