| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | |
| 7 | "reasonix/internal/agent" |
| 8 | "reasonix/internal/jobs" |
| 9 | ) |
| 10 | |
| 11 | // SessionDestroyHandle separates cancelled-job waiting from ending the destroy |
| 12 | // window, allowing persistent artifacts to move between Wait and Finish. |
| 13 | type SessionDestroyHandle struct { |
| 14 | Wait func() jobs.TeardownResult |
| 15 | WaitFor func(time.Duration) jobs.TeardownResult |
| 16 | WaitAll func() |
| 17 | Finish func() |
| 18 | Async bool |
| 19 | } |
| 20 | |
| 21 | func (c *Controller) BeginDestroySession(sessionPath string) SessionDestroyHandle { |
| 22 | parentSession := agent.BranchID(sessionPath) |
| 23 | if c.jobs == nil || parentSession == "" { |
| 24 | wait := func() jobs.TeardownResult { return jobs.TeardownResult{} } |
| 25 | noop := func() {} |
| 26 | return SessionDestroyHandle{Wait: wait, WaitAll: noop, Finish: noop} |
| 27 | } |
| 28 | teardown := c.jobs.BeginDestroySession(parentSession) |
| 29 | return SessionDestroyHandle{ |
| 30 | Wait: func() jobs.TeardownResult { |
| 31 | return c.jobs.WaitTeardown(context.Background(), teardown, c.jobs.TeardownGrace()) |
| 32 | }, |
| 33 | WaitFor: func(requested time.Duration) jobs.TeardownResult { |
| 34 | grace := c.jobs.TeardownGrace() |
| 35 | if requested >= 0 && requested < grace { |
| 36 | grace = requested |
| 37 | } |
| 38 | return c.jobs.WaitTeardown(context.Background(), teardown, grace) |
| 39 | }, |
| 40 | WaitAll: func() { |
| 41 | for _, ch := range teardown.DoneChannels() { |
| 42 | <-ch |
| 43 | } |
| 44 | }, |
| 45 | Finish: func() { c.jobs.FinishDestroySession(parentSession) }, |
| 46 | Async: teardown.Async(), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func (c *Controller) IsDestroyingSession(sessionPath string) bool { |
| 51 | return c.jobs != nil && c.jobs.IsDestroying(agent.BranchID(sessionPath)) |
| 52 | } |
| 53 |