Skip to content

Commit bc7f4fb

Browse files
committed
feat: add ability to stop individual daemons
Signed-off-by: Donnie Adams <[email protected]>
1 parent e5fe428 commit bc7f4fb

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

pkg/engine/cmd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,8 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T
292292
args = args[1:]
293293
}
294294

295-
var (
296-
stop = func() {}
297-
)
295+
ctx, cancel := context.WithCancel(ctx)
296+
stop := cancel
298297

299298
if strings.TrimSpace(rest) != "" {
300299
f, err := os.CreateTemp(env.Getenv("GPTSCRIPT_TMPDIR", envvars), version.ProgramName+requiredFileExtensions[args[0]])
@@ -303,6 +302,7 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T
303302
}
304303
stop = func() {
305304
_ = os.Remove(f.Name())
305+
cancel()
306306
}
307307

308308
_, err = f.Write([]byte(rest))

pkg/engine/daemon.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var ports Ports
1919

2020
type Ports struct {
2121
daemonPorts map[string]int64
22-
daemonsRunning map[string]struct{}
22+
daemonsRunning map[string]func()
2323
daemonLock sync.Mutex
2424

2525
startPort, endPort int64
@@ -57,6 +57,17 @@ func CloseDaemons() {
5757
ports.daemonWG.Wait()
5858
}
5959

60+
func StopDaemon(url string) {
61+
ports.daemonLock.Lock()
62+
defer ports.daemonLock.Unlock()
63+
64+
if stop := ports.daemonsRunning[url]; stop != nil {
65+
stop()
66+
}
67+
68+
delete(ports.daemonsRunning, url)
69+
}
70+
6071
func nextPort() int64 {
6172
if ports.startPort == 0 {
6273
ports.startPort = 10240
@@ -118,7 +129,7 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {
118129

119130
port, ok := ports.daemonPorts[tool.ID]
120131
url := fmt.Sprintf("http://127.0.0.1:%d%s", port, path)
121-
if ok {
132+
if ok && ports.daemonsRunning[url] != nil {
122133
return url, nil
123134
}
124135

@@ -172,13 +183,13 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {
172183

173184
if ports.daemonPorts == nil {
174185
ports.daemonPorts = map[string]int64{}
175-
ports.daemonsRunning = map[string]struct{}{}
186+
ports.daemonsRunning = map[string]func(){}
176187
}
177188
ports.daemonPorts[tool.ID] = port
178-
ports.daemonsRunning[url] = struct{}{}
189+
ports.daemonsRunning[url] = stop
179190

180-
killedCtx, cancel := context.WithCancelCause(ctx)
181-
defer cancel(nil)
191+
killedCtx, killedCancel := context.WithCancelCause(ctx)
192+
defer killedCancel(nil)
182193

183194
ports.daemonWG.Add(1)
184195
go func() {
@@ -189,7 +200,7 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {
189200
_ = r.Close()
190201
_ = w.Close()
191202

192-
cancel(err)
203+
killedCancel(err)
193204
stop()
194205
ports.daemonLock.Lock()
195206
defer ports.daemonLock.Unlock()

0 commit comments

Comments
 (0)