mirror of
https://github.com/F1bonacc1/process-compose.git
synced 2024-09-11 11:35:26 +03:00
Issue #149: Don't restart processes on manual stop
This commit is contained in:
parent
1a73f446a9
commit
3bf8bd3382
@ -13,6 +13,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@ -61,6 +62,7 @@ type Process struct {
|
||||
printLogs bool
|
||||
isMain bool
|
||||
extraArgs []string
|
||||
isStopped atomic.Bool
|
||||
}
|
||||
|
||||
func NewProcess(
|
||||
@ -249,6 +251,9 @@ func (p *Process) isRestartable() bool {
|
||||
p.Lock()
|
||||
exitCode := p.getExitCode()
|
||||
p.Unlock()
|
||||
if p.isStopped.Swap(false) {
|
||||
return false
|
||||
}
|
||||
if p.procConf.RestartPolicy.Restart == types.RestartPolicyNo ||
|
||||
p.procConf.RestartPolicy.Restart == "" {
|
||||
return false
|
||||
@ -365,7 +370,9 @@ func (p *Process) isRunning() bool {
|
||||
|
||||
func (p *Process) prepareForShutDown() {
|
||||
// prevent restart during global shutdown or scale down
|
||||
p.procConf.RestartPolicy.Restart = types.RestartPolicyNo
|
||||
//p.procConf.RestartPolicy.Restart = types.RestartPolicyNo
|
||||
p.isStopped.Store(true)
|
||||
|
||||
}
|
||||
|
||||
func (p *Process) onProcessStart() {
|
||||
|
@ -259,7 +259,7 @@ func (p *ProjectRunner) StopProcess(name string) error {
|
||||
log.Error().Msgf("Process %s is not running", name)
|
||||
return fmt.Errorf("process %s is not running", name)
|
||||
}
|
||||
err := proc.shutDown()
|
||||
err := proc.shutDownNoRestart()
|
||||
if err != nil {
|
||||
log.Err(err).Msgf("failed to stop process %s", name)
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package app
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"github.com/f1bonacc1/process-compose/src/command"
|
||||
"github.com/f1bonacc1/process-compose/src/loader"
|
||||
"github.com/f1bonacc1/process-compose/src/types"
|
||||
"os"
|
||||
@ -501,3 +502,91 @@ func TestSystem_TestProcListShutsDownInOrder(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSystem_TestProcShutDownNoRestart(t *testing.T) {
|
||||
restarting := "Restarting"
|
||||
notRestarting := "NotRestarting"
|
||||
shell := command.DefaultShellConfig()
|
||||
project := &types.Project{
|
||||
Processes: map[string]types.ProcessConfig{
|
||||
restarting: {
|
||||
Name: restarting,
|
||||
ReplicaName: restarting,
|
||||
Executable: shell.ShellCommand,
|
||||
Args: []string{shell.ShellArgument, "sleep 2"},
|
||||
RestartPolicy: types.RestartPolicyConfig{
|
||||
Restart: types.RestartPolicyAlways,
|
||||
},
|
||||
},
|
||||
notRestarting: {
|
||||
Name: notRestarting,
|
||||
ReplicaName: notRestarting,
|
||||
Executable: shell.ShellCommand,
|
||||
Args: []string{shell.ShellArgument, "sleep 2"},
|
||||
RestartPolicy: types.RestartPolicyConfig{
|
||||
Restart: types.RestartPolicyNo,
|
||||
},
|
||||
},
|
||||
},
|
||||
ShellConfig: shell,
|
||||
}
|
||||
runner, err := NewProjectRunner(&ProjectOpts{
|
||||
project: project,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf(err.Error())
|
||||
return
|
||||
}
|
||||
go runner.Run()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
state, err := runner.GetProcessState(restarting)
|
||||
if err != nil {
|
||||
t.Errorf(err.Error())
|
||||
return
|
||||
}
|
||||
if state.Status != types.ProcessStateRunning {
|
||||
t.Errorf("process %s is not running", restarting)
|
||||
return
|
||||
}
|
||||
err = runner.StopProcess(restarting)
|
||||
if err != nil {
|
||||
t.Errorf(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
state, err = runner.GetProcessState(restarting)
|
||||
if err != nil {
|
||||
t.Errorf(err.Error())
|
||||
return
|
||||
}
|
||||
if state.Status != types.ProcessStateCompleted {
|
||||
t.Errorf("process %s want %s got %s", restarting, types.ProcessStateCompleted, state.Status)
|
||||
return
|
||||
}
|
||||
state, err = runner.GetProcessState(notRestarting)
|
||||
if err != nil {
|
||||
t.Errorf(err.Error())
|
||||
return
|
||||
}
|
||||
if state.Status != types.ProcessStateRunning {
|
||||
t.Errorf("process %s is not running", notRestarting)
|
||||
return
|
||||
}
|
||||
err = runner.StopProcess(notRestarting)
|
||||
if err != nil {
|
||||
t.Errorf(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
state, err = runner.GetProcessState(notRestarting)
|
||||
if err != nil {
|
||||
t.Errorf(err.Error())
|
||||
return
|
||||
}
|
||||
if state.Status != types.ProcessStateCompleted {
|
||||
t.Errorf("process %s is running", notRestarting)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user