Skip to content

Commit

Permalink
fix: wait for processes to start when condition: process_started
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored and F1bonacc1 committed Mar 2, 2024
1 parent df422e3 commit 1a73f44
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
70 changes: 44 additions & 26 deletions src/app/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,34 @@ const (

type Process struct {
sync.Mutex
globalEnv []string
confMtx sync.Mutex
procConf *types.ProcessConfig
procState *types.ProcessState
stateMtx sync.Mutex
procCond sync.Cond
procStateChan chan string
procReadyCtx context.Context
readyCancelFn context.CancelFunc
procRunCtx context.Context
runCancelFn context.CancelFunc
procColor func(a ...interface{}) string
noColor func(a ...interface{}) string
redColor func(a ...interface{}) string
logBuffer *pclog.ProcessLogBuffer
logger pclog.PcLogger
command Commander
done bool
timeMutex sync.Mutex
startTime time.Time
liveProber *health.Prober
readyProber *health.Prober
shellConfig command.ShellConfig
printLogs bool
isMain bool
extraArgs []string
globalEnv []string
confMtx sync.Mutex
procConf *types.ProcessConfig
procState *types.ProcessState
stateMtx sync.Mutex
procCond sync.Cond
procStartedCond sync.Cond
procStateChan chan string
procReadyCtx context.Context
readyCancelFn context.CancelFunc
procRunCtx context.Context
runCancelFn context.CancelFunc
procColor func(a ...interface{}) string
noColor func(a ...interface{}) string
redColor func(a ...interface{}) string
logBuffer *pclog.ProcessLogBuffer
logger pclog.PcLogger
command Commander
started bool
done bool
timeMutex sync.Mutex
startTime time.Time
liveProber *health.Prober
readyProber *health.Prober
shellConfig command.ShellConfig
printLogs bool
isMain bool
extraArgs []string
}

func NewProcess(
Expand All @@ -82,6 +84,7 @@ func NewProcess(
redColor: color.New(color.FgHiRed).SprintFunc(),
noColor: color.New(color.Reset).SprintFunc(),
logger: logger,
started: false,
done: false,
logBuffer: procLog,
shellConfig: shellConfig,
Expand All @@ -95,6 +98,7 @@ func NewProcess(
proc.procRunCtx, proc.runCancelFn = context.WithCancel(context.Background())
proc.setUpProbes()
proc.procCond = *sync.NewCond(proc)
proc.procStartedCond = *sync.NewCond(proc)
return proc
}

Expand Down Expand Up @@ -272,6 +276,15 @@ func (p *Process) isRestartable() bool {
return false
}

func (p *Process) waitForStarted() {
p.Lock()
defer p.Unlock()

for !p.started {
p.procStartedCond.Wait()
}
}

func (p *Process) waitForCompletion() int {
p.Lock()
defer p.Unlock()
Expand Down Expand Up @@ -359,6 +372,11 @@ func (p *Process) onProcessStart() {
if isStringDefined(p.procConf.LogLocation) {
p.logger.Open(p.getLogPath(), p.procConf.LoggerConfig)
}

p.Lock()
p.started = true
p.Unlock()
p.procStartedCond.Broadcast()
}

func (p *Process) onProcessEnd(state string) {
Expand Down
4 changes: 3 additions & 1 deletion src/app/project_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func (p *ProjectRunner) waitIfNeeded(process *types.ProcessConfig) error {
if !ready {
return fmt.Errorf("process %s depended on %s to become ready, but it was terminated", process.ReplicaName, k)
}

case types.ProcessConditionStarted:
log.Info().Msgf("%s is waiting for %s to start", process.ReplicaName, k)
runningProc.waitForStarted()
}
} else {
log.Error().Msgf("Error: process %s depends on %s, but it isn't running", process.ReplicaName, k)
Expand Down

0 comments on commit 1a73f44

Please sign in to comment.