-
-
Save johnmuller87/e462c841541a8488cff41dae6f4a4ebb to your computer and use it in GitHub Desktop.
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
def send_slack_alert(context=None): | |
"""Send slack alert on failure to alert the team""" | |
payload_vars = { | |
'url': 'https://hooks.slack.com/services/<YOUR_CREDENTIALS_HERE>', | |
'run_id': str(context['run_id']), | |
'task': str(context['task']), | |
'dag_name': str(context['dag'].dag_id) | |
} | |
error_message = "{dag_name} Failure! Task failed: {task} Check log at: {run_id}".format(**payload_vars) | |
payload_vars['json'] = """payload={{"channel":"#<YOUR_CHANNEL_HERE>","text":"{0}"}}""".format(error_message) | |
slack_cmd = """curl | |
-X POST \ | |
--data-urlencode '{json}' \ | |
{url}""".format(**payload_vars) | |
slack_alert = BashOperator( | |
task_id='slack_alert', | |
dag=dag, | |
bash_command=slack_cmd | |
) | |
slack_alert.execute(context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment