|
| 1 | +package state |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +// Saga phase statuses. |
| 9 | +const ( |
| 10 | + PhasePending = "pending" |
| 11 | + PhaseStarted = "started" |
| 12 | + PhaseSatisfied = "satisfied" |
| 13 | + PhaseFailed = "failed" |
| 14 | +) |
| 15 | + |
| 16 | +// SagaPhase is one row of the saga_phase resumability table (spec 08/09). A phase |
| 17 | +// is keyed by (workspace, scope, phase); scope is "" for workspace-wide phases or |
| 18 | +// a project name for per-project ones. |
| 19 | +type SagaPhase struct { |
| 20 | + Workspace string |
| 21 | + Scope string |
| 22 | + Phase string |
| 23 | + Status string |
| 24 | + Fingerprint string |
| 25 | + StartedAt string |
| 26 | + SatisfiedAt string |
| 27 | + Error string |
| 28 | +} |
| 29 | + |
| 30 | +// StartPhase marks a phase started with the given input fingerprint (upsert). |
| 31 | +// Clears any prior satisfied/failed/error state for a fresh attempt. Hold the lock. |
| 32 | +func (db *DB) StartPhase(workspace, scope, phase, fingerprint string) error { |
| 33 | + _, err := db.Exec(` |
| 34 | + INSERT INTO saga_phase (ctx, workspace, scope, phase, status, fingerprint, started_at) |
| 35 | + VALUES (?,?,?,?,?,?,datetime('now')) |
| 36 | + ON CONFLICT(ctx, workspace, scope, phase) |
| 37 | + DO UPDATE SET status=excluded.status, fingerprint=excluded.fingerprint, |
| 38 | + started_at=excluded.started_at, satisfied_at=NULL, error=NULL`, |
| 39 | + db.Ctx, workspace, scope, phase, PhaseStarted, fingerprint) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("start phase %s/%s/%s: %w", workspace, scope, phase, err) |
| 42 | + } |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// SatisfyPhase marks a phase satisfied (completed). Hold the lock. |
| 47 | +func (db *DB) SatisfyPhase(workspace, scope, phase string) error { |
| 48 | + _, err := db.Exec(`UPDATE saga_phase SET status=?, satisfied_at=datetime('now'), error=NULL |
| 49 | + WHERE ctx=? AND workspace=? AND scope=? AND phase=?`, |
| 50 | + PhaseSatisfied, db.Ctx, workspace, scope, phase) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("satisfy phase %s/%s/%s: %w", workspace, scope, phase, err) |
| 53 | + } |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +// FailPhase marks a phase failed with an error message. Hold the lock. |
| 58 | +func (db *DB) FailPhase(workspace, scope, phase, errMsg string) error { |
| 59 | + _, err := db.Exec(`UPDATE saga_phase SET status=?, error=? |
| 60 | + WHERE ctx=? AND workspace=? AND scope=? AND phase=?`, |
| 61 | + PhaseFailed, errMsg, db.Ctx, workspace, scope, phase) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("fail phase %s/%s/%s: %w", workspace, scope, phase, err) |
| 64 | + } |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// GetPhase returns a phase row, ok=false if it has never been recorded. |
| 69 | +func (db *DB) GetPhase(workspace, scope, phase string) (SagaPhase, bool, error) { |
| 70 | + var p SagaPhase |
| 71 | + var started, satisfied, errMsg sql.NullString |
| 72 | + err := db.QueryRow(`SELECT workspace, scope, phase, status, fingerprint, started_at, satisfied_at, error |
| 73 | + FROM saga_phase WHERE ctx=? AND workspace=? AND scope=? AND phase=?`, |
| 74 | + db.Ctx, workspace, scope, phase). |
| 75 | + Scan(&p.Workspace, &p.Scope, &p.Phase, &p.Status, &p.Fingerprint, &started, &satisfied, &errMsg) |
| 76 | + if err == sql.ErrNoRows { |
| 77 | + return SagaPhase{}, false, nil |
| 78 | + } |
| 79 | + if err != nil { |
| 80 | + return SagaPhase{}, false, fmt.Errorf("get phase: %w", err) |
| 81 | + } |
| 82 | + p.StartedAt, p.SatisfiedAt, p.Error = started.String, satisfied.String, errMsg.String |
| 83 | + return p, true, nil |
| 84 | +} |
| 85 | + |
| 86 | +// PhaseSatisfied reports whether a phase is already satisfied for the SAME input |
| 87 | +// fingerprint — the skip check a re-run uses to avoid redoing unchanged work. A |
| 88 | +// changed fingerprint (config/template/param edit) re-arms the phase. |
| 89 | +func (db *DB) PhaseSatisfied(workspace, scope, phase, fingerprint string) (bool, error) { |
| 90 | + p, ok, err := db.GetPhase(workspace, scope, phase) |
| 91 | + if err != nil || !ok { |
| 92 | + return false, err |
| 93 | + } |
| 94 | + return p.Status == PhaseSatisfied && p.Fingerprint == fingerprint, nil |
| 95 | +} |
| 96 | + |
| 97 | +// PhasesFor returns all recorded phases for a workspace, ordered, for `status`. |
| 98 | +func (db *DB) PhasesFor(workspace string) ([]SagaPhase, error) { |
| 99 | + rows, err := db.Query(`SELECT workspace, scope, phase, status, fingerprint, started_at, satisfied_at, error |
| 100 | + FROM saga_phase WHERE ctx=? AND workspace=? ORDER BY scope, phase`, db.Ctx, workspace) |
| 101 | + if err != nil { |
| 102 | + return nil, fmt.Errorf("phases for %s: %w", workspace, err) |
| 103 | + } |
| 104 | + defer rows.Close() |
| 105 | + var out []SagaPhase |
| 106 | + for rows.Next() { |
| 107 | + var p SagaPhase |
| 108 | + var started, satisfied, errMsg sql.NullString |
| 109 | + if err := rows.Scan(&p.Workspace, &p.Scope, &p.Phase, &p.Status, &p.Fingerprint, &started, &satisfied, &errMsg); err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + p.StartedAt, p.SatisfiedAt, p.Error = started.String, satisfied.String, errMsg.String |
| 113 | + out = append(out, p) |
| 114 | + } |
| 115 | + return out, rows.Err() |
| 116 | +} |
| 117 | + |
| 118 | +// ClearPhase removes a phase row (compensation / forced re-run). Hold the lock. |
| 119 | +func (db *DB) ClearPhase(workspace, scope, phase string) error { |
| 120 | + _, err := db.Exec(`DELETE FROM saga_phase WHERE ctx=? AND workspace=? AND scope=? AND phase=?`, |
| 121 | + db.Ctx, workspace, scope, phase) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("clear phase %s/%s/%s: %w", workspace, scope, phase, err) |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
0 commit comments