这是indexloc提供的服务,不要输入任何密码
Skip to content

Improve child process management #663

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

Merged
merged 1 commit into from
Feb 3, 2022
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
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
"program": "${workspaceRoot}/cli/cmd/turbo",
"cwd": "${workspaceRoot}/examples/basic",
"args": ["run", "build"]
},
{
"name": "Turbo Version",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/cli/cmd/turbo",
"cwd": "${workspaceRoot}/examples/basic",
"args": ["--version"]
}
]
}
24 changes: 23 additions & 1 deletion cli/cmd/turbo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"time"
"turbo/internal/config"
"turbo/internal/login"
"turbo/internal/process"
prune "turbo/internal/prune"
"turbo/internal/run"
uiPkg "turbo/internal/ui"
"turbo/internal/util"

"github.com/fatih/color"
hclog "github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
)

Expand All @@ -38,6 +40,7 @@ func main() {
}
}
args = args[:argsEnd]

c := cli.NewCLI("turbo", turboVersion)

util.InitPrintf()
Expand All @@ -57,15 +60,25 @@ func main() {
c.HelpWriter = os.Stdout
c.ErrorWriter = os.Stderr
// Parse and validate cmd line flags and env vars
// Note that cf can be nil
cf, err := config.ParseAndValidate(c.Args, ui, turboVersion)
if err != nil {
ui.Error(fmt.Sprintf("%s %s", uiPkg.ERROR_PREFIX, color.RedString(err.Error())))
os.Exit(1)
}

var logger hclog.Logger
if cf != nil {
logger = cf.Logger
} else {
logger = hclog.Default()
}
processes := process.NewManager(logger.Named("processes"))
signalCh := watchSignals(func() { processes.Close() })
c.HiddenCommands = []string{"graph"}
c.Commands = map[string]cli.CommandFactory{
"run": func() (cli.Command, error) {
return &run.RunCommand{Config: cf, Ui: ui},
return &run.RunCommand{Config: cf, Ui: ui, Processes: processes},
nil
},
"prune": func() (cli.Command, error) {
Expand All @@ -87,7 +100,9 @@ func main() {

// Capture the defer statements below so the "done" message comes last
exitCode := 1
doneCh := make(chan struct{})
func() {
defer func() { close(doneCh) }()
// To view a CPU trace, use "go tool trace [file]". Note that the trace
// viewer doesn't work under Windows Subsystem for Linux for some reason.
if traceFile != "" {
Expand Down Expand Up @@ -157,5 +172,12 @@ func main() {
}
}
}()
// Wait for either our command to finish, in which case we need to clean up,
// or to receive a signal, in which case the signal handler above does the cleanup
select {
case <-doneCh:
processes.Close()
case <-signalCh:
}
os.Exit(exitCode)
}
20 changes: 20 additions & 0 deletions cli/cmd/turbo/signals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"os"
"os/signal"
"syscall"
)

func watchSignals(onClose func()) <-chan struct{} {
// TODO: platform specific signals to watch for?
doneCh := make(chan struct{})
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-signalCh
onClose()
close(doneCh)
}()
return doneCh
}
3 changes: 2 additions & 1 deletion cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ require (
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-hclog v0.16.2
github.com/hashicorp/go-gatedio v0.5.0
github.com/hashicorp/go-hclog v1.1.0
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/karrick/godirwalk v1.16.1
github.com/kelseyhightower/envconfig v1.4.0
Expand Down
4 changes: 4 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-gatedio v0.5.0 h1:Jm1X5yP4yCqqWj5L1TgW7iZwCVPGtVc+mro5r/XX7Tg=
github.com/hashicorp/go-gatedio v0.5.0/go.mod h1:Lr3t8L6IyxD3DAeaUxGcgl2JnRUpWMCsmBl4Omu/2t4=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.16.2 h1:K4ev2ib4LdQETX5cSZBG0DVLk1jwGqSPXBjdah3veNs=
github.com/hashicorp/go-hclog v0.16.2/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v1.1.0 h1:QsGcniKx5/LuX2eYoeL+Np3UKYPNaN7YKpTh29h8rbw=
github.com/hashicorp/go-hclog v1.1.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
Expand Down
Loading