Schedule daemon and jobs
The schedule package provides a command-line workflow for saving recurring support-bundle jobs and running them without manual intervention. A job records when to collect and which collection options to use. The daemon checks those jobs once each minute and starts the normal support-bundle command when a job is due.
Scheduling model
The scheduling model separates a durable job definition from the process that executes it. The support-bundle schedule command manages definitions in the user's local Troubleshoot data area. The daemon reads the same definitions, so creating a job and running the daemon are separate steps.
The package supports these job concerns:
| Concern | Role |
|---|---|
| Timing | A five-field cron expression selects the minute, hour, day, month, and weekday for collection. |
| Target | An optional Kubernetes namespace limits the collection target. |
| Discovery | The auto-discovery option asks the normal support-bundle flow to collect foundational data without a supplied spec. |
| Delivery | A non-empty upload option asks the normal flow to upload the resulting bundle to replicated.app. |
| State | The job records whether it is enabled and tracks successful run count and last-run time. |
The schedule package is a child interface of the main CLI. The top-level command adds it as support-bundle schedule, which keeps recurring collection beside the one-shot support-bundle workflow. See scheduled support bundles for the product-level workflow.
References: cmd/…/root.go• L108-115 cmd/…/schedule.go• L8-10 pkg/…/job.go• L13-25
Scheduler commands
The scheduler command exposes job creation, inspection, removal, and daemon start. These commands change or consume the shared job definitions rather than running a collection directly during job creation.
Job creation
support-bundle schedule create creates an enabled recurring job from a name and a required cron expression. The command can also attach a namespace, auto-discovery, and automatic upload to the job. The package checks the job name and accepts only five-field cron expressions with supported field values before it saves the definition.
The command prints the new job ID and the selected options. It also tells the operator to start the daemon because saving a job does not start a background process.
support-bundle schedule create daily-check \
--cron "0 2 * * *" \
--namespace production \
--auto \
--upload enabled
References: pkg/…/cli.go• L32-89 pkg/…/job.go• L47-88 pkg/…/job.go• L160-206
Job listing and removal
support-bundle schedule list reads all saved job definitions and presents their schedule, target namespace, discovery mode, upload mode, and successful run count. support-bundle schedule delete removes a job by its name or ID. Both commands operate on the local job store used by the daemon.
References: pkg/…/cli.go• L91-147 pkg/…/job.go• L91-135
Daemon start
support-bundle schedule daemon start constructs the scheduler and enters its long-running process. The command does not provide a separate stop subcommand. The running process stops when it receives an interrupt or termination signal.
References: pkg/…/cli.go• L150-171 pkg/…/daemon.go• L27-60 pkg/…/daemon.go• L62-101
Job lifecycle
The job lifecycle moves from a saved definition to a due check and then to a support-bundle run. A successful run updates the definition so later checks can avoid duplicate work and the list command can show useful run history.
When the daemon processes a job, it follows this sequence:
- It loads the saved jobs and ignores disabled or invalid entries.
- It compares the current time with every cron field.
- It skips a job that ran within the short duplicate-run cooldown.
- It marks the job as running and starts collection in the background.
- It invokes the support-bundle binary with the job's namespace, discovery, upload, and scheduled-run options.
- It records the run count and last-run time only after the command succeeds.
The following diagram shows the relationship between the CLI, local job state, daemon, and normal support-bundle flow.
flowchart LR
Operator[Operator] --> Create[Schedule create]
Create --> Store[(Saved job definition)]
Operator --> Start[Schedule daemon start]
Start --> Daemon[Scheduler daemon]
Daemon --> Store
Daemon -->|cron match and enabled| Run[Support-bundle command]
Run --> Bundle[Bundle collection]
Run --> Upload[Optional upload]
Run -->|success| Store
Operator --> List[Schedule list]
List --> Store
Operator --> Delete[Schedule delete]
Delete --> Store
References: pkg/…/daemon.go• L103-130 pkg/…/daemon.go• L132-205 pkg/…/daemon.go• L231-326 pkg/…/job.go• L91-157
Support-bundle execution
The daemon does not implement a second collection path. It translates the job into flags for the existing support-bundle binary, then captures the command result. This keeps scheduled collections aligned with normal collection behavior.
The execution mapping is:
| Job setting | Support-bundle behavior |
|---|---|
Namespace | Passes --namespace when set. |
Auto | Passes --auto to enable foundational collector discovery. |
Upload | Passes --auto-upload when non-empty. The normal command uses automatic detection for upload details. |
| Every scheduled run | Passes --auto-update=false so a background run does not update its binary. |
The support-bundle command performs the collection and optional upload. The daemon logs completion, upload results, and archive information. It logs command errors without increasing the successful run count.
References: pkg/…/daemon.go• L231-325 cmd/…/root.go• L135-155 cmd/…/run.go• L263-281 cmd/…/run.go• L390-424
Runtime state and observability
The scheduler uses the user's local Troubleshoot data area for both job definitions and its log. Job definitions persist as separate JSON records under ~/.troubleshoot/scheduled-jobs, while daemon messages append to ~/…/scheduler.log.
The daemon checks jobs at one-minute intervals. It starts due jobs in the background and keeps an in-memory running set per job ID. This prevents the same job from starting again while its prior collection is still active. A short last-run cooldown also protects against duplicate starts around a check boundary.
The daemon writes status to both the process output and the scheduler log. Operators can use the command output and log to distinguish a missing executable, a failed collection, an upload failure, and a successful archive.
References: pkg/…/job.go• L27-45 pkg/…/daemon.go• L17-25 pkg/…/daemon.go• L34-59 pkg/…/daemon.go• L62-95 pkg/…/daemon.go• L237-325 pkg/…/daemon.go• L328-341
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the job fields, CLI flags, and daemon-to-command flag mapping aligned. A new scheduling option is incomplete if the command can save it but the daemon does not pass it to the support-bundle flow.
- Preserve the shared job-store path and JSON shape across create, list, delete, and daemon startup. A path or field change can make existing jobs invisible or change their run history.
- Keep cron validation and cron matching compatible. A schedule accepted during creation must have the same meaning when the daemon checks it.
- Keep successful-run state updates after command success. Moving them earlier can suppress retries while reporting a collection that did not complete.
- Keep the daemon's binary lookup contract aligned with the packaged executable name and deployment layout.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The job name or cron expression is invalid | The job is not saved. | Creation validates the name and five cron fields before writing the job. |
The daemon cannot find the support-bundle executable | The due job fails and its success state does not change. | Place the executable in the current directory, beside the daemon executable, or on PATH. |
| A collection command exits with an error | The daemon logs the error and does not increment the successful run count. | Inspect command output and retry after fixing the collection cause. |
| Automatic upload fails after bundle creation | The bundle remains available locally while the daemon logs the upload failure. | Use the logged archive path and upload the bundle through the normal upload command. |
| The daemon is not running | Saved jobs do not execute. | Start support-bundle schedule daemon start in a process managed for the required uptime. |
| A job definition cannot be parsed | The listing and daemon scan skip that file. | Keep job records as valid JSON and restore the definition from its intended source if it is damaged. |
Key files
| File | Why to read it |
|---|---|
| pkg/…/cli.go | Defines the public scheduler command tree and its operator-facing job and daemon actions. |
| pkg/…/job.go | Defines the durable job model and the shared create, read, list, and delete behavior. |
| pkg/…/daemon.go | Defines the recurring check, duplicate-run protection, support-bundle launch, state update, and logging flow. |
| cmd/…/schedule.go | Connects the schedule package to the top-level support-bundle CLI. |
| cmd/…/run.go | Shows how the normal support-bundle command handles auto-discovery and automatic upload requested by scheduled jobs. |
| pkg/…/schedule_test.go | Shows the package-level guarantees for job persistence operations and basic schedule matching. |