| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reasonix/internal/jobs" |
| 6 | ) |
| 7 | |
| 8 | // ReserveBackgroundReplacement supports hosts with their own candidate factory. |
| 9 | // The caller holds its session admission lock through publish or disposal. |
| 10 | func ReserveBackgroundReplacement(previous any) (*jobs.SessionBackgroundScope, func(*Controller) error, func(), error) { |
| 11 | old, ok := previous.(*Controller) |
| 12 | if !ok || old.background.scope == nil { |
| 13 | return nil, func(*Controller) error { return nil }, func() {}, nil |
| 14 | } |
| 15 | if ModelReplacementBlocked(old) { |
| 16 | return nil, nil, nil, fmt.Errorf("runtime-dependent work prevents configuration replacement") |
| 17 | } |
| 18 | scope := old.background.scope |
| 19 | release, err := scope.Manager.BeginReplacement("") |
| 20 | if err != nil { |
| 21 | return nil, nil, nil, err |
| 22 | } |
| 23 | transferred := false |
| 24 | finish := func(candidate *Controller) error { |
| 25 | if candidate == nil || candidate.background.scope != scope { |
| 26 | return fmt.Errorf("session factory did not preserve background ownership") |
| 27 | } |
| 28 | candidate.StageBackgroundReplacement(release) |
| 29 | transferred = true |
| 30 | return nil |
| 31 | } |
| 32 | return scope, finish, func() { |
| 33 | if !transferred { |
| 34 | release() |
| 35 | } |
| 36 | }, nil |
| 37 | } |
| 38 |