Replacing Composer cron with event-driven rules

· The Nistor team · 5 min read

Most data platforms start with a clock. A Cloud Composer DAG runs at 06:00 because the loads are usually done by then. Usually. On the morning they are not, the mart is built from half the data, and nobody finds out until a report looks wrong.

This tutorial takes one such schedule and replaces it with a Nistor rule that runs when the inputs have landed. Then it replays the rule against past events in Rule Lab, before anything runs for real.

The schedule we start from

Picture one DAG in Cloud Composer. It has a single task that starts a Dataform workflow for a daily sales mart. Its schedule is a cron expression for 06:00. The three warehouse tables the mart reads are loaded overnight by other jobs, at times that drift.

The cron encodes a guess: "the inputs are ready by 06:00". The rule we write will encode the fact instead: "the inputs have changed, and have been quiet for ten minutes".

Step 1: let Nistor see the estate

Nistor's Google Cloud tool reads Composer environments and their DAGs, along with BigQuery, Dataform and the other services of the projects you monitor. The connection service account needs roles/composer.user for Composer, and the BigQuery roles for the tables. The tool also links a DAG task to the BigQuery tables its operator names, so the DAG shows up in lineage next to the tables it touches.

Events come from the audit logs. The monitored_project Terraform module creates log sinks in each project, and the input-logs role matches each BigQuery write to a node in the graph. That is the signal the rule will wait for.

Step 2: write the rule

A rule has four blocks: applies_to (which assets), when (what fires it), if (under which conditions) and then (what it does). Two optional blocks cover lateness. The scheduling concepts page shows a rule like this one, trimmed here to the parts this tutorial uses:

- name: Refresh the daily sales mart after the warehouse loads
  code_id: marts/mart-sales-daily
  priority: 5
  status: active
  applies_to:
    node_type: DataformAction
    node_ids:
      - DataformAction_acme-mart-{ENV}.marts.mart_sales_daily
  when:
    events:
      - origin: ancestors
        ancestor_depth: direct_materialized
        types:
          - BIGQUERY_INSERT
          - BIGQUERY_MERGE
    wait_for_all_ancestors: true
    wait_for_all_ancestors_timeout_seconds: 5400
    wait_for_all_ancestors_timeout_alert: true
  if:
    quiet_period_seconds: 600
  then:
    target:
      same_as_applies_to: true
      scope: node
    do:
      kind: single
      action:
        type: dataform_run
        parameters:
          release: projects/acme-mart-{ENV}/locations/europe-west1/repositories/acme-marts/releaseConfigs/daily
  failsafe:
    enabled: true
    deadline_cron: "30 7 * * *"
    time_zone: Europe/London
  freshness_expectation:
    days: weekdays
    before: "08:00"
    time_zone: Europe/London

Read it against the old cron, block by block.

applies_to picks the Dataform action that builds the mart. {ENV} becomes the environment's name when the rule is evaluated, so one file serves dev, test and prod.

when.events replaces the 06:00 cron. origin: ancestors listens to the nodes the mart reads from. direct_materialized takes the nearest materialised tables upstream, reaching through processing nodes such as Dataform actions. Event types are matched exactly: a rule that lists BIGQUERY_MERGE does not fire on BIGQUERY_INSERT, so list every write type your loads use.

wait_for_all_ancestors is the fan-in. The rule holds until every direct ancestor has produced a new event since the last run. If one source never arrives, the timeout fires the rule after 90 minutes with whatever is ready, and the alert opens an incident naming the sources that were still missing.

if.quiet_period_seconds is the debounce. A load that writes many times fires once, after ten quiet minutes.

failsafe keeps the clock as a safety net, not as the trigger. If no qualifying run happened by 07:30, Nistor runs the action itself. It raises no alert.

freshness_expectation is for people. If the mart is still not fresh by 08:00 on a weekday, Nistor opens a delivery_slo incident. It runs nothing.

The full example on the docs page also sets a run limit, a maximum runtime and retries. Add them once the basic rule behaves.

Step 3: replay it in Rule Lab

Rule Lab, at /scheduler/rule-lab in the console, shows what a draft rule would do before you save it. Paste the rule as JSON, or load an existing one. Nothing is stored until you save.

The page answers four questions.

  • Is it valid? It shows schema errors, and whether the rule matches one already stored, in which case saving updates it.
  • What would it hold? The blast radius lists the nodes the rule would match. For each, it shows the outcome of the priority contest with the rules that already hold it: uncontested, wins, loses or shares.
  • When would it have fired? The replay shows when the rule would have fired over the last 7, 14, 30, 60 or 90 days, for up to three nodes, next to what actually ran.
  • What does it cost? The measured spend of the nodes the rule would win, over the same window. This needs the FinOps module, part of Nistor Enterprise. Without it, the nodes are listed unpriced.

The replay is the part that settles the argument. Line up the rule's firings against the DAG's 06:00 runs over 30 days. On the days the loads were early, the rule fires earlier. On the day a source was late, the rule waits, and the DAG did not.

Validation, blast radius, replay and saving all need the actions:trigger permission.

Step 4: run it beside the DAG

Do not switch the DAG off yet. Run the rule alongside it for a while.

Two settings help. A rule with status: test is evaluated, but its action is only simulated, and it opens no incident. shadow_mode: true keeps a rule running and recording its runs, but stops those runs from opening incidents. The console calls it tuning mode.

When the rule's record matches what you expect, pause the DAG's schedule in Composer and set the rule to active.

Step 5: keep it as code

Rules belong in a repository. nistor rules validate parses the files and checks them against the schema for dev, test and prod, with no API call. It also flags any key the schema does not know, so a typo fails in CI instead of being dropped. nistor rules sync sends the files to Nistor, with --dry-run to see the plan first.

A synced rule is locked. The API refuses to edit or delete it, so the file stays the source of truth.

What changed

Before, a job ran at 06:00 whether or not its inputs were ready. After, it runs when they are, and the clock only steps in if they never come. Late data opens an incident by 08:00, before the first report is read.

The scheduling page shows rules and Rule Lab in the console. The Google Cloud integration lists the executors a rule can run. For freshness promises beyond a single rule, see data trust. The cron to event-driven use case sums up the move.

The full rule reference is in the scheduling docs.

See your platform as one graph, on your laptop. Explore the demo estate, or book a walkthrough on one like yours.