Newton José
Newton José

Reputation: 449

Problem to pass `http_conn_id` to SlackWebhookOperator

I try create a SlackWebhookOperator, and using may HTTP connection, but he still traing use the http_default.

failed_alert = SlackWebhookOperator(
        task_id='slack_test',
        http_conn_id='slack_conn',
        webhook_token=slack_webhook_token,
        message=slack_msg,
        username='airflow')

failed_alert.execute(context=context)

[2019-07-21 13:14:57,415] {{init.py:1625}} ERROR - Failed at executing callback

[2019-07-21 13:14:57,415] {{init.py:1626}} ERROR - The conn_id http_default isn't defined

Upvotes: 2

Views: 2931

Answers (1)

Bob
Bob

Reputation: 415

I think its a known issue with 1.10.3: https://github.com/apache/airflow/pull/5066

My workaround is this:

def task_fail_slack_alert_hook(url, context): 
   """ This is a webhook utility which will push an error message to a given slack channel using a URL """

   slack_msg = """
           :red_circle: Task Failed.
           *Task*: {task}
           *Dag*: {dag}
           *Execution Time*: {exec_date}
           *Log Url*: {log_url}
           <!channel>
           """.format(
       task=context.get("task_instance").task_id,
       dag=context.get("task_instance").dag_id,
       ti=context.get("task_instance"),
       exec_date=context.get("execution_date"),
       log_url=context.get("task_instance").log_url,
   )
   slack_data = {"text": slack_msg}
   return requests.post(
       url,
       data=json.dumps(slack_data),
       headers={"Content-Type": "application/json"},
   )
  • You will have to put the whole webhook URL in the host though, rather than splitting host and password up.

  • You could also have a look at the slack client instead

Upvotes: 2

Related Questions