Introducing a new way to review your PRs.

Read the announcement

keephq / keep

The open-source AIOps and alert management platform

View on GitHub

22 documents · Updated Nov 23, 2026

docs / asynchronous-jobs-and-event-processing

Asynchronous jobs and event processing

Keep moves work that does not need to finish inside an HTTP request into background runtimes. ARQ workers handle queued alert, incident, topology, and maintenance work through Redis. Provider consumers and the workflow scheduler handle other event streams and scheduled work so the API can accept events and return while processing continues. For the request boundary that creates these jobs, see API route domains. This page is a child of API and background runtime.

Background processing model

Keep has two main paths for work outside request handling. Redis-backed deployments put selected tasks in ARQ queues. Deployments without Redis use in-process background tasks for some request-triggered work, while provider consumers and the workflow scheduler still run as server services.

Runtime roles

Each runtime owns a different kind of delay or event source. The API accepts work and records a task name or job ID. A worker or service then performs the work and updates the systems that depend on it.

RuntimeMain responsibilityWork source
ARQ workerRun durable queued tasks and a periodic watcherRedis queues and the ARQ cron hook
Event subscriberRead events from installed consumer providersKafka, Amazon SQS, or another provider consumer
Workflow schedulerMatch alerts and incidents to workflows and run themIn-memory event queue and database interval checks
Server jobs processRun service-level loops that are separate from API request workersLocal API readiness and long-lived reporting or demo threads

References: keep/…/arq_worker.py• L32-45 keep/…/event_subscriber.py• L52-75 keep/…/workflowscheduler.py• L108-124 keep/server_jobs_bg.py• L12-40

Redis queue boundary

When Redis is enabled, the API creates an ARQ Redis pool and places work on named queues. The basic-processing queue carries alert, incident, and topology tasks. The maintenance queue carries the watcher task. The API returns a successful acceptance response after enqueueing, not after the task finishes.

When Redis is disabled, alert processing uses the API's in-process background task set and incident processing uses the web framework's background task mechanism. This fallback keeps the request path asynchronous, but it does not provide the same separate-worker boundary as ARQ.

References: keep/…/arq_pool.py• L1-6 keep/…/alerts.py• L621-655 keep/…/alerts.py• L748-781 keep/…/incidents.py• L777-807 keep/…/consts.py• L36-52

ARQ worker runtime

The ARQ runtime is the Redis-backed execution service for background tasks. keep/…/arq_worker.py defines the worker settings and task registration. keep/…/arq_worker_gunicorn.py runs that worker under Gunicorn and gives it a health endpoint and process supervision.

Worker pools and queues

The worker selects its task set from the task-pool setting. The all pool enables all registered basic tasks. The basic_processing pool enables the same event, topology, and incident processing group. A worker listens to the queue selected by the Gunicorn entry point, so a task can remain pending if no live worker listens to its queue.

The worker also creates a thread pool for synchronous event processing. This keeps the ARQ event loop available while the alert pipeline performs database and provider work. Result retention and job expiry are configurable, which affects how long operators can inspect completed or late jobs.

References: keep/…/arq_worker.py• L32-65 keep/…/arq_worker.py• L69-121 keep/…/arq_worker.py• L125-160 keep/…/arq_worker.py• L169-195 keep/…/arq_worker_gunicorn.py• L33-46

Worker lifecycle

The worker starts the workflow manager before it consumes jobs. It uses a per-process health key in Redis and a local heartbeat file for health checks. Gunicorn starts the worker loop, serves the health check on its socket, and cancels the ARQ task during shutdown.

Redis connection errors and other worker errors cause a logged three-second wait before another run. The Gunicorn wrapper can exit the process after the configured error count so the process manager can restart it. A clean cancellation closes the ARQ worker and its event-processing thread pool.

References: keep/…/arq_worker.py• L125-140 keep/…/arq_worker.py• L186-245 keep/…/arq_worker_gunicorn.py• L49-103 keep/…/arq_worker_gunicorn.py• L140-200 keep/…/arq_worker_gunicorn.py• L222-329

Queued task flows

Queued tasks turn accepted events into work that can finish after the request. The task receives tenant and provider context with the event, then applies the same business pipeline that stores, enriches, indexes, and forwards the result.

Alert and incident processing

Alert routes validate the provider and parse the request before they enqueue process_event_in_worker on the basic queue. Incident routes format the incident and enqueue async_process_incident on the same queue. Without Redis, these routes call the corresponding processing functions through local background execution.

The event task normalizes the input and then applies provider formatting, maintenance-window filtering, deduplication, database writes, enrichment, search indexing, workflow matching, rules, and client notifications. For batch input, provider formatting expands each item into the shared alert list. Already parsed alerts receive the provider's custom deduplication rule before that list enters the shared pipeline. The task can therefore change several downstream views from one accepted event. Errors in optional downstream actions such as search or client notification are logged and do not always fail the whole event task.

References: keep/…/alerts.py• L598-655 keep/…/alerts.py• L685-781 keep/…/incidents.py• L772-807 keep/…/process_event_task.py• L661-823

Topology processing

Topology work replaces the stored topology for one provider and tenant with the new service and dependency graph. It then updates related applications and sends a client update when the client push service is available. The task is registered with the same basic-processing worker group as alert and incident tasks.

References: keep/…/arq_worker.py• L40-49 keep/…/process_topology_task.py• L22-37 keep/…/process_topology_task.py• L39-163

Maintenance watcher

The watcher runs maintenance-window recovery and dismissal-expiry checks on a fixed cadence. In a Redis deployment, ARQ invokes it as a cron task on the maintenance queue. In a non-Redis deployment, the service starts a local loop instead.

Only one watcher run is allowed at a time. Redis deployments use a Redis lock. Non-Redis deployments use a file lock. The lock prevents overlapping recovery work when more than one runtime can start the watcher.

References: keep/…/arq_worker.py• L150-160 keep/…/process_watcher_task.py• L13-42 keep/…/process_watcher_task.py• L43-66 keep/…/api.py• L164-188

Event subscriber runtime

The event subscriber converts provider-side message streams into Keep alert events. It discovers installed consumer providers and starts each consumer in its own thread. A provider consumer reads its external source and sends normalized alerts back to the alert event endpoint, which puts them through the normal asynchronous processing path.

Consumer lifecycle

The singleton EventSubscriber starts once during API startup when consumer support is enabled. Provider installation adds a consumer at runtime. Provider deletion stops the matching consumer. Shutdown stops every consumer and joins its thread before the service finishes.

The subscriber exposes each provider's status for health and operational views. It does not itself decide how an alert is stored or matched to a workflow. Those actions happen after the consumer sends the event to the alert intake path.

References: keep/…/event_subscriber.py• L8-31 keep/…/event_subscriber.py• L33-75 keep/…/event_subscriber.py• L77-102 keep/…/api.py• L142-153 keep/…/api.py• L209-217 keep/…/status.py• L1-22

Provider message handling

Consumer providers own the details of reading and acknowledging their external messages. Kafka polls records and sends each value to the API. Amazon SQS builds an alert from each message and deletes the message only after the alert push succeeds. A provider error can stop a consumer loop or leave a message for later redelivery, depending on the provider's handling rules.

References: keep/…/base_provider.py• L739-769 keep/…/base_provider.py• L771-838 keep/…/kafka_provider.py• L228-275 keep/…/amazonsqs_provider.py• L315-361

Workflow scheduling integration

The workflow manager is started with the API when scheduling is enabled and is also started before an ARQ worker begins consuming jobs. It evaluates completed alert and incident events, places matching workflow runs into the scheduler, and uses a worker pool to run them outside the request. For workflow run behavior, see workflow execution engine.

Event-triggered workflows

After event processing saves and enriches alerts, it passes them to WorkflowManager. The manager evaluates enabled alert triggers and their filters, then adds matching runs to the scheduler's in-memory queue. Incident changes use a parallel path that adds incident-triggered runs to the same queue.

The scheduler drains that queue and starts workflow runs in its executor. It records a workflow execution before the run when possible, refreshes enrichment for queued retries, and stores the final result. The current event path uses this in-process scheduler queue rather than the planned ARQ workflow queue.

References: keep/…/process_event_task.py• L538-577 keep/…/workflowmanager.py• L124-176 keep/…/workflowmanager.py• L287-312 keep/…/workflowmanager.py• L526-596 keep/…/workflowscheduler.py• L408-447 keep/…/workflowscheduler.py• L576-642

Interval workflows

The scheduler checks the database for workflows whose interval is due. It loads each workflow, records provider-configuration or load errors as failed executions, and submits valid runs to the same executor used for event-triggered workflows. Periodic timeout checks mark executions that run too long as errors.

References: keep/…/workflowscheduler.py• L115-182 keep/…/workflowscheduler.py• L184-282 keep/…/workflowscheduler.py• L649-672

Workflow execution outcomes

Workflow runs end in success, error, or providers-not-configured status. A run can report step errors even when the scheduler itself stays alive. The workflow manager can run an on_failure action and the scheduler can send a failure email when a new failure follows a non-failing prior run.

Workflow collision behavior depends on the workflow strategy. A retrying non-parallel workflow is returned to the scheduler queue. A non-parallel workflow without retry is marked as an error when the same fingerprint is already running. This behavior prevents concurrent runs from silently changing the same external target.

References: keep/…/workflowscheduler.py• L41-45 keep/…/workflowscheduler.py• L497-570 keep/…/workflowscheduler.py• L710-763 keep/…/workflowmanager.py• L637-715

Server jobs

keep/server_jobs_bg.py is a separate long-lived process for server-level jobs that should not share the API process. It waits for the local API to become ready, starts the configured demo-mode and uptime-reporting threads, and keeps those threads joined until they stop.

This process is different from ARQ. It does not consume Redis jobs and it does not replace the event subscriber or workflow scheduler. Operators must start it alongside the server when these server-level jobs are required.

References: keep/server_jobs_bg.py• L12-40 keep/server_jobs_bg.py• L43-48

Failure and retry behavior

Background work uses several failure policies because its sources have different delivery guarantees. ARQ retries task failures through Retry, service loops keep their own locks and stop rules, and workflow failures become recorded execution outcomes instead of crashing the scheduler.

ARQ task retries

Event and incident tasks save the raw input as an error alert when processing fails. When ARQ supplies a job context, the task raises Retry with a delay based on the current job attempt. When the same function runs without ARQ context, it does not request an ARQ retry.

The event task returns early for malformed or unsupported input in several cases. A provider can return no event for a message that should be skipped. A string payload is treated as malformed and is skipped. These cases are different from an exception that enters the retry path.

References: keep/…/process_event_task.py• L62-77 keep/…/process_event_task.py• L744-765 keep/…/process_event_task.py• L801-830 keep/…/process_incident_task.py• L11-12 keep/…/process_incident_task.py• L132-148

Consumer failures

Consumer threads log provider errors and follow provider-specific stop behavior. Kafka exits its consume loop after a polling error. SQS logs a message error and does not delete that message in the failure branch, allowing the queue service to apply its own visibility and redelivery rules. The subscriber does not provide a general restart loop for a thread that has stopped.

References: keep/…/event_subscriber.py• L33-50 keep/…/kafka_provider.py• L244-271 keep/…/amazonsqs_provider.py• L323-358

Scheduler failures

The workflow scheduler catches errors while loading or running workflows and records an error execution. A failed provider configuration gets a distinct providers-not-configured status. Errors in the scheduler loop are logged and the loop continues, while long-running executions are later marked as timed out.

References: keep/…/workflowscheduler.py• L136-172 keep/…/workflowscheduler.py• L231-279 keep/…/workflowscheduler.py• L374-406 keep/…/workflowscheduler.py• L649-672

Change checklist

When these files or sections are changed, remember to consider these:

  • A new ARQ task needs a registered function, a task-pool and queue decision, and a worker that listens to that queue.
  • A change to an enqueue call must stay aligned with the worker function name, argument order, tenant context, and Redis fallback path.
  • A change to event processing must preserve the handoff to workflow matching and client notifications after database processing.
  • A change to consumer start or stop behavior must preserve provider installation, deletion, startup, shutdown, and status reporting.
  • A change to retry or lock timing must be checked against ARQ job expiry and the watcher cadence so work is not dropped or run twice.
  • A change to workflow execution storage or collision rules must preserve the status and retry behavior used by the scheduler.

Important failure modes

TriggerConsequenceGuard
Redis is enabled but no worker listens to the selected queueThe API returns acceptance while the job remains pendingMatch the task pool and queue name with a live ARQ worker
Redis connection errors exceed the configured worker error limitThe ARQ process exits and relies on Gunicorn or another supervisor to restart itMonitor worker health and the heartbeat endpoint
Event processing raises with ARQ contextThe raw input is stored as an error alert and ARQ receives a delayed retry requestInspect the job attempt and the saved error alert
A Kafka poll or consumer operation failsThe Kafka consumer thread leaves its consume loopCheck consumer status and restart or re-register the provider
An SQS message fails before deletionThe message is not acknowledged by this code and can be delivered againMake event handling safe for repeated delivery and inspect queue redelivery settings
Two non-parallel workflows collide on one eventRetry strategy requeues the event, while the non-retry strategy records an errorSelect the workflow strategy that matches the target system's concurrency needs
A workflow provider is missing or invalidThe execution is recorded as providers-not-configured and is not runConfigure the provider before expecting the workflow to execute
A watcher run overlaps another runThe second run skips because of the Redis or file lockKeep the lock lifetime longer than the watcher interval

Key files

FileWhy to read it
keep/…/arq_worker.pyDefines ARQ task registration, worker settings, queue selection, cron work, and reconnect behavior.
keep/…/arq_worker_gunicorn.pyShows how the worker runs as a supervised process and reports health.
keep/…/arq_pool.pyShows the shared Redis pool used to enqueue ARQ jobs.
keep/…/alerts.pyShows how generic and provider alert events cross from HTTP into background work.
keep/…/incidents.pyShows the incident enqueue path and the non-Redis fallback.
keep/…/process_event_task.pyDescribes the main event pipeline, batch input handling, and its retry boundary.
keep/…/process_incident_task.pyShows incident persistence and retry behavior.
keep/…/process_watcher_task.pyShows periodic maintenance work and overlap protection.
keep/…/event_subscriber.pyDefines consumer discovery, lifecycle, and status tracking.
keep/…/base_provider.pyDefines the provider consumer contract and the alert handoff to the API.
keep/…/workflowmanager.pyConnects processed events to workflow trigger evaluation.
keep/…/workflowscheduler.pyRuns event and interval workflow executions and records their outcomes.
keep/server_jobs_bg.pyDefines the separate process for long-lived server jobs.