Introducing a new way to review your PRs.

Read the announcement

rajnandan1 / kener

Stunning status pages, batteries included!

View on GitHub

19 documents · Updated Nov 23, 2026

docs / background-runtime

Background runtime orchestration

The background runtime starts the recurring work that keeps monitoring data current. It coordinates monitor checks with maintenance event creation, data cleanup, alert work, and notification delivery through Redis-backed workers. It also sets the process network proxy behavior, warns when authentication lacks a safe signing key, and provides grouped shutdown entry points so background workers can stop without leaving active work behind.

Runtime lifecycle

The runtime has a small set of long-lived schedulers and on-demand queue workers. Startup enables the recurring schedulers first. Those schedulers then create or submit work to the queue workers that carry out checks and downstream actions.

Startup sequence

src/…/startup.ts is the entry point for background startup. It sets the process time zone to UTC, applies the process proxy settings after environment loading, warns when KENER_SECRET_KEY is missing or uses a published example value, and starts the main monitor scheduler, the maintenance event scheduler, and the daily cleanup scheduler before it reports the running version. The module can run directly or be imported and called by the server host.

The startup work is asynchronous. Each scheduler must finish its start operation before startup moves to the next one, so a missing Redis connection or another start error can prevent the runtime from reaching its running state. A missing or unsafe secret key produces a warning at startup; authentication operations still require a configured key. When an HTTP proxy variable is set, startup also requires a Node version that supports the process proxy feature; otherwise startup fails before the schedulers begin.

References: src/…/startup.ts• L9-30 src/…/commonController.ts• L7-34 src/…/proxy.ts• L6-43

Runtime work graph

The main scheduler reconciles active monitors with their recurring schedules. Each scheduled monitor job submits a check to the monitor execution queue. The check result then moves through storage and alert processing before notification work reaches the email or other delivery paths.

The following diagram shows the normal path from startup to recurring work and its downstream effects.

flowchart TD
    A[Startup] --> B[Main scheduler]
    A --> C[Maintenance scheduler]
    A --> D[Daily cleanup scheduler]
    B --> E[Monitor schedule jobs]
    E --> F[Monitor execution worker]
    F --> G[Monitor response worker]
    G --> H[Monitoring data and latest value]
    G --> I[Alerting worker]
    I --> J[Incidents and alert notifications]
    I --> K[Subscriber worker]
    K --> L[Email worker]
    C --> M[Maintenance events and notifications]
    D --> N[Expired monitoring data removal]

The scheduler and queue layers share Redis for job state. Worker database work uses a separate database context from web requests, which helps a burst of background activity avoid consuming the web request pool.

References: src/…/appScheduler.ts• L25-76 src/…/cron-minute.ts• L15-27 src/…/monitorExecuteQueue.ts• L120-243 src/…/monitorResponseQueue.ts• L30-60 src/…/q.ts• L13-52 src/…/poolContext.ts• L4-25

Recurring scheduler roles

Each scheduler owns one recurring responsibility. The schedulers use the same queue and worker model, but their time windows and business effects differ.

Monitor schedule reconciliation

The main scheduler runs every 10 seconds. It reads active monitors, removes schedules for monitors that are no longer active, and adds schedules for active monitors that have a cron expression. This keeps the set of recurring checks aligned with current monitor state rather than requiring a restart after each monitor change.

The monitor schedule worker turns each due schedule into a minute-based execution request. Group monitors can add a delay before execution, while all monitor checks use the UTC start of the current minute as their work timestamp.

References: src/…/appScheduler.ts• L28-76 src/…/appScheduler.ts• L86-108 src/…/monitorSchedulers.ts• L19-70 src/…/cron-minute.ts• L15-27

Maintenance event processing

The maintenance scheduler runs at startup and then once each hour. For each active recurring maintenance rule, it looks ahead seven days and creates any missing maintenance events in that window. It skips one-time maintenance because that event is handled when the maintenance is created.

Maintenance event creation is separate from monitor execution. During a check, active maintenance and incident records can overlay the observed result, so the stored monitoring view reflects planned or declared impact as well as the service response. See Incidents and maintenance events for the event lifecycle.

References: src/…/maintenanceScheduler.ts• L21-78 src/…/maintenanceScheduler.ts• L81-106 src/…/maintenanceScheduler.ts• L134-165 src/…/monitorExecuteQueue.ts• L129-199

Data retention cleanup

The cleanup scheduler runs at midnight UTC each day. It reads the site data retention policy, uses a 90-day enabled policy when no valid policy exists, and removes monitoring data older than the selected retention period. A disabled policy causes the job to skip deletion.

The policy is read at job time rather than only at startup. A policy change therefore affects the next cleanup run without a process restart.

References: src/…/dailyCleanup.ts• L17-65 src/…/dailyCleanup.ts• L85-115

Queue worker coordination

The queue layer separates work into short job stages. A shared queue factory gives each stage the Redis connection and common retry behavior. Workers are created when their queue is first used, so downstream workers can start as the runtime produces work instead of requiring a separate startup step for every queue.

The main monitoring path has these responsibilities:

  • The execution worker runs the monitor service and combines its result with incident, maintenance, and default-status data.
  • The response worker stores the result and updates the latest monitoring value.
  • The alerting worker checks configured thresholds and creates or resolves alerts and incidents.
  • The subscriber worker finds event subscribers and creates one email job per recipient.
  • The email worker sends the final message.

Manual monitoring data writes also request alert evaluation after the data is stored. If the alerting queue is unavailable, the write remains successful and the queue failure is logged for later attention.

Jobs use deduplication where a repeated monitor timestamp or event could create duplicate work. Common queue settings retry failed jobs up to three times with increasing delays. Failed jobs remain available for inspection for a limited period defined by each queue or scheduler.

For the detailed queue contracts and scheduler behavior, see Queues and scheduled work. Monitor-specific work is described in Monitor execution and service checks.

References: src/…/q.ts• L13-52 src/…/monitorExecuteQueue.ts• L254-280 src/…/monitorResponseQueue.ts• L71-99 src/…/alertingQueue.ts• L289-350 src/…/subscriberQueue.ts• L90-125 src/…/emailQueue.ts• L68-97 src/…/monitorsController.ts• L132-155

Graceful shutdown

Shutdown is grouped by resource type. The scheduler shutdown entry point closes the main reconciliation worker, monitor schedule worker, maintenance worker, and cleanup worker. The queue shutdown entry point closes the monitor execution, response, alerting, subscriber, and email workers.

Closing a worker stops it from taking new jobs and lets its current worker lifecycle close through the queue system. The runtime host must call the grouped shutdown functions when the process receives its shutdown signal; the startup module itself does not register signal handlers.

sequenceDiagram
    participant Host as Runtime host
    participant S as Scheduler shutdown
    participant Q as Queue shutdown
    participant R as Redis workers

    Host->>S: stop recurring scheduler workers
    S->>R: close scheduler workers
    Host->>Q: stop work queue workers
    Q->>R: close execution and delivery workers
    R-->>Host: workers closed

References: src/…/shutdown.ts• L1-11 src/…/appScheduler.ts• L111-122 src/…/monitorSchedulers.ts• L79-84 src/…/maintenanceScheduler.ts• L167-180 src/…/dailyCleanup.ts• L118-128 src/…/shutdown.ts• L1-13 src/…/monitorExecuteQueue.ts• L283-294 src/…/emailQueue.ts• L99-112

Change checklist

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

  • Keep the startup scheduler set aligned with the scheduler shutdown set. A new long-lived scheduler needs both lifecycle paths.
  • Keep the queue shutdown set aligned with queues that can create workers. A downstream queue must close with its upstream queues.
  • Preserve the UTC contract across scheduler patterns, minute timestamps, maintenance windows, and cleanup timing.
  • If startup proxy setup changes, check both the environment-loading order and the Node version requirement for process-wide outbound requests.
  • If the startup secret check changes, keep the warning behavior aligned with token signing and verification. An absent key must not cause authentication to use a fallback.
  • If a queue job changes its payload or result shape, review every next-stage producer and consumer in the monitoring and notification paths.
  • If manual monitoring writes change, preserve their best-effort handoff to alert evaluation and its failure logging behavior.
  • If worker concurrency or database routing changes, check that background load remains isolated from web request database access.

Important failure modes

TriggerConsequenceGuard
REDIS_URL is missing when a queue or worker is first createdStartup or the first background action fails before the job can runSupply a valid Redis URL before starting the runtime
KENER_SECRET_KEY is missing or uses a published example valueStartup warns, and token or API-key operations remain unavailable or unsafe until a real key is setSet a long, unique secret before enabling authentication
A proxy environment variable is set on an unsupported Node versionStartup fails before any scheduler startsUse a Node version that supports the process proxy feature or remove the proxy variables
An active monitor has no valid cron expressionThe monitor has no recurring schedule and is not checked by the scheduler reconciliationSet a valid monitor cron expression before enabling the monitor
A maintenance recurrence cannot be parsedThe scheduler logs the generation error and does not create events for that maintenance in that runKeep maintenance RRULE data valid and review scheduler error logs
A cleanup policy is malformedCleanup uses the default enabled 90-day policyStore valid policy JSON when changing retention behavior
Alerting queue submission fails after a manual monitoring writeThe monitoring row is saved but alert evaluation is delayed or absentReview the logged queue error and restore alerting queue service
A worker is omitted from its grouped shutdown functionThat worker can continue processing while the host is stoppingUpdate the matching shutdown aggregator whenever a worker is added

Key files

FileWhy to read it
src/…/startup.tsShows the background runtime entry point, proxy setup, secret warning, and scheduler start order
src/…/proxy.tsShows the process-wide proxy contract used during startup
src/…/commonController.tsShows the signing-key checks that protect authentication and API-key operations
src/…/appScheduler.tsShows how active monitors become recurring monitor schedules
src/…/monitorSchedulers.tsShows how due monitor schedules become execution requests
src/…/maintenanceScheduler.tsShows recurring maintenance event generation and its time window
src/…/dailyCleanup.tsShows the daily retention job and its policy behavior
src/…/q.tsShows the shared Redis queue and worker rules
src/…/monitorExecuteQueue.tsShows the monitoring execution stage and result handoff
src/…/monitorResponseQueue.tsShows result storage, latest-value updates, and alert handoff
src/…/alertingQueue.tsShows alert, incident, and notification decisions
src/…/subscriberQueue.tsShows subscriber fan-out into individual email jobs
src/…/emailQueue.tsShows the final notification delivery worker
src/…/monitorsController.tsShows how manual monitoring data reaches alert evaluation without making the data write depend on Redis
src/…/shutdown.tsShows the scheduler shutdown boundary
src/…/shutdown.tsShows the queue worker shutdown boundary