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

playground: add panic guard to prevent some corner cases #2457

Merged
merged 1 commit into from
Sep 5, 2024
Merged
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
31 changes: 31 additions & 0 deletions components/playground/instance/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"github.com/pingcap/errors"
)

var (
errNotUp = errors.New("not up")
)

// Process represent process to be run by playground
type Process interface {
Start() error
Expand All @@ -32,13 +36,21 @@ type process struct {

// Start the process
func (p *process) Start() error {
if p == nil {
return errNotUp
}

// fmt.Printf("Starting `%s`: %s", filepath.Base(p.cmd.Path), strings.Join(p.cmd.Args, " "))
p.startTime = time.Now()
return p.cmd.Start()
}

// Wait implements Instance interface.
func (p *process) Wait() error {
if p == nil {
return errNotUp
}

p.waitOnce.Do(func() {
p.waitErr = p.cmd.Wait()
})
Expand All @@ -48,11 +60,18 @@ func (p *process) Wait() error {

// Pid implements Instance interface.
func (p *process) Pid() int {
if p == nil {
return 0
}
return p.cmd.Process.Pid
}

// Uptime implements Instance interface.
func (p *process) Uptime() string {
if p == nil {
return errNotUp.Error()
}

s := p.cmd.ProcessState

if s != nil {
Expand All @@ -64,6 +83,10 @@ func (p *process) Uptime() string {
}

func (p *process) SetOutputFile(fname string) error {
if p == nil {
return errNotUp
}

f, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return errors.AddStack(err)
Expand All @@ -73,11 +96,19 @@ func (p *process) SetOutputFile(fname string) error {
}

func (p *process) setOutput(w io.Writer) {
if p == nil {
return
}

p.cmd.Stdout = w
p.cmd.Stderr = w
}

func (p *process) Cmd() *exec.Cmd {
if p == nil {
panic(errNotUp)
}

return p.cmd
}

Expand Down
Loading