mirror of
https://github.com/fatedier/frp.git
synced 2026-04-09 02:29:15 +08:00
Compare commits
2 Commits
master
...
8d1ab7d585
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d1ab7d585 | ||
|
|
f32bec9f4d |
@@ -76,7 +76,10 @@ func (f *Framework) RunFrps(args ...string) (*process.Process, string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return p, p.Output(), err
|
return p, p.Output(), err
|
||||||
}
|
}
|
||||||
time.Sleep(2 * time.Second)
|
select {
|
||||||
|
case <-p.Done():
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
}
|
||||||
return p, p.Output(), nil
|
return p, p.Output(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +90,10 @@ func (f *Framework) RunFrpc(args ...string) (*process.Process, string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return p, p.Output(), err
|
return p, p.Output(), err
|
||||||
}
|
}
|
||||||
time.Sleep(1500 * time.Millisecond)
|
select {
|
||||||
|
case <-p.Done():
|
||||||
|
case <-time.After(1500 * time.Millisecond):
|
||||||
|
}
|
||||||
return p, p.Output(), nil
|
return p, p.Output(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package process
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Process struct {
|
type Process struct {
|
||||||
@@ -12,6 +14,11 @@ type Process struct {
|
|||||||
errorOutput *bytes.Buffer
|
errorOutput *bytes.Buffer
|
||||||
stdOutput *bytes.Buffer
|
stdOutput *bytes.Buffer
|
||||||
|
|
||||||
|
done chan struct{}
|
||||||
|
closeOne sync.Once
|
||||||
|
waitErr error
|
||||||
|
|
||||||
|
started bool
|
||||||
beforeStopHandler func()
|
beforeStopHandler func()
|
||||||
stopped bool
|
stopped bool
|
||||||
}
|
}
|
||||||
@@ -27,6 +34,7 @@ func NewWithEnvs(path string, params []string, envs []string) *Process {
|
|||||||
p := &Process{
|
p := &Process{
|
||||||
cmd: cmd,
|
cmd: cmd,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
|
done: make(chan struct{}),
|
||||||
}
|
}
|
||||||
p.errorOutput = bytes.NewBufferString("")
|
p.errorOutput = bytes.NewBufferString("")
|
||||||
p.stdOutput = bytes.NewBufferString("")
|
p.stdOutput = bytes.NewBufferString("")
|
||||||
@@ -36,11 +44,35 @@ func NewWithEnvs(path string, params []string, envs []string) *Process {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) Start() error {
|
func (p *Process) Start() error {
|
||||||
return p.cmd.Start()
|
if p.started {
|
||||||
|
return errors.New("process already started")
|
||||||
|
}
|
||||||
|
p.started = true
|
||||||
|
|
||||||
|
err := p.cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
p.waitErr = err
|
||||||
|
p.closeDone()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
p.waitErr = p.cmd.Wait()
|
||||||
|
p.closeDone()
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Process) closeDone() {
|
||||||
|
p.closeOne.Do(func() { close(p.done) })
|
||||||
|
}
|
||||||
|
|
||||||
|
// Done returns a channel that is closed when the process exits.
|
||||||
|
func (p *Process) Done() <-chan struct{} {
|
||||||
|
return p.done
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) Stop() error {
|
func (p *Process) Stop() error {
|
||||||
if p.stopped {
|
if p.stopped || !p.started {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -50,7 +82,8 @@ func (p *Process) Stop() error {
|
|||||||
p.beforeStopHandler()
|
p.beforeStopHandler()
|
||||||
}
|
}
|
||||||
p.cancel()
|
p.cancel()
|
||||||
return p.cmd.Wait()
|
<-p.done
|
||||||
|
return p.waitErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Process) ErrorOutput() string {
|
func (p *Process) ErrorOutput() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user