Illyoung Choi | 3926274 | 2019-07-23 13:28:00 -0700 | [diff] [blame^] | 1 | # Copyright 2019-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """ |
| 16 | Example workflow using Airflow |
| 17 | """ |
| 18 | |
| 19 | |
| 20 | import logging |
| 21 | from datetime import datetime |
| 22 | from airflow import DAG |
| 23 | from airflow.sensors.http_sensor import HttpSensor |
| 24 | from airflow.operators.python_operator import PythonOperator |
| 25 | |
| 26 | log = logging.getLogger(__name__) |
| 27 | |
| 28 | args = { |
| 29 | # hard coded date |
| 30 | 'start_date': datetime(2019, 1, 1), |
| 31 | 'owner': 'iychoi' |
| 32 | } |
| 33 | |
| 34 | dag = DAG( |
| 35 | dag_id='simple_airflow_workflow', |
| 36 | default_args=args, |
| 37 | # this dag will be triggered by external systems |
| 38 | schedule_interval=None |
| 39 | ) |
| 40 | |
| 41 | dag.doc_md = __doc__ |
| 42 | |
| 43 | |
| 44 | def handler(ds, **kwargs): |
| 45 | log.info('Handler is called!') |
| 46 | print(ds) |
| 47 | return |
| 48 | |
| 49 | |
| 50 | def check_http(response): |
| 51 | content = response.text |
| 52 | if len(content) > 0: |
| 53 | log.info('the server responsed http content - %s' % content) |
| 54 | return True |
| 55 | |
| 56 | log.info('the server did not respond') |
| 57 | return False |
| 58 | |
| 59 | |
| 60 | sensor = HttpSensor( |
| 61 | task_id='http_sensor', |
| 62 | # 'http_default' goes to https://google.com |
| 63 | http_conn_id='http_default', |
| 64 | endpoint='', |
| 65 | request_params={}, |
| 66 | response_check=check_http, |
| 67 | poke_interval=5, |
| 68 | dag=dag, |
| 69 | ) |
| 70 | |
| 71 | handler = PythonOperator( |
| 72 | task_id='python_operator', |
| 73 | provide_context=True, |
| 74 | python_callable=handler, |
| 75 | dag=dag |
| 76 | ) |
| 77 | |
| 78 | |
| 79 | sensor >> handler |