UpdateStatus updates a unit's status and recalculates readiness for affected dependents.
(unit ID, newStatus Status)
| 183 | |
| 184 | // UpdateStatus updates a unit's status and recalculates readiness for affected dependents. |
| 185 | func (m *Manager) UpdateStatus(unit ID, newStatus Status) error { |
| 186 | m.mu.Lock() |
| 187 | defer m.mu.Unlock() |
| 188 | |
| 189 | switch { |
| 190 | case unit == "": |
| 191 | return xerrors.Errorf("updating status for unit %q: %w", unit, ErrUnitIDRequired) |
| 192 | case !m.registered(unit): |
| 193 | return xerrors.Errorf("unit %q must be registered first: %w", unit, ErrUnitNotFound) |
| 194 | } |
| 195 | |
| 196 | u := m.units[unit] |
| 197 | if u.status == newStatus { |
| 198 | return xerrors.Errorf("checking status for unit %q: %w", unit, ErrSameStatusAlreadySet) |
| 199 | } |
| 200 | |
| 201 | u.status = newStatus |
| 202 | m.units[unit] = u |
| 203 | |
| 204 | // Get all units that depend on this one (reverse adjacent vertices) |
| 205 | dependents := m.graph.GetReverseAdjacentVertices(unit) |
| 206 | |
| 207 | // Recalculate readiness for all dependents |
| 208 | for _, dependent := range dependents { |
| 209 | m.recalculateReadinessUnsafe(dependent.From) |
| 210 | } |
| 211 | |
| 212 | return nil |
| 213 | } |
| 214 | |
| 215 | // recalculateReadinessUnsafe recalculates the readiness state for a unit. |
| 216 | // This method assumes the caller holds the write lock. |