Background Operations

Long-running maintenance goroutines with progress, cancellation, and equipment lockout

What this demonstrates

  • Background goroutines — maintenance runs alongside the simulation tick loop
  • context.Context cancellation — cancel a running maintenance operation cleanly
  • Equipment lockout — pump/valve controls disabled during their maintenance
  • Progress via existing polling — HTMX 1s polling picks up progress automatically (no new endpoints)
  • Single maintenance slot — only one operation at a time (teaching simplicity)

New routes

POST /maintenance/pump   — start pump maintenance (~8s)
POST /maintenance/valve  — start valve inspection (~5s)
POST /maintenance/cancel — cancel running maintenance
POST /maintenance/clear  — dismiss completed/cancelled result

Maintenance goroutine pattern

func (s *Simulation) runMaintenance(ctx context.Context, maintType string) {
    for i, step := range steps {
        select {
        case <-ctx.Done():
            // cancelled
            return
        case <-time.After(1 * time.Second):
            // update progress + log under lock
        }
    }
    // completed
}

The WASM demo below simulates this — JavaScript calls Go maintenance functions directly. Run task go-example:10 to see real HTMX partial updates.


Live Demo (WASM)

Loading WASM...