From 3dc3e8162208c2e0db6da6b0b8ff8baac7cea825 Mon Sep 17 00:00:00 2001 From: Shuo Wu Date: Thu, 21 Mar 2024 23:09:13 -0700 Subject: [PATCH] Process: Check if process is running rather than started or not Longhorn 8091 Signed-off-by: Shuo Wu --- pkg/process/command.go | 20 ++++++++++---------- pkg/process/process.go | 7 +++++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/process/command.go b/pkg/process/command.go index 3b26f0ce4..9ef4a6456 100644 --- a/pkg/process/command.go +++ b/pkg/process/command.go @@ -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() @@ -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) { @@ -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 { @@ -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 @@ -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() { diff --git a/pkg/process/process.go b/pkg/process/process.go index 5db78e6d8..3ff544795 100644 --- a/pkg/process/process.go +++ b/pkg/process/process.go @@ -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 }