| 1 | package daemon |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | |
| 7 | "github.com/FelineStateMachine/tinyrelay/internal/catalog" |
| 8 | ) |
| 9 | |
| 10 | // deleteTenant stops the live services before erasing the catalog row and |
| 11 | // tenant directory. It removes the app map entry before waiting so a request |
| 12 | // arriving concurrently cannot acquire a second reference to the tenant. |
| 13 | func (a *App) deleteTenant(ctx context.Context, id string) error { |
| 14 | a.mu.Lock() |
| 15 | t := a.tenants[id] |
| 16 | if t == nil { |
| 17 | a.mu.Unlock() |
| 18 | return catalog.ErrNotFound |
| 19 | } |
| 20 | if err := a.catalog.Disable(ctx, id); err != nil && !errors.Is(err, catalog.ErrInvalidTransition) { |
| 21 | a.mu.Unlock() |
| 22 | return err |
| 23 | } |
| 24 | delete(a.tenants, id) |
| 25 | a.mu.Unlock() |
| 26 | if err := t.Close(ctx); err != nil { |
| 27 | return err |
| 28 | } |
| 29 | return a.catalog.Delete(ctx, id) |
| 30 | } |