Skip to content

Data Import Pipeline ​

Every DataImport is processed by a state machine for its import type. The DataImportManager moves an import from one state to the next. Steps never queue the step after them, so one step can't start before the previous one has really finished: for example, non-detects only run once matching is complete.

Starting an import ​

queue_data_import(data_import) (data_import/tasks.py) starts processing once an import's files are staged. It is called by:

  • the scheduled import tasks (EMIT, UNEP, CarbonMapper, ...)
  • the external API's start-import endpoint (data-pipelines scripts)
  • the tanager_pull management command

It does nothing for an import that is already COMPLETE (no new data was found) or already running. Otherwise it only puts the import in its initial state: run_data_import_manager makes the first transition, so callers like the start-import request never wait on the antivirus check. Locally, where celery beat doesn't run, run make run-data-import-manager in the backend container.

Import types ​

Every definition starts the same way: READY_FOR_PROCESSING waits for the antivirus scan of the uploaded files, then IMPORTING runs the importer. What happens after that depends on the import type.

Import typeSteps after IMPORTINGDefinition
EMISSIONS, EPA_SEPPROCESSING_IMAGES → MATCHING → PROCESSING_NON_DETECTS → COMPLETEemissions/data_import/state_machine.py
INFRASTRUCTURE, data type Infrastructure (sites)UPDATING_SITES → MATCHING → PROCESSING_NON_DETECTS → REMATCHING_AERIAL_IMAGES → COMPLETEinfrastructure/data_import/state_machine.py
INFRASTRUCTURE, data type AerialImagesPROCESSING_IMAGES → MATCHING_AERIAL_IMAGES → COMPLETEsame
INFRASTRUCTURE, data type PipelinesUPDATING_PRESETS → COMPLETEsame
SCENES, NON_DETECTS, EXTRA_DATA→ COMPLETEdefault, data_import/definitions/__init__.py

What the steps do:

  • PROCESSING_IMAGES: processes every plume or aerial image of the import in parallel, and waits until none is PENDING or PROCESSING. Failed images count as done.
  • MATCHING: starts one emission matching run per data batch (emissions) or per site owner (sites), and waits until every run is COMPLETE.
  • PROCESSING_NON_DETECTS: computes non-detects for the batches or sites.
  • UPDATING_SITES, UPDATING_PRESETS: recompute location presets (and site timezones) for the sites or pipelines the import created or updated.
  • MATCHING_AERIAL_IMAGES: matches the processed aerial images to sites.
  • REMATCHING_AERIAL_IMAGES: re-matches the processed aerial images near the sites the import created or updated.

State and status ​

DataImport.state is the state machine state above. The public DataImport.status, shown in the API, is written in the same update:

statestatus
READY_FOR_PROCESSINGREADY_FOR_PROCESSING
IMPORTING and every step after itIMPORTING
COMPLETE, ERROR, TIMED_OUTsame
SAFEGUARD_TRIGGEREDERROR
NO_DATACOMPLETE

COMPLETE means the whole pipeline finished, not just that the rows were saved.

How an import moves ​

A definition is a list of transitions, each source → target with a condition and an optional action:

  1. DataImportManager.advance() checks the transitions out of the import's current state in order, and fires the first one whose condition holds.
  2. Firing writes the new state and status, appends to state_history, and marks the action QUEUED, all in one guarded update. After the commit, the action is sent to the dataimport queue as run_data_import_action.
  3. A worker claims the action (QUEUED → RUNNING), runs it, and records SUCCEEDED or FAILED. Then it calls advance() again straight away.
  4. The first transition, and steps that wait for work running elsewhere (antivirus scan, images, matching runs), are made by run_data_import_manager, which runs every 30 seconds.

While an action is QUEUED or RUNNING, only timeouts can move the import.

run_data_import_manager runs regardless of the enable_periodic_tasks switch. Without it, imports would never leave a step that waits on something.

Failures and timeouts ​

There are no automatic retries. A failed import is reported to Slack and re-run by hand.

What happensResult
An action raises, or runs past the 6h task limitERROR, with import_errors = {"errors": ..., "state": <step>}
A safeguard refuses the data (the importer raises DataImportSafeguardError), e.g. an emission import that would change too many data pointsSAFEGUARD_TRIGGERED, with "safeguard_triggered": true added to import_errors
The antivirus scan flags a fileERROR, "Virus detected in files"
A matching run fails or is timed out by the matching auditERROR, "Emission matching failed"
No worker picks up an action within 60 minutesERROR, "No worker picked up the action."
The import stays in a state too longTIMED_OUT. The error names the state and how its action ended: QUEUED means it was never picked up, RUNNING means it hung or its worker died

State limits: READY_FOR_PROCESSING 3h, IMPORTING 6h, MATCHING 3h, every other step 2h.

Debugging an import ​

Three places show an import's state machine:

  • The admin's Data imports page lists imports with their current step, and opens one in a side panel: a timeline of its steps with durations, its errors and its parameters. Link to an import with /admin/data-imports?import=<id>.
  • Django admin (Data imports): the changelist filters by state (including "Legacy" for imports from before the state machine) and action status. The change page shows a pipeline steps table, the context and errors, and links to the import's data batches and matching runs.
  • The API detail routes (GET /api/v1/data-import/<id>/ internally, and GET /api/v1/external-data-import/<id>/ for the import scripts) return state, state_entered_at, action_status and state_history. Each state_history step has state, entered_at, left_at, duration_seconds and action_status. state is null, with an empty history, for imports from before the state machine.

In a step's action_status, the failed step shows FAILED. The final ERROR, SAFEGUARD_TRIGGERED or TIMED_OUT step shows the status of the reporting action that ran on entering it, so normally SUCCEEDED.

Or in a Django shell:

python
from data_import.models import DataImport
di = DataImport.objects.get(pk="<id>")
di.state, di.status, di.action_status
di.state_history
di.import_errors
di.context
  • state_history: every state with entered_at, and the action_status_on_exit of the state it left.
  • context: what the steps passed on (matching_run_ids, site_ids, aerial_image_ids, pipeline_ids, the import report).
  • Batches: an import's data batches are di.data_batches.all().

How to read action_status while an import is in a step:

action_statusMeaning
QUEUED for a long timeNo worker has picked the action up. Check the dataimport workers and the broker
RUNNINGThe action is running, or its worker died. The state timeout catches the latter
SUCCEEDEDThe action is done, and the step is waiting on its condition. Check the image statuses or the matching runs in context

Adding or changing a definition ​

  • Build a definition with data_import.steps.import_definition(import_type, transitions, step_timeouts). List only the steps after IMPORTING. The builder adds the antivirus check and import, an ERROR exit for each step (unless you define your own), the timeouts and the status map.
  • Register it with register_definition() from your app's AppConfig.ready(). Import types without one get the default.
  • Conditions and actions are functions that take the DataImport. Actions run on a worker, and the claim makes sure each one runs once. To fan out work, queue the tasks in the action and wait for them with a condition that reads the tasks' own status (as PROCESSING_IMAGES does).
  • Pass IDs from the importer to later steps by overriding post_import_context(). Importers skip their post_import_hook for imports a state machine drives.
  • Branch with several transitions out of the same state whose conditions exclude each other. INFRASTRUCTURE branches on data_type after IMPORTING.

Legacy path ​

do_import and the importers' post_import_hook only run for imports queued before the state machine was deployed, and audit_data_import_runs only times out those imports.