Skip to content

Commit

Permalink
Process: Check if process is running rather than started or not
Browse files Browse the repository at this point in the history
Longhorn 8091

Signed-off-by: Shuo Wu <shuo.wu@suse.com>
  • Loading branch information
shuo-wu committed Mar 25, 2024
1 parent 62c25ce commit 3dc3e81
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
20 changes: 10 additions & 10 deletions pkg/process/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Executor interface {
type Command interface {
Run() error
SetOutput(io.Writer)
Started() bool
IsRunning() bool
Stop()
StopWithSignal(signal syscall.Signal)
Kill()
Expand Down Expand Up @@ -62,10 +62,10 @@ func (bc *BinaryCommand) SetOutput(writer io.Writer) {
bc.Stderr = writer
}

func (bc *BinaryCommand) Started() bool {
func (bc *BinaryCommand) IsRunning() bool {
bc.RLock()
defer bc.RUnlock()
return bc.Process != nil
return bc.Process != nil && bc.ProcessState == nil
}

func (bc *BinaryCommand) StopWithSignal(signal syscall.Signal) {
Expand Down Expand Up @@ -112,8 +112,8 @@ type MockCommand struct {

stopCh chan error

started bool
stopped bool
isRunning bool
stopped bool
}

func NewMockCommand(name string, arg ...string) *MockCommand {
Expand All @@ -125,14 +125,14 @@ func NewMockCommand(name string, arg ...string) *MockCommand {

stopCh: make(chan error),

started: false,
stopped: false,
isRunning: false,
stopped: false,
}
}

func (mc *MockCommand) Run() error {
mc.Lock()
mc.started = true
mc.isRunning = true
mc.Unlock()

return <-mc.stopCh
Expand All @@ -141,10 +141,10 @@ func (mc *MockCommand) Run() error {
func (mc *MockCommand) SetOutput(writer io.Writer) {
}

func (mc *MockCommand) Started() bool {
func (mc *MockCommand) IsRunning() bool {
mc.RLock()
defer mc.RUnlock()
return mc.started
return mc.isRunning
}

func (mc *MockCommand) Stop() {
Expand Down
7 changes: 5 additions & 2 deletions pkg/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ func (p *Process) StopWithSignal(signal syscall.Signal) {
}
}()

if cmd == nil || !cmd.Started() {
logrus.Errorf("Process Manager: cmd of %v hasn't started, no need to stop", p.Name)
if cmd == nil || !cmd.IsRunning() {
logrus.Errorf("Process Manager: cmd of %v is not running anymore, no need to stop", p.Name)
if p.State != StateStopped && p.State != StateError {
p.State = StateStopped
}
return
}

Expand Down

0 comments on commit 3dc3e81

Please sign in to comment.