shutdown

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package shutdown manages graceful daemon shutdown and orphan prevention.

On shutdown:

  1. Stop accepting new work
  2. Wait for active workers to finish (up to grace period)
  3. Kill remaining workers
  4. Clean up worktrees
  5. Update state.db
  6. Remove PID file

On startup:

  1. Detect stale workers from previous crash
  2. Kill orphaned claude processes
  3. Clean up abandoned worktrees
  4. Reset worker states in DB

Index

Constants

View Source
const (
	// DefaultGracePeriod is how long to wait for workers to finish.
	DefaultGracePeriod = 60 * time.Second

	// KillTimeout is how long to wait after SIGTERM before SIGKILL.
	KillTimeout = 10 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager handles graceful shutdown and orphan cleanup.

func NewManager

func NewManager(db *state.DB, wm *worktree.Manager, logger *slog.Logger, anvils map[string]string) *Manager

NewManager creates a new shutdown manager.

func (*Manager) CleanupOrphans

func (m *Manager) CleanupOrphans() (cleaned int)

CleanupOrphans detects and cleans up orphaned resources from a previous crash. Call this on daemon startup.

func (*Manager) CleanupWorktrees

func (m *Manager) CleanupWorktrees()

CleanupWorktrees removes all worktrees across all anvils (for full shutdown).

func (*Manager) GracefulShutdown

func (m *Manager) GracefulShutdown() int

GracefulShutdown performs an orderly shutdown of all active workers. Returns the number of workers that had to be forcefully killed.

func (*Manager) RecoverOrphanedBeads

func (m *Manager) RecoverOrphanedBeads() (recovered int)

RecoverOrphanedBeads detects beads with status=in_progress in the beads DB that have no active worker and no open PR in Forge's state DB. These are beads that were claimed but orphaned (e.g., daemon crashed mid-session). Only beads belonging to this Forge's configured anvils are considered, and only beads that Forge has previously claimed (i.e., have a worker record in state.db) are eligible for recovery — beads set to in_progress by humans or external tools are left untouched. This runs both at startup and periodically during normal operation (every 10 poll cycles) so recovery is not limited to crash scenarios. Returns the number of beads recovered.

func (*Manager) SetCrucibleActiveCheck added in v0.3.0

func (m *Manager) SetCrucibleActiveCheck(fn func(beadID, anvil string) bool)

SetCrucibleActiveCheck registers a callback that orphan recovery uses to determine whether a bead ID in a given anvil has an active Crucible run. If the callback returns true the bead is skipped — it is not orphaned, just managed by the Crucible rather than a direct worker row. The anvil parameter scopes the check so that two anvils with the same bead ID are handled independently.

func (*Manager) SetGracePeriod

func (m *Manager) SetGracePeriod(d time.Duration)

SetGracePeriod configures the shutdown grace period.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL