Skip to content

Idle container shutdown (Epic 2/6) Idle sweep - #544

Open
cmyers-mieweb wants to merge 3 commits into
cmyers_idle-shutdownfrom
cmyers_idle-shutdown-sweep
Open

cmyers-mieweb wants to merge 3 commits into
cmyers_idle-shutdownfrom
cmyers_idle-shutdown-sweep

Conversation

@cmyers-mieweb

Copy link
Copy Markdown
Collaborator

Issue: #527

Stacked on #540 (Epic 1/6). Review/merge that first... This diff contains only the sweep commits.

Adds an hourly sweep that stops containers with no proxied traffic for longer than their effective idleTime, records why/when/who stopped them, and ships off by default so admins can review dry-run reports before enabling it.

Idle sweep

  • resources/idle-shutdown: idle rule = now - max(createdAt, startedAt, service.lastAccessedAt…) > idleTime; idleTime = 0 is exempt.
  • Only running containers are evaluated (one clusterResources('lxc') snapshot per node).
  • Stops use the provider stopLxc with a 60 s wait; failures are logged and skipped, and any failure marks the job failed.
  • bin/idle-sweep.js supports --dry-run and --enqueue (inserts a Job unless a sweep is already pending/running). Output lists STOPPED / WOULD STOP / FAILED / SKIPPED with reasons, plus per-owner lines; sweep() returns byOwner for Idle shutdown: notify owners of stopped containers (digest + unsubscribe) #528.

Stop/start state

  • New Containers columns: stoppedReason (idle|manual), stoppedAt, stoppedBy, startedAt.
  • Container.markStopped() / markStarted(); the create and reconfigure jobs call markStarted(), which clears stop state and gives every start a full idleTime window.

Rollout safety

  • New idle_shutdown_enabled setting (Settings → Idle shutdown), default off: every sweep is report-only until an admin enables it.

Packaging

  • contrib/systemd/idle-sweep.{service,timer} (hourly), installed by the Makefile and enabled/disabled by the post-install/pre-remove scripts.

Testing

  • Unit tests for the idle rule; DB-backed sweep tests on a dummy node (stop, skip reasons, exemption, start grace, failure handling, dry run, dedupe); settings API tests.
  • Verified end to end: migrate up/down → --enqueue (dedupes) → job-runner runs the sweep → containers recorded as idle/system → next sweep skips them as offline.

Screenshots

  • Settings → Idle shutdown
image image

cmyers-mieweb and others added 3 commits December 3, 2026 11:10
Add stoppedReason (idle|manual), stoppedAt, stoppedBy and startedAt to
Containers with markStopped()/markStarted() helpers. The create and
reconfigure jobs call markStarted() after starting, clearing stop state
and giving the idle rule a start grace. DummyApi.clusterResources reports
a container as stopped when stoppedAt is set so dev sweeps behave like
production.

Refs #527

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- resources/idle-shutdown: idle rule (max of createdAt, startedAt and
  service lastAccessedAt vs effective idleTime; 0 = exempt), sweep over
  running containers via one status snapshot per node, stopLxc with a
  60s wait, failures logged and skipped, per-owner results for #528.
- bin/idle-sweep.js: run a sweep, --dry-run, or --enqueue a Job unless a
  sweep is already pending/running.
- contrib/systemd/idle-sweep.{service,timer}: hourly enqueue; installed
  and enabled by the package scripts.
- idle_shutdown_enabled setting (default off): the sweep only reports
  until an admin enables it; switch on the Settings page.
- parseArgs accepts bare --flags.

Closes #527

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Refs #527

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cmyers-mieweb
cmyers-mieweb requested review from runleveldev and a balanced review from Copilot December 4, 2026 16:08
@cmyers-mieweb cmyers-mieweb changed the title Cmyers idle shutdown sweep Idle container shutdown (Epic 2/6) Idle sweep Dec 4, 2026
@cmyers-mieweb
cmyers-mieweb added this pull request to stack #545 December 4, 2026 16:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A non-admin account named system can access every sweep report, while grouping, stale-state, and enqueue concurrency flaws risk lost reporting and incorrect or duplicate container stops.

4 open issues
What changed in this PR

Implements the idle-shutdown sweep on top of the existing idleTime resource, with shutdown disabled by default for staged rollout.

Changes:

  • Adds hourly sweep jobs, dry-run reporting and per-owner results.
  • Records stop reasons and start times, with a fresh grace period after manager starts.
  • Adds the admin setting, packaging, tests and documentation.
File Description
mie-opensource-landing/​docs/​developers/​release-pipeline.md Documents packaged sweep units.
mie-opensource-landing/​docs/​developers/​idle-shutdown.md Describes sweep behavior and lifecycle state.
mie-opensource-landing/​docs/​developers/​database-schema.md Documents stop/start fields.
mie-opensource-landing/​docs/​admins/​settings.md Explains the rollout setting.
mie-opensource-landing/​docs/​admins/​core-concepts/​containers.md Adds sweep operation commands.
create-a-container/​utils/​dummy-api.js Reflects persisted stop state.
create-a-container/​utils/​cli.js Parses bare command-line flags.
create-a-container/​utils/​__tests__/​cli.test.js Tests flag parsing.
create-a-container/​routers/​api/​v1/​settings.js Exposes the shutdown setting.
create-a-container/​routers/​api/​v1/​__tests__/​settings-idle-shutdown.api.test.js Tests safe setting defaults and updates.
create-a-container/​resources/​idle-shutdown/​service.js Implements idle evaluation, stopping and enqueueing.
create-a-container/​resources/​idle-shutdown/​repository.js Provides sweep database queries.
create-a-container/​resources/​idle-shutdown/​README.md Maps the sweep module.
create-a-container/​resources/​idle-shutdown/​__tests__/​sweep.test.js Tests sweep execution and bookkeeping.
create-a-container/​resources/​idle-shutdown/​__tests__/​service.test.js Tests idle thresholds and grace periods.
create-a-container/​README.md Documents timer packaging.
create-a-container/​openapi.v1.yaml Defines the shutdown setting contract.
create-a-container/​models/​setting.js Adds the enabled-state lookup.
create-a-container/​models/​container.js Adds stop/start fields and helpers.
create-a-container/​migrations/​20261203000000-add-container-stop-state.js Migrates lifecycle audit columns.
create-a-container/​Makefile Installs sweep systemd units.
create-a-container/​contrib/​systemd/​idle-sweep.timer Schedules hourly sweeps.
create-a-container/​contrib/​systemd/​idle-sweep.service Enqueues sweep jobs.
create-a-container/​contrib/​preremove.sh Disables the timer on removal.
create-a-container/​contrib/​postinstall.sh Enables the timer after installation.
create-a-container/​client/​src/​pages/​settings/​SettingsPage.tsx Adds the shutdown toggle.
create-a-container/​client/​src/​lib/​types.ts Types the shutdown setting.
create-a-container/​bin/​reconfigure-container.js Records starts after reconfiguration.
create-a-container/​bin/​idle-sweep.js Adds sweep CLI and report output.
create-a-container/​bin/​create-container.js Records the initial container start.

Review effort: Balanced

if (active) return { enqueued: false, job: active };
const job = await repo.createJob({
command: dryRun ? `${SWEEP_COMMAND} --dry-run` : SWEEP_COMMAND,
createdBy: STOPPED_BY,
}

function groupByOwner(entries) {
const byOwner = {};
Comment on lines +113 to +114
try {
await stopContainer(container, clients);
Comment on lines +130 to +132
const active = await repo.findActiveJobByCommandPrefix(SWEEP_COMMAND);
if (active) return { enqueued: false, job: active };
const job = await repo.createJob({
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants