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-importendpoint (data-pipelines scripts) - the
tanager_pullmanagement 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 type | Steps after IMPORTING | Definition |
|---|---|---|
EMISSIONS, EPA_SEP | PROCESSING_IMAGES → MATCHING → PROCESSING_NON_DETECTS → COMPLETE | emissions/data_import/state_machine.py |
INFRASTRUCTURE, data type Infrastructure (sites) | UPDATING_SITES → MATCHING → PROCESSING_NON_DETECTS → REMATCHING_AERIAL_IMAGES → COMPLETE | infrastructure/data_import/state_machine.py |
INFRASTRUCTURE, data type AerialImages | PROCESSING_IMAGES → MATCHING_AERIAL_IMAGES → COMPLETE | same |
INFRASTRUCTURE, data type Pipelines | UPDATING_PRESETS → COMPLETE | same |
SCENES, NON_DETECTS, EXTRA_DATA | → COMPLETE | default, 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 isPENDINGorPROCESSING. 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 isCOMPLETE.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:
state | status |
|---|---|
READY_FOR_PROCESSING | READY_FOR_PROCESSING |
IMPORTING and every step after it | IMPORTING |
COMPLETE, ERROR, TIMED_OUT | same |
SAFEGUARD_TRIGGERED | ERROR |
NO_DATA | COMPLETE |
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:
DataImportManager.advance()checks the transitions out of the import's current state in order, and fires the first one whose condition holds.- Firing writes the new
stateandstatus, appends tostate_history, and marks the actionQUEUED, all in one guarded update. After the commit, the action is sent to thedataimportqueue asrun_data_import_action. - A worker claims the action (
QUEUED→RUNNING), runs it, and recordsSUCCEEDEDorFAILED. Then it callsadvance()again straight away. - 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 happens | Result |
|---|---|
| An action raises, or runs past the 6h task limit | ERROR, 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 points | SAFEGUARD_TRIGGERED, with "safeguard_triggered": true added to import_errors |
| The antivirus scan flags a file | ERROR, "Virus detected in files" |
| A matching run fails or is timed out by the matching audit | ERROR, "Emission matching failed" |
| No worker picks up an action within 60 minutes | ERROR, "No worker picked up the action." |
| The import stays in a state too long | TIMED_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, andGET /api/v1/external-data-import/<id>/for the import scripts) returnstate,state_entered_at,action_statusandstate_history. Eachstate_historystep hasstate,entered_at,left_at,duration_secondsandaction_status.stateisnull, 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:
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.contextstate_history: every state withentered_at, and theaction_status_on_exitof the state it left.context: what the steps passed on (matching_run_ids,site_ids,aerial_image_ids,pipeline_ids, the importreport).- Batches: an import's data batches are
di.data_batches.all().
How to read action_status while an import is in a step:
action_status | Meaning |
|---|---|
QUEUED for a long time | No worker has picked the action up. Check the dataimport workers and the broker |
RUNNING | The action is running, or its worker died. The state timeout catches the latter |
SUCCEEDED | The 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 afterIMPORTING. The builder adds the antivirus check and import, anERRORexit for each step (unless you define your own), the timeouts and the status map. - Register it with
register_definition()from your app'sAppConfig.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 (asPROCESSING_IMAGESdoes). - Pass IDs from the importer to later steps by overriding
post_import_context(). Importers skip theirpost_import_hookfor imports a state machine drives. - Branch with several transitions out of the same state whose conditions exclude each other.
INFRASTRUCTUREbranches ondata_typeafterIMPORTING.
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.