AddDependency adds a dependency relationship between units. The unit depends on the dependsOn unit reaching the requiredStatus.
(unit ID, dependsOn ID, requiredStatus Status)
| 156 | // AddDependency adds a dependency relationship between units. |
| 157 | // The unit depends on the dependsOn unit reaching the requiredStatus. |
| 158 | func (m *Manager) AddDependency(unit ID, dependsOn ID, requiredStatus Status) error { |
| 159 | m.mu.Lock() |
| 160 | defer m.mu.Unlock() |
| 161 | |
| 162 | switch { |
| 163 | case unit == "": |
| 164 | return xerrors.Errorf("dependent name cannot be empty: %w", ErrUnitIDRequired) |
| 165 | case dependsOn == "": |
| 166 | return xerrors.Errorf("dependency name cannot be empty: %w", ErrUnitIDRequired) |
| 167 | case !m.registered(unit): |
| 168 | return xerrors.Errorf("dependent unit %q must be registered first: %w", unit, ErrUnitNotFound) |
| 169 | } |
| 170 | |
| 171 | // Add the dependency edge to the graph |
| 172 | // The edge goes from unit to dependsOn, representing the dependency |
| 173 | err := m.graph.AddEdge(unit, dependsOn, requiredStatus) |
| 174 | if err != nil { |
| 175 | return xerrors.Errorf("adding edge for unit %q: %w", unit, errors.Join(ErrFailedToAddDependency, err)) |
| 176 | } |
| 177 | |
| 178 | // Recalculate readiness for the unit since it now has a new dependency |
| 179 | m.recalculateReadinessUnsafe(unit) |
| 180 | |
| 181 | return nil |
| 182 | } |
| 183 | |
| 184 | // UpdateStatus updates a unit's status and recalculates readiness for affected dependents. |
| 185 | func (m *Manager) UpdateStatus(unit ID, newStatus Status) error { |