# Processing pipeline

# Processing pipeline

Four scheduled jobs run against every tenant and every enabled data source. They run independently; a failure in one does not block the next. Execution order on a shared schedule tick is incoming -> failed -> geolocation -> alerts.

## Pipeline

![mermaid diagram](processing-pipeline.diagram-0.svg)

A report that lands in `failed` stays there until the failed-queue job retries it on its own schedule, with no exponential backoff: each run is one attempt per message.

## The four jobs

1. **Incoming queue** (`SCHEDULE_INCOMING_QUEUE`, default every 12 hours). Fetches unread email from each enabled data source, runs the pipeline above, moves the message into one of the three outcome folders, and writes a `job_runs` row with per-source counts.
2. **Failed queue** (`SCHEDULE_FAILED_QUEUE`, default every 12 hours). Reprocesses whatever is sitting in the `failed` folder. No retry wrapper: one attempt per message per run. Success promotes to `processed`; continued failure leaves the message in place for manual inspection.
3. **Geolocation** (runs after each incoming-queue batch when `IP2LOCATION_DB_PATH` is set). Enriches source IPs on parsed DMARC records in batches of 1000 with country, region, city, and coordinates from the IP2Location LITE DB11 file. Unset `IP2LOCATION_DB_PATH` disables the job entirely.
4. **Alerts generation**. Evaluates the configured rules per tenant (or the three legacy detectors when no rules are defined), writes new alerts to `alerts_sent`, and dispatches each to every enabled channel. SMTP is the only channel wired today.

## Weekly report (optional)

`WEEKLY_REPORT_ENABLED=true` adds a fifth job on `SCHEDULE_WEEKLY_REPORT` (default Mondays at 08:00) that emails a per-tenant seven-day summary.
