Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to stop individual daemons #914

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/engine/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,8 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T
args = args[1:]
}

var (
stop = func() {}
)
ctx, cancel := context.WithCancel(ctx)
stop := cancel

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

_, err = f.Write([]byte(rest))
Expand Down
25 changes: 18 additions & 7 deletions pkg/engine/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var ports Ports

type Ports struct {
daemonPorts map[string]int64
daemonsRunning map[string]struct{}
daemonsRunning map[string]func()
daemonLock sync.Mutex

startPort, endPort int64
Expand Down Expand Up @@ -57,6 +57,17 @@ func CloseDaemons() {
ports.daemonWG.Wait()
}

func StopDaemon(url string) {
StrongMonkey marked this conversation as resolved.
Show resolved Hide resolved
ports.daemonLock.Lock()
defer ports.daemonLock.Unlock()

if stop := ports.daemonsRunning[url]; stop != nil {
stop()
}

delete(ports.daemonsRunning, url)
}

func nextPort() int64 {
if ports.startPort == 0 {
ports.startPort = 10240
Expand Down Expand Up @@ -118,7 +129,7 @@ func (e *Engine) startDaemon(tool types.Tool) (string, error) {

port, ok := ports.daemonPorts[tool.ID]
url := fmt.Sprintf("http://127.0.0.1:%d%s", port, path)
if ok {
if ok && ports.daemonsRunning[url] != nil {
return url, nil
}

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

if ports.daemonPorts == nil {
ports.daemonPorts = map[string]int64{}
ports.daemonsRunning = map[string]struct{}{}
ports.daemonsRunning = map[string]func(){}
}
ports.daemonPorts[tool.ID] = port
ports.daemonsRunning[url] = struct{}{}
ports.daemonsRunning[url] = stop

killedCtx, cancel := context.WithCancelCause(ctx)
defer cancel(nil)
killedCtx, killedCancel := context.WithCancelCause(ctx)
defer killedCancel(nil)

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

cancel(err)
killedCancel(err)
stop()
ports.daemonLock.Lock()
defer ports.daemonLock.Unlock()
Expand Down
Loading