Ejemplo n.º 1
0
def slack_dag_failure_notification(context):
    slack_webhook_token = BaseHook.get_connection("slack").password
    icon_color = (":red_circle" if configuration.ENVIRONMENT.lower()
                  == "production" else ":yellow_circle")

    msg = """
          {icon_color}: Task Failed. 
          *Task*: {task}  
          *Dag*: {dag}
          *Execution Time*: {exec_date}  
          *Log Url*: {log_url} 
          """.format(
        icon_color=icon_color,
        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,
    )

    failed_alert = SlackWebhookOperator(
        task_id="slack_failed_notification",
        http_conn_id="slack",
        webhook_token=slack_webhook_token,
        message=msg,
        username="******",
    )

    return failed_alert.execute(context=context)
Ejemplo n.º 2
0
def slack_success_notification(context):
    slack_webhook_token = BaseHook.get_connection("slack").password

    msg = """
          :green_circle: Task Successful. 
          *Task*: {task}  
          *Dag*: {dag} 
          *Execution Time*: {exec_date}  
          *Log Url*: {log_url} 
          """.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,
    )

    success_alert = SlackWebhookOperator(
        task_id="slack_success_notification",
        http_conn_id="slack",
        webhook_token=slack_webhook_token,
        message=msg,
        username="******",
    )

    return success_alert.execute(context=context)
def task_fail_slack_alert(context):
    slack_webhook_token = BaseHook.get_connection(SLACK_CONN_ID).password
    if var_loader.get_git_user() != "cloud-bulldozer":
        print("Task Failed")
        return
    if context.get('task_instance').task_id != "final_status":
        print(context.get('task_instance').task_id, "Task failed")
        return

    slack_msg = """
            :red_circle: DAG Failed {mem} 
            *Task*: {task}  
            *Dag*: {dag} 
            *Execution Time*: {exec_date}  
            *Log Url*: {log_url} 
            """.format(
        task=context.get('task_instance').task_id,
        dag=context.get('task_instance').dag_id,
        mem=alert_members(context),
        ti=context.get('task_instance'),
        exec_date=context.get('execution_date'),
        log_url=get_hyperlink(context),
    )
    failed_alert = SlackWebhookOperator(task_id='slack_test',
                                        http_conn_id='slack',
                                        webhook_token=slack_webhook_token,
                                        message=slack_msg,
                                        username='******',
                                        link_names=True)
    return failed_alert.execute(context=context)
Ejemplo n.º 4
0
def task_fail_slack_alert(context):
    """
    Callback task that can be used in DAG to alert of failure task completion
    Args:
        context (dict): Context variable passed in from Airflow
    Returns:
        None: Calls the SlackWebhookOperator execute method internally
    """
    conection = BaseHook.get_connection(SLACK_CONN_ID)
    slack_webhook_token = conection.password
    slack_msg = """*Status*: :x: Task Failed\n*Task*: {task}\n*Dag*: {dag}\n*Execution Time*: {exec_date}\n*Log Url*: {log_url}""".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,
    )
    if conection.extra_dejson.get('users'):
        slack_msg = slack_msg + '\n' + conection.extra_dejson.get('users')

    failed_alert = SlackWebhookOperator(
        task_id="slack_task",
        http_conn_id="slack",
        webhook_token=slack_webhook_token,
        message=slack_msg,
        username="******",
        link_names=True
    )

    return failed_alert.execute(context=context)
Ejemplo n.º 5
0
def task_fail_slack_alert(context):

    dag_run = context.get('dag_run')
    analysis_id = dag_run.conf.get('analysis_id')
    
    
    if analysis_id:
        print("Fail, updating analysis status")
        gendb_utils.add_analysis_metadata(analysis_id, "airflow_execution_status", "failed", update=True)
        gendb_utils.update_analysis_status(analysis_id, "failed")
    else:
        print("NO ANALYSE NUMBER")

    print("task_fail_slack_alert")
    slack_webhook_token = BaseHook.get_connection(SLACK_CONN_ID).password

    slack_msg = """
            :red_circle: Task Failed. 
            *Task*: {task}  
            *Dag*: {dag} 
            *Execution Time*: {exec_date}  
            *Log Url*: {log_url} 
            *Analysis*: {analysis_id}
            """.format(
            task=context.get('task_instance').task_id,
            dag=context.get('task_instance').dag_id,
            exec_date=context.get('execution_date'),
            log_url=context.get('task_instance').log_url,
            analysis_id=analysis_id,
        )
          
    print("message:\n", slack_msg)
            
    failed_alert = SlackWebhookOperator(
        task_id='slack_test',
        http_conn_id='slack',
        webhook_token=slack_webhook_token,
        message=slack_msg,
        username='******')

    return failed_alert.execute(context=context)
Ejemplo n.º 6
0
def task_success_slack_alert(context):
    
    '''
    Slack message + update analysis status
    '''

    dag_run = context.get('dag_run')
    analysis_id = dag_run.conf.get('analysis_id')

    if analysis_id:
        print("Success, updating db")
        gendb_utils.add_analysis_metadata(analysis_id, "airflow_execution_status", "success", update=True)
        gendb_utils.update_analysis_status(analysis_id, "success")

    print("slack message")
    slack_webhook_token = BaseHook.get_connection(SLACK_CONN_ID).password

    slack_msg = """
            :heavy_check_mark: Dag Success. 
            *Dag*: {dag} 
            *Execution Time*: {exec_date}  
            *Log Url*: {log_url}
            *Analysis ID*: {analysis_id}
            """.format(
            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,
            analysis_id=analysis_id
        )
    
    success_alert = SlackWebhookOperator(
        task_id='slack_test',
        http_conn_id='slack',
        webhook_token=slack_webhook_token,
        message=slack_msg,
        username='******')

    return success_alert.execute(context=context)