| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/sylvinus/agent-vm/internal/diagnostics" |
| 11 | "github.com/sylvinus/agent-vm/internal/managedproxy" |
| 12 | ) |
| 13 | |
| 14 | func (s *Server) lifetimeContextLocked() context.Context { |
| 15 | if s.lifeContext == nil { |
| 16 | s.lifeContext, s.lifeCancel = context.WithCancel(context.Background()) |
| 17 | if s.closing { |
| 18 | s.lifeCancel() |
| 19 | } |
| 20 | } |
| 21 | return s.lifeContext |
| 22 | } |
| 23 | |
| 24 | func (s *Server) lifetimeContext() context.Context { |
| 25 | s.mu.Lock() |
| 26 | defer s.mu.Unlock() |
| 27 | return s.lifetimeContextLocked() |
| 28 | } |
| 29 | |
| 30 | func (s *Server) startMonitors() { |
| 31 | s.mu.Lock() |
| 32 | if s.closing { |
| 33 | s.mu.Unlock() |
| 34 | return |
| 35 | } |
| 36 | ctx := s.lifetimeContextLocked() |
| 37 | s.monitors.Add(4) |
| 38 | s.mu.Unlock() |
| 39 | go func() { |
| 40 | defer s.monitors.Done() |
| 41 | ticker := time.NewTicker(time.Hour) |
| 42 | defer ticker.Stop() |
| 43 | for { |
| 44 | select { |
| 45 | case <-ctx.Done(): |
| 46 | return |
| 47 | case <-ticker.C: |
| 48 | if err := diagnostics.New(s.profile).Clean(); err != nil { |
| 49 | diagnostics.Log("error", "diagnostics", "log cleanup failed: "+err.Error()) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | }() |
| 54 | go func() { |
| 55 | defer s.monitors.Done() |
| 56 | s.monitor(ctx) |
| 57 | }() |
| 58 | go func() { |
| 59 | defer s.monitors.Done() |
| 60 | ticker := time.NewTicker(5 * time.Second) |
| 61 | defer ticker.Stop() |
| 62 | for ctx.Err() == nil { |
| 63 | |
| 64 | |
| 65 | if !s.busy() { |
| 66 | s.monitorSafety() |
| 67 | } |
| 68 | select { |
| 69 | case <-ctx.Done(): |
| 70 | return |
| 71 | case <-ticker.C: |
| 72 | } |
| 73 | } |
| 74 | }() |
| 75 | go func() { |
| 76 | defer s.monitors.Done() |
| 77 | |
| 78 | |
| 79 | if s.macosGuest == nil && (s.windowsGuest == nil || s.windowsGuest.NetworkPairingFile != "") { |
| 80 | s.monitorNetwork(ctx) |
| 81 | } |
| 82 | }() |
| 83 | } |
| 84 | |
| 85 | |
| 86 | func (s *Server) beginClose() { |
| 87 | s.mu.Lock() |
| 88 | s.closing = true |
| 89 | s.lifetimeContextLocked() |
| 90 | s.lifeCancel() |
| 91 | if s.checkCancel != nil { |
| 92 | s.checkCancel() |
| 93 | } |
| 94 | if s.monitorCancel != nil { |
| 95 | s.monitorCancel() |
| 96 | } |
| 97 | s.mu.Unlock() |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | func (s *Server) closeBackground() { |
| 103 | s.beginClose() |
| 104 | s.jobs.close() |
| 105 | s.monitors.Wait() |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | func (s *Server) beginActivity(parent context.Context) (context.Context, func(), error) { |
| 111 | s.mu.Lock() |
| 112 | if s.closing { |
| 113 | s.mu.Unlock() |
| 114 | return nil, nil, errJobsClosed |
| 115 | } |
| 116 | lifetime := s.lifetimeContextLocked() |
| 117 | s.activities.Add(1) |
| 118 | s.mu.Unlock() |
| 119 | ctx, cancel := context.WithCancel(parent) |
| 120 | stop := context.AfterFunc(lifetime, cancel) |
| 121 | return ctx, func() { stop(); cancel(); s.activities.Done() }, nil |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | func (s *Server) closeService() error { |
| 127 | s.closeOnce.Do(func() { |
| 128 | s.beginClose() |
| 129 | |
| 130 | |
| 131 | s.terminals.close() |
| 132 | s.closeBackground() |
| 133 | s.activities.Wait() |
| 134 | s.op.Lock() |
| 135 | defer s.op.Unlock() |
| 136 | s.closeProjectShares() |
| 137 | s.closeWindowsBrowser() |
| 138 | if s.windowsGuest != nil { |
| 139 | s.closeErr = s.closeProxyGate("控制服务正在退出,外网保持关闭") |
| 140 | } |
| 141 | s.closeWindowsNetwork() |
| 142 | s.mu.Lock() |
| 143 | browser := s.browserControl |
| 144 | s.ready = false |
| 145 | s.credentials = guestCredentials{} |
| 146 | s.mu.Unlock() |
| 147 | if browser != nil { |
| 148 | browser.close() |
| 149 | } |
| 150 | s.controlSession.close() |
| 151 | if s.windowsGuest == nil { |
| 152 | s.closeErr = s.closeProxyGate("控制服务正在退出,外网保持关闭") |
| 153 | } |
| 154 | s.closeErr = errors.Join(s.closeErr, managedproxy.Stop(s.profile)) |
| 155 | if s.controlPublished { |
| 156 | if err := os.Remove(filepath.Join(s.profile, "control.json")); err != nil && !os.IsNotExist(err) { |
| 157 | s.closeErr = errors.Join(s.closeErr, err) |
| 158 | } |
| 159 | } |
| 160 | }) |
| 161 | return s.closeErr |
| 162 | } |
| 163 | |
| 164 | func (s *Server) controlContext() (context.Context, context.CancelFunc) { |
| 165 | return context.WithTimeout(s.lifetimeContext(), 5*time.Second) |
| 166 | } |