0

Trying to post to Slack using the on_failure_callback. I have this procedure below and my default dag args has

'on_failure_callback' : notify_slack_failure

When my task fails, it is creating the output in /tmp..but my Slack message is not posting. When I test it via command line it will post to Slack. Any thoughts on what I'm missing?

    def notify_slack_failure(context):
       """
       Define the callback to post on Slack if a failure is detected in the Workflow
       :return: operator.execute
       """
       
       cmd = "echo '" + getSlackToken() +"' > /tmp/a.out"
       os.system("touch /tmp/b.out")
       os.system(cmd)
       text_message='333'
       #text_message=str(context['task_instance'])
       
       operator = SlackAPIPostOperator(
          task_id='failure',
          text=text_message,
          token=getSlackToken(),
          channel=SLACK_CHANNEL,
          username=SLACK_USER
       )
       os.system("touch /tmp/e1.out")
       return operator.execute(context=context)
0x26res
  • 11,925
  • 11
  • 54
  • 108
George Mansoor
  • 121
  • 1
  • 2
  • 7

2 Answers2

1

George -Welcome to SO! Try running this code. See if Airflow is able to post to Slack, and if the code writing to the tmp file was messing it up.

def notify_slack_failure(contextDictionary, **kwargs):  
   """
   Define the callback to post on Slack if a failure is detected in the Workflow
   :return: operator.execute
   """
   text_message='333'
   #text_message=str(context['task_instance'])

   operator= SlackAPIPostOperator(
      task_id='failure',
      token=getSlackToken(),
      text=text_message,
      channel=SLACK_CHANNEL,
      username=SLACK_USER,)
      return operator.execute

Taken from SO Slack Airflow answer

Zack
  • 2,296
  • 20
  • 28
  • 1
    thank you so much..but the error was mine. we go through a http proxy for our outbound connections. Have it set in the bash profile...but forgot to define it for the systemd service. 1st time up..and dumb mistake. Sorry! – George Mansoor Jul 05 '18 at 22:20
  • No worries George! – Zack Jul 06 '18 at 13:28
0

In the command line I had my http_proxy vars defined and did not have them set for the daemon processes.

George Mansoor
  • 121
  • 1
  • 2
  • 7