Skip to content

Commit

Permalink
playground: add panic guard to prevent some corner cases (#2457)
Browse files Browse the repository at this point in the history
Signed-off-by: xhe <[email protected]>
  • Loading branch information
xhebox authored Sep 5, 2024
1 parent 0b15af9 commit c1a14a5
Showing 1 changed file with 31 additions and 0 deletions.
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

0 comments on commit c1a14a5

Please sign in to comment.