-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CZID-8390] Add sqs step notifications (#116)
* add current sqs queue to sqs notification step * run black * terraform fmt * typing * change to using sns * add formatting * add options to turn off step notifications
- Loading branch information
Showing
14 changed files
with
249 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# sns_notifications |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="sns_notification", | ||
version="0.0.1", | ||
description="miniwdl plugin for notification of task completion to Amazon SQS", | ||
url="https://github.com/chanzuckerberg/swipe", | ||
project_urls={}, | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
author="", | ||
py_modules=["sns_notification"], | ||
python_requires=">=3.6", | ||
setup_requires=["reentry"], | ||
install_requires=["boto3"], | ||
reentry_register=True, | ||
entry_points={ | ||
"miniwdl.plugin.task": ["sns_notification_task = sns_notification:task"], | ||
"miniwdl.plugin.workflow": [ | ||
"sns_notification_workflow = sns_notification:workflow" | ||
], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
Send SNS notifications after each miniwdl step | ||
""" | ||
|
||
import os | ||
import json | ||
from typing import Dict | ||
from datetime import datetime | ||
from WDL import values_to_json | ||
from WDL._util import StructuredLogMessage as _ | ||
|
||
import boto3 | ||
|
||
sns_client = boto3.client("sns", endpoint_url=os.getenv("AWS_ENDPOINT_URL")) | ||
topic_arn = os.getenv('STEP_NOTIFICATION_TOPIC_ARN') | ||
|
||
|
||
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 SNS""" | ||
sns_resp = sns_client.publish( | ||
TopicArn=topic_arn, | ||
Message=body, | ||
MessageAttributes=attr, | ||
) | ||
return sns_resp | ||
|
||
|
||
def task(cfg, logger, run_id, run_dir, task, **recv): | ||
""" | ||
on completion of any task sends a message to sns with the output files | ||
""" | ||
log = logger.getChild("sns_step_notification") | ||
|
||
# ignore inputs | ||
recv = yield recv | ||
# ignore command/runtime/container | ||
recv = yield recv | ||
|
||
log.info(_("sending message to sns")) | ||
|
||
if topic_arn: | ||
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", | ||
}, | ||
} | ||
|
||
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): | ||
log = logger.getChild("sns_step_notification") | ||
|
||
# ignore inputs | ||
recv = yield recv | ||
|
||
log.info(_("ignores workflow calls")) | ||
yield recv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.