From f6bc4a46b5a3c08f1110d330415a93fe360e4edc Mon Sep 17 00:00:00 2001 From: "Anna (Anya) Parker" <50943381+anna-parker@users.noreply.github.com> Date: Tue, 13 Aug 2024 18:38:27 +0200 Subject: [PATCH] feat(ingest): Add slack_hook to ingest, send notification when reingest wants to change grouping and revoke old groups. (#2392) * Add slack_hook to ingest, send notification when reingest wants to change grouping and revoke old groups. --- ingest/Snakefile | 1 + ingest/scripts/prepare_files.py | 25 +++++++++++++++++++ .../loculus/templates/ingest-deployment.yaml | 5 ++++ 3 files changed, 31 insertions(+) diff --git a/ingest/Snakefile b/ingest/Snakefile index 931392905..4936f2672 100644 --- a/ingest/Snakefile +++ b/ingest/Snakefile @@ -11,6 +11,7 @@ for key, value in defaults.items(): if not key in config: config[key] = value +config["slack_hook"] = os.getenv("SLACK_HOOK") # Infer whether nucleotide sequences are segmented config["segmented"] = len(config["nucleotide_sequences"]) > 1 diff --git a/ingest/scripts/prepare_files.py b/ingest/scripts/prepare_files.py index 585d83f69..468a6444b 100644 --- a/ingest/scripts/prepare_files.py +++ b/ingest/scripts/prepare_files.py @@ -7,6 +7,7 @@ import click import orjsonl +import requests import yaml @@ -14,6 +15,8 @@ class Config: segmented: str nucleotide_sequences: list[str] + slack_hook: str + backend_url: str logger = logging.getLogger(__name__) @@ -37,6 +40,24 @@ def ids_to_add(fasta_id, config) -> set[str]: return {fasta_id} +def notify(config: Config, text: str): + """Send slack notification with revocation details""" + if config.slack_hook: + requests.post(config.slack_hook, data=json.dumps({"text": text}), timeout=10) + logger.warn(text) + + +def revocation_notification(config: Config, to_revoke: dict[str, dict[str, str]]): + """Send slack notification with revocation details""" + text = ( + f"{config.backend_url}: Ingest pipeline wants to add the following sequences" + f" which will lead to revocations: {to_revoke}. " + "If you agree with this run the regroup_and_revoke rule in the ingest pod:" + " `kubectl exec -it INGEST_POD_NAME -- snakemake regroup_and_revoke`." + ) + notify(config, text) + + @click.command() @click.option("--config-file", required=True, type=click.Path(exists=True)) @click.option("--metadata-path", required=True, type=click.Path(exists=True)) @@ -102,10 +123,14 @@ def main( metadata_revise.append(revise_record) revise_ids.update(ids_to_add(fasta_id, config)) + found_seq_to_revoke = False for fasta_id in to_revoke: metadata_submit_prior_to_revoke.append(metadata[fasta_id]) submit_prior_to_revoke_ids.update(ids_to_add(fasta_id, config)) + if found_seq_to_revoke: + revocation_notification(config, to_revoke) + def write_to_tsv(data, filename): if not data: Path(filename).touch() diff --git a/kubernetes/loculus/templates/ingest-deployment.yaml b/kubernetes/loculus/templates/ingest-deployment.yaml index 00cecce64..3147c52c1 100644 --- a/kubernetes/loculus/templates/ingest-deployment.yaml +++ b/kubernetes/loculus/templates/ingest-deployment.yaml @@ -43,6 +43,11 @@ spec: secretKeyRef: name: ingest-ncbi key: api-key + - name: SLACK_HOOK + valueFrom: + secretKeyRef: + name: slack-notifications + key: slack-hook args: - snakemake - results/approved