Introducing a new way to review your PRs.

Read the announcement

bunkerity / bunkerweb

🛡️ Open-source and cloud-native Web Application Firewall (WAF)

View on GitHub

19 documents · Updated Nov 23, 2026

docs / scheduler-jobs

Scheduler jobs

The scheduler is the control process that runs plugin jobs, keeps job state in the database, and applies the resulting configuration to BunkerWeb instances. It runs once per service installation or container, then watches for changes and performs recurring work. See Configuration orchestration for the wider configuration flow and Autoconf service for automatic service discovery.

Scheduler role

The scheduler connects three system needs: scheduled plugin work, configuration generation, and configuration delivery. A job can update cached data or create files that configuration generation needs. The scheduler then sends the new files to reachable BunkerWeb instances and requests a reload.

Process entrypoints

Linux service entrypoint

The Linux service starts src/…/bunkerweb-scheduler.sh as the bunkerweb-scheduler.service unit. This entrypoint prepares shared paths and environment files, checks and upgrades the database, and starts the scheduler under the nginx user. Systemd restarts the service after an unexpected exit and supports stop, reload, and restart actions.

References: src/…/bunkerweb-scheduler.service• L1-20 src/…/bunkerweb-scheduler.sh• L27-52 src/…/bunkerweb-scheduler.sh• L89-126 src/…/bunkerweb-scheduler.sh• L260-269

Container entrypoint

The container entrypoint performs database preparation before it starts usr/…/main.py. It fails before startup when database migration setup or migration execution fails. During shutdown it signals the child by its PID file or direct process ID, handles a stop that arrives before the child starts, and waits for the child to finish after an interrupted wait. Supervisor enables this path only when SERVICE_SCHEDULER=yes.

References: src/…/scheduler.ini• L1-20 src/…/entrypoint.sh• L8-23 src/…/entrypoint.sh• L46-152 src/…/entrypoint.sh• L156-183

Job scheduling lifecycle

The scheduler follows a repeated lifecycle from startup recovery to job execution and configuration publication.

flowchart TD
    A[Start process] --> B[Connect to database]
    B --> C[Load and validate job definitions]
    C --> D[Run startup jobs]
    D --> E[Generate and publish configuration]
    E --> F[Run due recurring jobs]
    F --> G[Record job results]
    G --> H[Publish changed cache or configuration]
    H --> I{Publication complete?}
    I -- No --> J[Retry with backoff]
    J --> H
    I -- Yes --> F

Job discovery

Jobs are declared by plugins with a name, a job file, a cadence, and a reload rule. The scheduler reads valid declarations from core, external, and Pro plugins. It rejects declarations whose job file leaves the plugin job directory, then loads the current job source when work is due. A dedicated scheduler registry keeps these jobs separate from other scheduled work in the process.

References: src/…/JobScheduler.py• L219-279 src/…/JobScheduler.py• L328-371 src/…/JobScheduler.py• L472-484

Startup execution

At startup, the scheduler waits until the database is initialized and builds its working environment from stored configuration. It restores job cache data, runs the startup job set, and generates configuration before it begins the recurring loop. This order lets jobs create certificates, lists, or other files before those files are used by the published configuration.

References: src/…/main.py• L1208-1277 src/…/main.py• L1424-1459 src/…/main.py• L1493-1579

Recurring execution

The main loop checks due jobs at a short interval and records each run in the database. Non-asynchronous jobs from one plugin run in declaration order, while asynchronous jobs can run in separate worker tasks. The scheduler waits for every task before it cleans up job state or publishes a related change. A failed scheduled job advances to its next cadence instead of remaining due on every loop tick.

The scheduler also compares the job cache before and after a batch. A cache change requests publication even when the job reports success, so jobs that replace or remove files still update the running service.

References: src/…/JobScheduler.py• L486-540 src/…/JobScheduler.py• L624-688 src/…/JobScheduler.py• L736-773

Change publication

The scheduler watches database change markers for plugin settings, instances, external plugins, Pro plugins, and custom configuration. A detected change causes the affected data to be regenerated, jobs to be refreshed when needed, and the result to be sent to reachable instances.

Publication is complete only after file delivery and reload confirmation succeed. If an instance has not confirmed readiness, the scheduler keeps the reload pending and checks it during a later publication cycle. If generation, delivery, or reload fails, the last working configuration stays in service and the retry delay grows up to five minutes. A pending generation also holds publication until the configuration is ready.

References: src/…/JobScheduler.py• L293-326 src/…/JobScheduler.py• L542-616 src/…/main.py• L1582-1630 src/…/main.py• L1689-1739 src/…/main.py• L1839-1963

Scheduler dependencies

Database dependency

The database is the scheduler's source of configuration, plugin metadata, instance state, job history, and job cache records. The scheduler must reach an initialized database before normal work can start. If the database becomes read-only or unavailable, the scheduler avoids work that would create inconsistent state and retries the connection later.

References: src/…/main.py• L1208-1214 src/…/main.py• L1249-1266 src/…/JobScheduler.py• L646-649 src/…/JobScheduler.py• L819-848

Job cache

The job cache provides durable data for jobs that download, create, or maintain files. It has a database copy and a local copy so a restart can restore job state before jobs run. A failed restore marks the cache as unsafe for overwriting, which protects the last known good data from being replaced by incomplete results.

References: src/…/jobs.py• L40-97 src/…/jobs.py• L99-210 src/…/jobs.py• L255-320

BunkerWeb instances

The scheduler uses the instance records in the database to find BunkerWeb APIs and to track reachability. It sends configuration and cache files to reachable instances, requests a configuration test and reload, and updates each instance status from the result. When no instance is reachable, it preserves the current service state and reports the condition instead of treating the change as applied.

References: src/…/main.py• L1270-1277 src/…/main.py• L1604-1679 src/…/main.py• L1727-1737

Runtime dependencies

The scheduler runtime includes the clients needed for certificate work, DNS provider access, and communication with external services. The dependency lock keeps these capabilities available in the scheduler image as provider packages change.

References: src/…/requirements.txt• L1-15 src/…/requirements.txt• L57-100 src/…/requirements.txt• L501-512

Maintenance responsibilities

Database maintenance

The database plugin schedules daily maintenance jobs for job-run history and expired UI sessions. The job-run limit is controlled by DATABASE_MAX_JOBS_RUNS, while session cleanup uses DATABASE_MAX_SESSION_AGE_DAYS. These jobs keep operational history and session data within their intended bounds.

References: src/…/plugin.json• L36-52 src/…/plugin.json• L155-170 src/…/cleanup-excess-jobs-runs.py• L18-30 src/…/cleanup-expired-ui-sessions.py• L19-36

Health and failover

The scheduler marks itself healthy after it completes its initial work. Health checks require both the scheduler PID marker and the health marker. After a successful publication, the scheduler also keeps a failover copy of configuration, custom configuration, and cache data so it can restore the last working state when a later reload cannot be applied.

References: src/…/healthcheck-scheduler.sh• L1-11 src/…/main.py• L1152-1174 src/…/main.py• L1760-1779

Change checklist

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

  • Keep the Linux service and container entrypoints aligned on database migration failure and graceful stop behavior.
  • When changing job declaration fields or cadence values, keep validation, safe job paths, scheduling metadata, and plugin declarations compatible.
  • When changing synchronous or asynchronous job behavior, preserve plugin job order and wait for all tasks before cache cleanup or publication.
  • When changing job outputs or cache rules, check the order of cache comparison, configuration generation, file delivery, reload confirmation, and retry.
  • When changing reload or generation state, keep pending publication ownership consistent between the scheduler and its caller.
  • When changing the scheduler dependency lock, preserve the certificate and DNS provider capabilities used by plugin jobs.

Important failure modes

TriggerConsequenceGuard
Database is not initializedStartup work waits and no normal job loop begins.Startup polls database metadata until initialization succeeds.
Database is read-only or unavailableJobs that need writes do not run.The scheduler checks write access and retries the connection later.
Job declaration points outside its plugin job directoryThe job is not scheduled.Job validation requires a safe relative path.
A job raises an errorThe run is recorded as failed and the scheduler continues with other jobs.The job runner catches job errors and records the named plugin job.
Job changes the cache without reporting a changeThe service could keep stale cache data.Before-and-after cache snapshots request publication when the cache differs.
Reload confirmation remains pendingThe publication stays incomplete.The scheduler keeps the pending responses and checks instance health on a later cycle.
Database migration or migration setup failsThe container scheduler does not start.The entrypoint exits with an error before launching the scheduler.
A stop arrives while the child is starting or waitingThe child could outlive the entrypoint or the wait could end too soon.The entrypoint signals the direct child PID and waits again after an interrupted wait.

Key files

FileWhy to read it
src/…/main.pyMain lifecycle from startup recovery through recurring checks, publication, and failover.
src/…/JobScheduler.pyJob discovery, cadence handling, worker coordination, cache change detection, reload confirmation, and publication decisions.
src/…/entrypoint.shContainer database preparation, startup failure handling, signal forwarding, and process shutdown.
src/…/bunkerweb-scheduler.shLinux service setup and scheduler control actions.
src/…/jobs.pyShared job cache contract used by plugin jobs.
src/…/plugin.jsonDatabase settings and recurring maintenance job declarations.
src/…/requirements.txtLocked runtime support for certificate, DNS, and external service jobs.
src/…/healthcheck-scheduler.shScheduler liveness and readiness checks.