Introducing a new way to review your PRs.

Read the announcement

getredash / redash

Make Your Company Data Driven. Connect to any data source, easily visualize, dashboard and share your data.

View on GitHub

20 documents · Updated Nov 11, 2026

docs / background-jobs-and-scheduling

Background jobs and scheduling

Redash runs work outside the web request through Redis-backed queues and RQ workers. This system accepts query work, runs scheduled maintenance, evaluates alerts, and reports failures without blocking the main application. See Application runtime and service composition for the service boundary, Configuration and organization settings for runtime options, and Data model and persistence lifecycle for the records changed by these jobs.

Worker runtime

The worker runtime is the process that takes queued jobs and runs them in separate work processes. The manage.py rq worker command starts a worker with either named queues or the default queue set. Supervisor keeps the worker process running and provides a health check based on worker presence, heartbeat, and work state. (redash/cli/rq.py#L25-L47, worker.conf#L12-L32)

Worker lifecycle

The worker connects to the shared Redis job store, watches its assigned queues, and records job activity as it runs. Redash adds cancellation state and a hard time limit so a job cannot hold a worker forever when the child process does not stop on its own. (redash/tasks/worker.py#L26-L35, redash/tasks/worker.py#L57-L79, redash/tasks/worker.py#L81-L121)

The worker treats a normal return as success. A failed job, a cancelled job, or a process that ends in an unexpected way is reported as a failure. Queue creation and job start, finish, and failure also produce StatsD metrics, which gives operations a view of load and failure rates. (redash/tasks/worker.py#L38-L46, redash/tasks/worker.py#L62-L79, redash/tasks/worker.py#L175-L199)

References: redash/…/rq.py• L25-47 redash/…/worker.py• L26-35 redash/…/worker.py• L57-79 redash/…/worker.py• L81-121

Worker health

Worker health is checked independently of the work being processed. The health check finds the worker process on the local host, then accepts it as healthy when it is busy, has sent a recent heartbeat, or has no queued work to process. Supervisor runs this check on a fixed event tick and can restart the worker when it fails. (redash/cli/rq.py#L50-L88, worker.conf#L26-L32)

The application exposes queue and worker state for operational use. This state includes queued and running jobs, worker assignments, recent heartbeats, and success or failure counts. (redash/monitor.py#L61-L119)

References: redash/…/rq.py• L50-93 worker.conf• L26-32 redash/monitor.py• L61-119

Task routing

Task routing separates work by its effect and expected load. A queue is a named stream of jobs in Redis; workers may watch all default streams or only the streams assigned to a process. Query work can use a data-source queue, while scheduled queries use the scheduled-query queue so routine refreshes can be isolated from interactive work. (redash/worker.py#L10-L12, redash/tasks/queries/execution.py#L75-L104)

Queue groups

The default worker set contains these groups:

Queue groupPurpose
periodicReceives jobs released by the scheduler.
emailsSends email outside the request path.
defaultHandles general work such as alert checks.
scheduled_queriesRuns scheduled query executions.
queriesRuns interactive query work and related query tasks.
schemasRefreshes data source schemas.

Decorated tasks declare their target queue. A task can therefore move from a scheduler or web request to the queue that matches its work without changing the caller's flow. (redash/worker.py#L10-L12, redash/tasks/alerts.py#L28-L30, redash/tasks/general.py#L14-L15, redash/tasks/general.py#L54-L76)

References: redash/worker.py• L10-12 redash/…/rq.py• L32-47 redash/…/alerts.py• L28-30 redash/…/general.py• L14-15 redash/…/general.py• L54-76

Job admission

Query admission prevents duplicate work for the same query and data source. Redash places a short-lived Redis lock beside the queued job, reuses an active job, and removes the lock when the job is complete, cancelled, or no longer exists. This protects both interactive requests and scheduled refreshes from creating needless parallel executions. (redash/tasks/queries/execution.py#L24-L32, redash/tasks/queries/execution.py#L38-L123)

The admission step carries the user, organization, query, schedule, and queue context with the job. That context lets the worker apply the right access identity and lets later alert and failure work relate back to the source query. (redash/tasks/queries/execution.py#L82-L104, redash/tasks/queries/execution.py#L174-L195)

References: redash/…/execution.py• L24-32 redash/…/execution.py• L38-123 redash/…/execution.py• L174-195

Scheduled query execution

Scheduled query execution keeps saved query results current without a user request. A periodic refresh task finds queries whose schedule is due, submits them to the scheduled-query queue, and records enough state to decide when each query should run again. (redash/tasks/queries/maintenance.py#L84-L117, redash/models/init.py#L593-L646)

Schedule decision

The schedule decision uses the saved schedule, the last known execution or result time, and the query's failure count. It skips disabled schedules, schedules that have ended, and schedules with no usable values. If schedule data cannot be read, the system disables that schedule and reports the error instead of repeatedly submitting bad work. (redash/models/init.py#L601-L646)

The periodic refresh applies saved parameter values and the data source's query limit rules before it submits work. It skips disabled organizations, paused data sources, missing data sources, and globally disabled refreshes. (redash/tasks/queries/maintenance.py#L33-L81, redash/tasks/queries/maintenance.py#L84-L117)

Refresh preparation can fail before a job reaches the query queue. Invalid parameter values or a parameter query that is no longer linked to a data source are recorded as scheduled-query failures, then the refresh is skipped. (redash/tasks/queries/maintenance.py#L55-L72, redash/tasks/queries/maintenance.py#L84-L107)

References: redash/…/__init__.py• L581-590 redash/…/__init__.py• L593-646 redash/…/maintenance.py• L33-117

Query result flow

A worker executes the query through the selected data source and stores the result for later reads. It updates the query's latest result before it asks the alert system to inspect that result, which keeps alert evaluation aligned with the newest stored data. Successful scheduled work clears its failure count. (redash/tasks/queries/execution.py#L197-L263)

The main query request uses the same admission path when no usable cached result exists. The request receives a job description while the worker performs the query, so the web process does not wait for the data source. (redash/handlers/query_results.py#L81-L113, redash/tasks/queries/execution.py#L32-L104)

flowchart LR
    A[Periodic refresh] --> B[Find due queries]
    B --> C[Apply schedule and query rules]
    C --> D[Queue scheduled query]
    D --> E[Run against data source]
    E --> F[Store result and update query]
    F --> G[Queue alert checks]

References: redash/…/maintenance.py• L84-117 redash/…/execution.py• L197-263 redash/…/query_results.py• L81-113

Alert processing

Alert processing starts after a new query result is committed. It evaluates each alert attached to the updated query, changes the alert state when needed, and sends notifications to its subscriptions only when the state change meets the alert's notification rules. (redash/tasks/queries/execution.py#L253-L259, redash/tasks/alerts.py#L28-L54)

Alert state transitions

An alert notifies when its new state differs from its saved state. A rearm period can also permit a new notification while the alert remains triggered. The system saves the new state before delivery so the alert has a durable state even if a destination fails. (redash/tasks/alerts.py#L20-L25, redash/tasks/alerts.py#L34-L44)

An initial transition from unknown to OK does not notify. A muted alert also changes state without sending a notification. Each destination is handled on its own, so one destination error does not stop the other subscriptions from being processed. (redash/tasks/alerts.py#L11-L18, redash/tasks/alerts.py#L46-L54)

References: redash/…/alerts.py• L11-25 redash/…/alerts.py• L28-54 redash/…/execution.py• L253-259

Notification handoff

Alert delivery uses each subscription's destination handler and the organization's base URL. The alert job stays in the general queue while destination-specific work remains behind that subscription boundary. Delivery errors are logged with the alert job context for later diagnosis. (redash/tasks/alerts.py#L11-L18)

References: redash/…/alerts.py• L11-18

Failure reporting

Failure reporting turns scheduled query errors into a bounded user-facing report. A failed scheduled execution increments the query failure count, stores a failure record in Redis for the query owner, and later sends one grouped report for that owner when the periodic report task runs. (redash/tasks/queries/execution.py#L230-L235, redash/tasks/failure_report.py#L68-L95)

Failure aggregation

The report groups repeated failures by query and message. It includes the query name, time, reason, count, and a warning when the query is near the configured report limit. The organization setting must allow these emails, the owner must be active, and the query must remain below the failure limit. (redash/tasks/failure_report.py#L13-L25, redash/tasks/failure_report.py#L33-L65, redash/tasks/failure_report.py#L68-L85)

The failure stream includes both errors from the data source and errors found while preparing a scheduled query. This lets one report path cover failed execution as well as invalid parameters or detached parameter queries that prevent execution. (redash/tasks/queries/execution.py#L230-L235, redash/tasks/queries/maintenance.py#L55-L72)

The report task removes the owner's pending failure records after it prepares the message. It then queues email delivery separately, which keeps report creation independent from the mail service. (redash/tasks/failure_report.py#L27-L65, redash/tasks/general.py#L54-L62)

References: redash/…/execution.py• L230-241 redash/…/maintenance.py• L55-72 redash/…/failure_report.py• L13-25 redash/…/failure_report.py• L27-95 redash/…/general.py• L54-62

Periodic maintenance

The scheduler is a long-running process that owns recurring system work. It creates a stable identity for each periodic definition, keeps unchanged jobs in place, removes jobs that no longer exist, and schedules new or changed jobs on the periodic queue. (redash/tasks/schedule.py#L24-L55, redash/tasks/schedule.py#L94-L116)

Maintenance tasks

The built-in periodic work covers the main housekeeping needs:

WorkSystem result
Query refreshFinds due scheduled queries and submits execution jobs.
Lock cleanupRemoves query locks that point to missing jobs.
Schedule cleanupClears schedules that have passed their end date.
Schema refreshSubmits eligible data sources to the schema queue.
User syncUpdates user activity details.
Failure reportingSends grouped scheduled-query failure emails.
Version checkRuns daily when enabled.
Result cleanupRemoves old unused results when enabled.

Schema refresh checks paused data sources, a Redis blacklist, and disabled organizations before it submits one refresh job per eligible data source. Optional version checks and result cleanup are controlled by settings, while deployments can add periodic jobs through dynamic settings. (redash/tasks/schedule.py#L57-L91, redash/tasks/queries/maintenance.py#L147-L219)

References: redash/…/schedule.py• L24-116 redash/…/maintenance.py• L22-30 redash/…/maintenance.py• L147-219

Scheduler operation

The scheduler command loads the current periodic definitions, reconciles them with Redis, and then runs the scheduler loop. The scheduler interval is short so due work moves to a worker queue without requiring the web process to remain active. (redash/cli/rq.py#L25-L29, redash/tasks/schedule.py#L32-L39)

sequenceDiagram
    participant S as Scheduler
    participant R as Redis
    participant W as Worker
    participant T as Periodic task
    S->>R: Reconcile periodic job definitions
    loop On each interval
        S->>R: Release due job to periodic queue
        W->>R: Claim queued job
        W->>T: Run maintenance task
    end

References: redash/…/rq.py• L25-29 redash/…/schedule.py• L32-55 redash/…/schedule.py• L94-116

Change checklist

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

  • Queue names in task declarations must stay aligned with the queue list used by worker startup. A new queue otherwise receives work that no worker watches.
  • Changes to scheduled-query state or result timestamps must preserve the inputs used by the due-query decision. A mismatch can cause skipped or repeated refreshes.
  • Changes to query completion must keep result persistence before alert enqueueing. Alerts need the committed latest result.
  • Changes to scheduled-query failure fields must preserve the Redis aggregation key and the organization email setting. These form the handoff from failure capture to user reporting.
  • Changes to scheduled-query preparation must keep parameter errors on the failure-reporting path. A skipped refresh must remain visible even when no query job is created.
  • Changes to periodic job definitions must preserve stable job identity. A changed identity can leave old recurring jobs active beside the new ones.
  • Changes to worker process behavior must preserve heartbeat and timeout handling. Health checks and queue capacity depend on both.

Important failure modes

TriggerConsequenceGuard
A query lock points to a job that expired or disappeared.A later query can remain blocked from admission.Query admission removes irrelevant locks, and periodic lock cleanup removes ghost locks.
A scheduled query has invalid schedule data.The system cannot decide when to run it.The schedule is disabled and the error is sent to Sentry.
A scheduled query has invalid default parameters or a detached parameter query.No refresh job is queued, and the saved query stays stale.The preparation error is tracked as a scheduled-query failure and the refresh is skipped.
A worker child process ignores its time limit.The worker can stop serving other jobs.The parent worker applies a hard limit and kills the child after its grace period.
A data source is paused or its organization is disabled.A refresh job would run against an unavailable or inactive source.Refresh selection skips the source before enqueueing work.
An alert destination raises an error.That destination does not receive the notification.The alert state is saved first, and the error is logged without stopping other destinations.
Scheduled queries fail at or above the report limit.Further failure emails are suppressed for that query.The failure counter and configured limit bound report volume.
A worker stops heartbeating while work remains.Supervisor can treat the process as unhealthy and restart it.The health check watches process identity, state, and recent heartbeat.

Key files

FileWhy to read it
redash/…/rq.pyShows worker and scheduler startup plus health checks.
redash/worker.pyDefines the default queue groups and shared job behavior.
redash/…/worker.pyDefines queue admission metrics, cancellation, timeout handling, and worker failure state.
redash/…/schedule.pyDefines periodic work and schedule reconciliation.
redash/…/maintenance.pyConnects periodic maintenance to scheduled query preparation, execution enqueueing, and schema work.
redash/…/execution.pyConnects job admission, query execution, result persistence, alerts, and failure tracking.
redash/…/alerts.pyDefines alert state changes and notification handoff.
redash/…/failure_report.pyDefines scheduled-query failure aggregation and reporting.
worker.confShows process supervision, worker count, queue selection, and health-check wiring.