Skip to Content
Alzo

packages/config/src/examples/airflow.config.ts

/** * Worked example — one image, several roles. * * Type-checked local configuration/parity example. Loader and graph tests do not * certify an actual Airflow deployment or support on every execution target. * * Modelled on Apache Airflow's published Compose file * (`airflow-core/docs/howto/docker-compose/docker-compose.yaml`, 337 lines, 10 * services). It was chosen because it is the shape Alzo handles best and the * shape it handled *worst* until recently: * *   - Six of the ten services are the **same image**, differing only by argv. *     Until `provision.args` existed this file was not merely verbose here, it *     was inexpressible. *   - `airflow-init` is a one-shot container that six services wait on. Compose *     needs `depends_on: { condition: service_completed_successfully }`; here it *     is `defineTask` plus `.done`, which is vocabulary that already existed. *   - `dags/`, `logs/` and `plugins/` are **bind mounts** to host paths the user *     edits. There is no named-volume substitute; the whole point is that your *     DAGs live in your repository. * * The Compose original leans on a `x-airflow-common` YAML anchor merged into * eight services. `commonEnv` below is the same idea and is explicitly fine — * the anti-DRY stance covers structural fields, not seventy environment * variables (D124). */import { defineConfig, defineIngress, defineService, defineTask, secret } from 'alzo';import type { Node } from 'alzo';const IMAGE = 'apache/airflow:3.1.0';const local = defineIngress({ hostname: 'example.dev', tls: 'internal' });function graph(): Node[] {  // ---------------------------------------------------------------------------  // Infrastructure  // ---------------------------------------------------------------------------  const pgPassword = secret('POSTGRES_PASSWORD');  const postgres = defineService({    name: 'postgres',    ports: { pg: 5432 },    group: 'infrastructure',    provision: { image: 'postgres:16' },    env: { POSTGRES_USER: 'airflow', POSTGRES_PASSWORD: pgPassword, POSTGRES_DB: 'airflow' },    // A Postgres address is not an HTTP URL, so the derived per-port record is    // the wrong shape. Any non-HTTP service needs `outputs` to say what its    // address actually looks like.    outputs: ({ hostname, port }) => ({      connectionString: `postgresql+psycopg2://airflow:${pgPassword}@${hostname}:${port.pg}/airflow`,    }),    volumes: { data: { path: '/var/lib/postgresql/data', onRemove: 'retain' } },    // Correctness, not tuning: Postgres fails on the 64mb default under load,    // and six of the projects surveyed set this.    resources: { shmSize: '256mb' },    ready: { exec: 'pg_isready -U airflow', in: 'container', period: '5s' },    restart: { policy: 'always' },  });  const redis = defineService({    name: 'redis',    ports: { redis: 6379 },    group: 'infrastructure',    provision: { image: 'redis:7' },    outputs: ({ hostname, port }) => ({ connectionString: `redis://${hostname}:${port.redis}/0` }),    ready: { exec: 'redis-cli ping', in: 'container', period: '5s' },    restart: { policy: 'always' },  });  // ---------------------------------------------------------------------------  // Shared configuration  //  // The Compose file's `x-airflow-common` anchor, as an ordinary object.  // ---------------------------------------------------------------------------  const commonEnv = {    AIRFLOW__CORE__EXECUTOR: 'CeleryExecutor',    AIRFLOW__CORE__FERNET_KEY: secret('AIRFLOW_FERNET_KEY'),    AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION: 'true',    AIRFLOW__CORE__LOAD_EXAMPLES: 'false',    AIRFLOW__API__EXPOSE_CONFIG: 'true',  };  const commonVolumes = {    dags: { from: './dags', to: '/opt/airflow/dags' },    logs: { from: './logs', to: '/opt/airflow/logs' },    plugins: { from: './plugins', to: '/opt/airflow/plugins', readOnly: true },  };  // Not a plain object: `postgres.outputs.connectionString` cannot be read until allocation, so the  // reference has to live inside a callback. A zero-argument function is a  // perfectly good `(self) => EnvMap`.  const withBackends = () => ({    ...commonEnv,    AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgres.outputs.connectionString,    AIRFLOW__CELERY__BROKER_URL: redis.outputs.connectionString,  });  // ---------------------------------------------------------------------------  // One-shot initialisation  //  // Compose expresses this as a service with `restart: "no"` that six others  // gate on with `condition: service_completed_successfully`. Here it is a task,  // and `.done` already means "exited 0" — no new vocabulary.  //  // Note `provision` rather than `run`: `airflow` lives in the image, not on  // anyone's PATH.  // ---------------------------------------------------------------------------  const init = defineTask({    name: 'init',    group: 'infrastructure',    provision: { image: IMAGE, args: ['db', 'migrate'] },    env: withBackends,    volumes: commonVolumes,    dependsOn: [postgres.available, redis.available],  });  // ---------------------------------------------------------------------------  // Six roles, one image  //  // This is the block that could not be written before `provision.args`. In the  // Compose original each of these is `<<: *airflow-common` plus one `command:`  // line; the shape is identical, and the argv is the only difference.  // ---------------------------------------------------------------------------  const apiserver = defineService({    name: 'apiserver',    ports: { api: { port: 8080, ingress: local } },    group: 'airflow',    provision: { image: IMAGE, args: ['api-server'] },    env: withBackends,    volumes: commonVolumes,    dependsOn: [postgres.available, redis.available, init.done],    // Re-running `init` changes the schema underneath every role, and their    // ORM metadata and connection pools are stale afterwards (D126).    restartOn: [init.done],    ready: { http: { path: '/api/v2/version', on: 'api' }, initialDelay: '10s' },    restart: { policy: 'always' },  });  const scheduler = defineService({    name: 'scheduler',    ports: { health: { port: 8974, internal: true } },    group: 'airflow',    provision: { image: IMAGE, args: ['scheduler'] },    env: withBackends,    volumes: commonVolumes,    dependsOn: [postgres.available, redis.available, init.done],    restartOn: [init.done],    ready: { http: { path: '/health', on: 'health' }, initialDelay: '10s' },    restart: { policy: 'always' },  });  const dagProcessor = defineService({    name: 'dag-processor',    group: 'airflow',    provision: { image: IMAGE, args: ['dag-processor'] },    env: withBackends,    volumes: commonVolumes,    dependsOn: [postgres.available, init.done],    restartOn: [init.done],    ready: { exec: 'airflow jobs check --job-type DagProcessorJob --hostname "$(hostname)"' },    restart: { policy: 'always' },  });  const worker = defineService({    name: 'worker',    group: 'airflow',    provision: { image: IMAGE, args: ['celery', 'worker'] },    env: withBackends,    volumes: commonVolumes,    dependsOn: [postgres.available, redis.available, init.done],    restartOn: [init.done],    ready: {      exec: 'celery --app airflow.providers.celery.executors.celery_executor.app inspect ping',    },    restart: { policy: 'always' },  });  const triggerer = defineService({    name: 'triggerer',    group: 'airflow',    provision: { image: IMAGE, args: ['triggerer'] },    env: withBackends,    volumes: commonVolumes,    dependsOn: [postgres.available, init.done],    restartOn: [init.done],    ready: { exec: 'airflow jobs check --job-type TriggererJob --hostname "$(hostname)"' },    restart: { policy: 'always' },  });  // Compose puts Flower behind `profiles: [flower]`. A whole named graph would  // be too much for one optional dashboard; `start: 'manual'` keeps it in the  // graph — named, ordered, visible — without running it (D58).  const flower = defineService({    name: 'flower',    ports: { ui: { port: 5555, ingress: local, label: 'Flower' } },    group: 'airflow',    provision: { image: IMAGE, args: ['celery', 'flower'] },    env: withBackends,    dependsOn: [redis.available, worker.available],    start: 'manual',    ready: { http: { path: '/', on: 'ui' }, initialDelay: '10s' },  });  return [apiserver, scheduler, dagProcessor, worker, triggerer, flower];}export default defineConfig({  name: 'airflow',  requires: [{ command: 'docker', hint: 'Install Docker Desktop' }],  logFormat: 'auto',  nodes: graph,});