Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CZID-8390] Add sqs step notifications #116

Merged
merged 7 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ RUN apt-get -q update && apt-get -q install -y \
# upgrade because of this issue https://github.com/chanzuckerberg/miniwdl/issues/607 in miniwdl
RUN pip3 install importlib-metadata==4.13.0
RUN pip3 install miniwdl==${MINIWDL_VERSION}
RUN pip3 install urllib3==1.26.16

RUN curl -Ls https://github.com/chanzuckerberg/s3parcp/releases/download/v1.0.1/s3parcp_1.0.1_linux_amd64.tar.gz | tar -C /usr/bin -xz s3parcp

Expand All @@ -62,6 +63,7 @@ ADD miniwdl-plugins miniwdl-plugins
RUN pip install miniwdl-plugins/s3upload
RUN pip install miniwdl-plugins/sfn_wdl
RUN pip install miniwdl-plugins/s3parcp_download
RUN pip install miniwdl-plugins/sqs_notification

RUN cd /usr/bin; curl -O https://amazon-ecr-credential-helper-releases.s3.amazonaws.com/0.4.0/linux-amd64/docker-credential-ecr-login
RUN chmod +x /usr/bin/docker-credential-ecr-login
Expand Down
1 change: 1 addition & 0 deletions miniwdl-plugins/sqs_notification/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# sqs_notifications
29 changes: 29 additions & 0 deletions miniwdl-plugins/sqs_notification/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
from setuptools import setup
from os import path

this_directory = path.abspath(path.dirname(__file__))
with open(path.join(path.dirname(__file__), "README.md")) as f:
long_description = f.read()

setup(
name="sqs_notification",
version="0.0.1",
description="miniwdl plugin for notification of task completion to Amazon SQS",
url="https://github.com/chanzuckerberg/miniwdl-s3upload",
project_urls={},
long_description=long_description,
long_description_content_type="text/markdown",
author="",
py_modules=["sqs_notification"],
python_requires=">=3.6",
setup_requires=["reentry"],
install_requires=["boto3"],
reentry_register=True,
entry_points={
"miniwdl.plugin.task": ["sqs_notification_task = sqs_notification:task"],
"miniwdl.plugin.workflow": [
"sqs_notification_workflow = sqs_notification:workflow"
],
},
)
83 changes: 83 additions & 0 deletions miniwdl-plugins/sqs_notification/sqs_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
TODO
"""
rzlim08 marked this conversation as resolved.
Show resolved Hide resolved

import os
import json
from typing import Dict
from datetime import datetime
from WDL import values_to_json

import boto3

sqs_client = boto3.client("sqs", endpoint_url=os.getenv("AWS_ENDPOINT_URL"))
queue_url = os.getenv("AWS_STEP_NOTIFICATION_PLUGIN")


def process_outputs(outputs: Dict):
"""process outputs dict into string to be passed into SQS"""
# only stringify for now
return json.dumps(outputs)


def send_message(attr, body):
"""send message to SQS, eventually wrap this in a try catch to deal with throttling"""
sqs_resp = sqs_client.send_message(
QueueUrl=queue_url,
DelaySeconds=0,
MessageAttributes=attr,
MessageBody=body,
)
return sqs_resp


def task(cfg, logger, run_id, run_dir, task, **recv):
"""
on completion of any task, upload its output files to S3, and record the S3 URI corresponding
to each local file (keyed by inode) in _uploaded_files
"""
logger = logger.getChild("s3_progressive_upload")

# ignore inputs
recv = yield recv

# ignore command/runtime/container
recv = yield recv

message_attributes = {
"WorkflowName": {"DataType": "String", "StringValue": run_id[0]},
"TaskName": {"DataType": "String", "StringValue": run_id[-1]},
"ExecutionId": {
"DataType": "String",
"StringValue": "execution_id_to_be_passed_in",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this get populated? You should be able to pass this as an env variable in the step function definition.

Copy link
Collaborator Author

@rzlim08 rzlim08 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking we could also make this a WDL input & set it when the workflow starts. Either one is fine with me. We also want to use the Run Id here instead of the Execution Id

},
}

outputs = process_outputs(values_to_json(recv["outputs"]))
message_body = {
"version": "0",
"id": "0",
"detail-type": "Step Functions Execution Step Notification",
"source": "aws.batch",
"account": "",
"time": datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"),
"resources": [],
"detail": outputs,
}
send_message(message_attributes, json.dumps(message_body))

yield recv


def workflow(cfg, logger, run_id, run_dir, workflow, **recv):
"""
on workflow completion, add a file outputs.s3.json to the run directory, which is outputs.json
with local filenames rewritten to the uploaded S3 URIs (as previously recorded on completion of
each task).
"""
logger = logger.getChild("s3_progressive_upload")

# ignore inputs
recv = yield recv

yield recv
1 change: 1 addition & 0 deletions terraform/modules/swipe-sfn-batch-job/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ locals {
"MINIWDL__DOWNLOAD_CACHE__DISABLE_PATTERNS" = "[\"s3://swipe-samples-*/*\"]",
"DOWNLOAD_CACHE_MAX_GB" = "500",
"WDL_PASSTHRU_ENVVARS" = join(" ", [for k, v in var.extra_env_vars : k]),
"AWS_STEP_NOTIFICATION_PLUGIN" = var.sfn_notification_queue_urls[0],
rzlim08 marked this conversation as resolved.
Show resolved Hide resolved
"OUTPUT_STATUS_JSON_FILES" = tostring(var.output_status_json_files)
})
container_env_vars = { "environment" : [for k in sort(keys(local.batch_env_vars)) : { "name" : k, "value" : local.batch_env_vars[k] }] }
Expand Down
10 changes: 10 additions & 0 deletions terraform/modules/swipe-sfn-batch-job/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ variable "docker_network" {
type = string
default = ""
}

variable "sfn_notification_queue_arns" {
description = "ARNs of notification SQS queues"
type = list(string)
}
rzlim08 marked this conversation as resolved.
Show resolved Hide resolved

variable "sfn_notification_queue_urls" {
description = "URLs of notification SQS queues"
type = list(string)
}
rzlim08 marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 15 additions & 13 deletions terraform/modules/swipe-sfn/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ resource "aws_iam_role_policy_attachment" "swipe_sfn_service" {
}

module "batch_job" {
source = "../swipe-sfn-batch-job"
app_name = var.app_name
batch_job_docker_image = var.batch_job_docker_image
batch_job_timeout_seconds = var.batch_job_timeout_seconds
miniwdl_dir = var.miniwdl_dir
workspace_s3_prefixes = var.workspace_s3_prefixes
wdl_workflow_s3_prefix = var.wdl_workflow_s3_prefix
job_policy_arns = var.job_policy_arns
extra_env_vars = var.extra_env_vars
docker_network = var.docker_network
call_cache = var.call_cache
output_status_json_files = var.output_status_json_files
tags = var.tags
source = "../swipe-sfn-batch-job"
app_name = var.app_name
batch_job_docker_image = var.batch_job_docker_image
batch_job_timeout_seconds = var.batch_job_timeout_seconds
miniwdl_dir = var.miniwdl_dir
workspace_s3_prefixes = var.workspace_s3_prefixes
wdl_workflow_s3_prefix = var.wdl_workflow_s3_prefix
job_policy_arns = var.job_policy_arns
extra_env_vars = var.extra_env_vars
docker_network = var.docker_network
call_cache = var.call_cache
output_status_json_files = var.output_status_json_files
sfn_notification_queue_arns = [for name, queue in aws_sqs_queue.sfn_notifications_queue : queue.arn]
sfn_notification_queue_urls = [for name, queue in aws_sqs_queue.sfn_notifications_queue : queue.url]
tags = var.tags
}

module "sfn_io_helper" {
Expand Down
Loading
Loading