batch_test.py 2.81 KB
Newer Older
김진영's avatar
김진영 committed
1
2
from datetime import datetime, timedelta
from textwrap import dedent
김진영's avatar
김진영 committed
3
import pendulum
김진영's avatar
김진영 committed
4
from airflow.operators.bash import BashOperator
김진영's avatar
김진영 committed
5
from airflow import DAG
김진영's avatar
김진영 committed
6
from data.gasan_data import gasanData   # import cj-gasan data
김진영's avatar
김진영 committed
7
from dateutil.relativedelta import relativedelta
김진영's avatar
김진영 committed
8
from airflow.models import Variable
김진영's avatar
김진영 committed
9

김진영's avatar
김진영 committed
10
# set timezone
김진영's avatar
김진영 committed
11
12
local_tz = pendulum.timezone("Asia/Seoul")

김진영's avatar
김진영 committed
13
def fail_alert(context):
김진영's avatar
김진영 committed
14
15
    context_data = """
    {
김진영's avatar
김진영 committed
16
17
18
19
20
21
22
23
        "@context": "https://schema.org/extensions",
        "@type":"MessageCard",
        "themeColor":"0072C6",
        "title":"Batch Job Error",
        "summary":"test",
        "sections": [
            {
                "facts":[
김진영's avatar
김진영 committed
24
                    {"name":"■ Exec Time", "value": "%s"},
김진영's avatar
김진영 committed
25
26
                    {"name":"■ Task", "value": "%s"},
                    {"name":"■ DAG", "value": "%s"},
김진영's avatar
김진영 committed
27
                    {"name":"■ Reason", "value": "%s"},
김진영's avatar
김진영 committed
28
                    {"name":"■ Log URL", "value": "%s"},
김진영's avatar
김진영 committed
29
30
31
32
                ]
            }
        ]
    }
김진영's avatar
김진영 committed
33
34
35
36
    """ % (
        context.get('execution_date')+relativedelta(hours=9), 
        context.get('task_instance').task_id, 
        context.get('task_instance').dag_id,
김진영's avatar
김진영 committed
37
        context.get('exception'),
김진영's avatar
김진영 committed
38
        context.get('task_instance').log_url.replace("localhost:8080", Variable.get("AIRFLOW_WEB_URL"))
김진영's avatar
김진영 committed
39
    )
김진영's avatar
김진영 committed
40

김진영's avatar
김진영 committed
41
42
    alert = BashOperator(
        task_id='fail_alert',
김진영's avatar
김진영 committed
43
        bash_command="curl -d \'{data}\' -H \"Content-Type: Application/JSON\" -X POST {teams_url}".format(data=context_data, teams_url=Variable.get("TEAMS_WEBHOOKS_URL"))
김진영's avatar
김진영 committed
44
    )
김진영's avatar
김진영 committed
45
    return alert.execute(context=context)
김진영's avatar
김진영 committed
46

김진영's avatar
김진영 committed
47
48
49
50
with DAG(
    'batch_test',
    default_args={
        'depends_on_past': False,
김진영's avatar
김진영 committed
51
        'email': 'kim-jy@lotte.net',
김진영's avatar
김진영 committed
52
53
        #'on_failure_callback': fail_alert
        'on_success_callback': fail_alert
김진영's avatar
김진영 committed
54
55
    },
    description='Test Batch Job',
김진영's avatar
김진영 committed
56
    schedule_interval='*/1 * * * *',
김진영's avatar
김진영 committed
57
    start_date=datetime(2022, 5, 13, tzinfo=local_tz),
김진영's avatar
김진영 committed
58
    tags=['test'],
김진영's avatar
김진영 committed
59
    catchup=False,
김진영's avatar
김진영 committed
60
) as dag:
김진영's avatar
김진영 committed
61
62
63
    # (Task1) 헬스체크
    health_check = BashOperator(
        task_id='health_check',
김진영's avatar
김진영 committed
64
        bash_command="curl -X GET -v {api_url}/api/v1/core/health \'-H accept: application/json\'".format(api_url=Variable.get("INF_API_URL")),
김진영's avatar
김진영 committed
65
    )
김진영's avatar
김진영 committed
66

김진영's avatar
김진영 committed
67
    # (Task2) gasan 작업 병렬처리
김진영's avatar
김진영 committed
68
    post_gasan_tasks = []
김진영's avatar
김진영 committed
69

김진영's avatar
김진영 committed
70
71
72
    for i, data in enumerate(gasanData):
        post_gasan_task = BashOperator(
            task_id='post_gasan'+str(i+1),
김진영's avatar
김진영 committed
73
            bash_command="curl -X \'POST\' \'%s/api/v1/camera/writeimage\' -H \'Content-Type: application/json\' -d \'{\"id\": \"%s\", \"%s\": \"test\", \"ip\": \"%s\", \"serialNum\": \"%s\", \"camName\": \"%s\"}\'" %(Variable.get("INF_API_URL"), Variable.get("INF_API_ID"), Variable.get("INF_API_PW")data["ip"], data["serialNum"], data["camName"]),
김진영's avatar
김진영 committed
74
        )
김진영's avatar
김진영 committed
75

김진영's avatar
김진영 committed
76
        post_gasan_tasks.append(post_gasan_task)
김진영's avatar
김진영 committed
77

김진영's avatar
김진영 committed
78
    # 작업 순서 정의
김진영's avatar
김진영 committed
79
    health_check >> post_gasan_tasks