|
| 1 | +import os |
| 2 | +from datetime import datetime, timedelta, timezone |
| 3 | +from airflow import DAG |
| 4 | +from airflow.providers.amazon.aws.operators.ecs import EcsRunTaskOperator |
| 5 | + |
| 6 | +from airflow.operators.latest_only import LatestOnlyOperator |
| 7 | +from utils.slack import on_failure_callback |
| 8 | + |
| 9 | +default_args = { |
| 10 | + "owner": "airflow", |
| 11 | + "depends_on_past": False, |
| 12 | + # the start_date needs to be less than the last cron run |
| 13 | + "start_date": datetime.now(tz=timezone.utc) - timedelta(hours=3), |
| 14 | + "retries": 2, |
| 15 | + "retry_delay": timedelta(minutes=1), |
| 16 | + "max_active_runs": 10, |
| 17 | + "concurrency": 10, |
| 18 | + "max_active_tasks": 10, |
| 19 | +} |
| 20 | + |
| 21 | +env = os.getenv("ENVIRONMENT", "development") |
| 22 | +subnet = os.getenv("ECS_SUBNET") |
| 23 | +security_group = os.getenv("ECS_SECURITY_GROUP") |
| 24 | +cluster = f"Nowcasting-{env}" |
| 25 | + |
| 26 | +# Tasks can still be defined in terraform, or defined here |
| 27 | + |
| 28 | +region = "uk" |
| 29 | + |
| 30 | +with DAG( |
| 31 | + f"{region}-cloudcasting", |
| 32 | + schedule_interval="5,20,35,50 * * * *", |
| 33 | + default_args=default_args, |
| 34 | + concurrency=10, |
| 35 | + max_active_tasks=10, |
| 36 | +) as dag: |
| 37 | + dag.doc_md = "Run Cloudcasting app" |
| 38 | + |
| 39 | + latest_only = LatestOnlyOperator(task_id="latest_only") |
| 40 | + |
| 41 | + cloudcasting_forecast = EcsRunTaskOperator( |
| 42 | + task_id=f"{region}-cloudcasting", |
| 43 | + task_definition="cloudcasting", |
| 44 | + cluster=cluster, |
| 45 | + overrides={}, |
| 46 | + launch_type="FARGATE", |
| 47 | + network_configuration={ |
| 48 | + "awsvpcConfiguration": { |
| 49 | + "subnets": [subnet], |
| 50 | + "securityGroups": [security_group], |
| 51 | + "assignPublicIp": "ENABLED", |
| 52 | + }, |
| 53 | + }, |
| 54 | + task_concurrency=10, |
| 55 | + on_failure_callback=on_failure_callback, |
| 56 | + awslogs_group="/aws/ecs/forecast/cloudcasting", |
| 57 | + awslogs_stream_prefix="streaming/cloudcasting-forecast", |
| 58 | + awslogs_region="eu-west-1", |
| 59 | + ) |
| 60 | + |
| 61 | + latest_only >> cloudcasting_forecast >> |
| 62 | + |
0 commit comments