package web import "context" // StartWorker registers ownership before launching a worker. Shutdown excludes // admission under the same lock, then waits through output/history persistence. func (jm *JobManager) StartWorker(jobID string, work func()) bool { jm.mu.Lock() if jm.closing { jm.mu.Unlock() jm.SetError(jobID, context.Canceled, "Server shutdown started before the worker could run") return false } jm.workers.Add(1) jm.mu.Unlock() go func() { defer jm.workers.Done() work() }() return true } func (jm *JobManager) stopAdmissionAndCancel() { jm.mu.Lock() defer jm.mu.Unlock() jm.closing = true for _, job := range jm.jobs { // A committed operation must finish or roll back and persist its result. // Cancelling a package manager mid-transaction is not a safe shutdown. if !job.commitStarted && job.cancel != nil { job.cancel() job.Status = "cancelling" job.Message = "Server shutting down; waiting for the worker to stop" } } }