Issue #149: Don't restart processes on manual stop

This commit is contained in:
Berger Eugene 2024-03-08 01:11:23 +02:00
parent 1a73f446a9
commit 3bf8bd3382
3 changed files with 98 additions and 2 deletions

View File

@ -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() {

View File

@ -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)
}

View File

@ -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
}
}