169 lines
3.5 KiB
Go
169 lines
3.5 KiB
Go
package installguide
|
|
|
|
import (
|
|
"context"
|
|
"maps"
|
|
"slices"
|
|
"sync"
|
|
)
|
|
|
|
type activeGuide struct {
|
|
pid int
|
|
unfold chan struct{}
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
// Manager coordinates active VM-window installation guides for the lifetime of
|
|
// the builtin.install-guide plugin. Calling Close cancels every active overlay
|
|
// and prevents new ones from starting (zero-residue plugin disposal).
|
|
type Manager struct {
|
|
mu sync.Mutex
|
|
closed bool
|
|
active map[string]*activeGuide
|
|
onChange func()
|
|
runner func(context.Context, Guide) error
|
|
}
|
|
|
|
// NewManager creates an active guide manager. onChange, if non-nil, is called
|
|
// whenever the set of active guides changes outside a synchronous caller's own
|
|
// return value (e.g. when a guide exits on its own or is torn down).
|
|
func NewManager(onChange func()) *Manager {
|
|
return &Manager{
|
|
active: make(map[string]*activeGuide),
|
|
onChange: onChange,
|
|
runner: Run,
|
|
}
|
|
}
|
|
|
|
// Start launches a guide for g.Name unless the manager has been closed. Any
|
|
// previous guide for the same VM is canceled first.
|
|
func (m *Manager) Start(parent context.Context, g Guide) bool {
|
|
if parent == nil {
|
|
parent = context.Background()
|
|
}
|
|
ctx, cancel := context.WithCancel(parent)
|
|
entry := &activeGuide{
|
|
pid: int(g.PID),
|
|
unfold: make(chan struct{}, 1),
|
|
cancel: cancel,
|
|
}
|
|
|
|
m.mu.Lock()
|
|
if m.closed {
|
|
m.mu.Unlock()
|
|
cancel()
|
|
return false
|
|
}
|
|
if prev := m.active[g.Name]; prev != nil {
|
|
prev.cancel()
|
|
}
|
|
m.active[g.Name] = entry
|
|
runFn := m.runner
|
|
if runFn == nil {
|
|
runFn = Run
|
|
}
|
|
m.mu.Unlock()
|
|
|
|
g.Unfold = entry.unfold
|
|
go func() {
|
|
defer cancel()
|
|
if err := runFn(ctx, g); err != nil && g.LogError != nil {
|
|
g.LogError(err)
|
|
}
|
|
m.mu.Lock()
|
|
removed := m.active[g.Name] == entry
|
|
if removed {
|
|
delete(m.active, g.Name)
|
|
}
|
|
cb := m.onChange
|
|
m.mu.Unlock()
|
|
if removed && cb != nil {
|
|
cb()
|
|
}
|
|
}()
|
|
return true
|
|
}
|
|
|
|
// Unfold requests that a folded guide for (name, pid) expand back to its card.
|
|
func (m *Manager) Unfold(name string, pid int) bool {
|
|
m.mu.Lock()
|
|
entry := m.active[name]
|
|
m.mu.Unlock()
|
|
if entry == nil || entry.pid != pid {
|
|
return false
|
|
}
|
|
select {
|
|
case entry.unfold <- struct{}{}:
|
|
default:
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Cancel stops the active guide for name, if any.
|
|
func (m *Manager) Cancel(name string) bool {
|
|
m.mu.Lock()
|
|
entry := m.active[name]
|
|
if entry != nil {
|
|
entry.cancel()
|
|
delete(m.active, name)
|
|
}
|
|
cb := m.onChange
|
|
m.mu.Unlock()
|
|
if entry != nil {
|
|
if cb != nil {
|
|
cb()
|
|
}
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// CancelStopped stops any guide whose VM PID is no longer in running.
|
|
func (m *Manager) CancelStopped(running map[int]bool) bool {
|
|
m.mu.Lock()
|
|
changed := false
|
|
for name, entry := range m.active {
|
|
if !running[entry.pid] {
|
|
entry.cancel()
|
|
delete(m.active, name)
|
|
changed = true
|
|
}
|
|
}
|
|
m.mu.Unlock()
|
|
return changed
|
|
}
|
|
|
|
// ActiveNames returns a sorted slice of VM names with an active guide.
|
|
func (m *Manager) ActiveNames() []string {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if len(m.active) == 0 {
|
|
return nil
|
|
}
|
|
return slices.Sorted(maps.Keys(m.active))
|
|
}
|
|
|
|
// HasActive reports whether a guide is currently active for (name, pid).
|
|
func (m *Manager) HasActive(name string, pid int) bool {
|
|
m.mu.Lock()
|
|
entry := m.active[name]
|
|
m.mu.Unlock()
|
|
return entry != nil && entry.pid == pid
|
|
}
|
|
|
|
// Close cancels all active guides and rejects future Start calls.
|
|
func (m *Manager) Close() {
|
|
m.mu.Lock()
|
|
m.closed = true
|
|
hadAny := len(m.active) > 0
|
|
for name, entry := range m.active {
|
|
entry.cancel()
|
|
delete(m.active, name)
|
|
}
|
|
cb := m.onChange
|
|
m.mu.Unlock()
|
|
if hadAny && cb != nil {
|
|
cb()
|
|
}
|
|
}
|