Skip to content

Commit

Permalink
add working dir to probe exec
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Jul 20, 2023
1 parent 89faad4 commit a632c49
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 20 deletions.
Empty file.
24 changes: 24 additions & 0 deletions issues/issue_72/process-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "0.5"
log_level: debug
log_length: 300

processes:
clientA:
working_dir: "clientA"
is_daemon: true
command: "sleep 10 && touch ready"
shutdown:
command: "rm ready"
readiness_probe:
exec:
command: "echo $(pwd) > ready-check && test -f ready"
clientB:
command: "echo all done!"
depends_on:
clientA:
condition: process_healthy

_pc_log:
command: "tail -f -n100 process-compose-${USER}.log"
working_dir: "/tmp"

3 changes: 2 additions & 1 deletion src/app/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ func (p *Process) doConfiguredStop(params types.ShutDownParams) error {
defer p.notifyDaemonStopped()

cmd := command.BuildCommandShellArgContext(ctx, p.shellConfig, params.ShutDownCommand)
cmd.Env = p.getProcessEnvironment()
cmd.SetEnv(p.getProcessEnvironment())
cmd.SetDir(p.procConf.WorkingDir)

if err := cmd.Run(); err != nil {
// the process termination timedout and it will be killed
Expand Down
12 changes: 8 additions & 4 deletions src/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ func BuildCommandShellArg(shell ShellConfig, cmd string) *CmdWrapper {
//return NewMockCommand()
}

func BuildCommandContext(ctx context.Context, shellCmd string) *exec.Cmd {
return exec.CommandContext(ctx, getRunnerShell(), getRunnerArg(), shellCmd)
func BuildCommandContext(ctx context.Context, shellCmd string) *CmdWrapper {
return &CmdWrapper{
cmd: exec.CommandContext(ctx, getRunnerShell(), getRunnerArg(), shellCmd),
}
}

func BuildCommandShellArgContext(ctx context.Context, shell ShellConfig, cmd string) *exec.Cmd {
return exec.CommandContext(ctx, shell.ShellCommand, shell.ShellArgument, cmd)
func BuildCommandShellArgContext(ctx context.Context, shell ShellConfig, cmd string) *CmdWrapper {
return &CmdWrapper{
cmd: exec.CommandContext(ctx, shell.ShellCommand, shell.ShellArgument, cmd),
}
}

func getRunnerShell() string {
Expand Down
22 changes: 13 additions & 9 deletions src/command/command_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,41 @@ import (
)

type CmdWrapper struct {
Cmd *exec.Cmd
cmd *exec.Cmd
}

func (c *CmdWrapper) Start() error {
return c.Cmd.Start()
return c.cmd.Start()
}

func (c *CmdWrapper) Run() error {
return c.cmd.Run()
}

func (c *CmdWrapper) Wait() error {
return c.Cmd.Wait()
return c.cmd.Wait()
}

func (c *CmdWrapper) ExitCode() int {
return c.Cmd.ProcessState.ExitCode()
return c.cmd.ProcessState.ExitCode()
}

func (c *CmdWrapper) Pid() int {
return c.Cmd.Process.Pid
return c.cmd.Process.Pid
}

func (c *CmdWrapper) StdoutPipe() (io.ReadCloser, error) {
return c.Cmd.StdoutPipe()
return c.cmd.StdoutPipe()
}

func (c *CmdWrapper) StderrPipe() (io.ReadCloser, error) {
return c.Cmd.StderrPipe()
return c.cmd.StderrPipe()
}

func (c *CmdWrapper) SetEnv(env []string) {
c.Cmd.Env = env
c.cmd.Env = env
}

func (c *CmdWrapper) SetDir(dir string) {
c.Cmd.Dir = dir
c.cmd.Dir = dir
}
8 changes: 5 additions & 3 deletions src/health/exec_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ import (
)

type execChecker struct {
command string
timeout int
command string
timeout int
workingDir string
}

func (c *execChecker) Status() (interface{}, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.timeout)*time.Second)
defer cancel()

cmd := command.BuildCommandContext(ctx, c.command)
cmd.SetDir(c.workingDir)

if err := cmd.Run(); err != nil {
return nil, err
}

return map[string]int{"exit_code": cmd.ProcessState.ExitCode()}, nil
return map[string]int{"exit_code": cmd.ExitCode()}, nil
}
5 changes: 3 additions & 2 deletions src/health/health_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func (p *Prober) getHttpChecker() (health.ICheckable, error) {

func (p *Prober) getExecChecker() (health.ICheckable, error) {
return &execChecker{
command: p.probe.Exec.Command,
timeout: p.probe.TimeoutSeconds,
command: p.probe.Exec.Command,
timeout: p.probe.TimeoutSeconds,
workingDir: p.probe.Exec.WorkingDir,
}, nil
}
3 changes: 2 additions & 1 deletion src/health/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Probe struct {
}

type ExecProbe struct {
Command string `yaml:"command,omitempty"`
Command string `yaml:"command,omitempty"`
WorkingDir string `yaml:"working_dir,omitempty"`
}

type HttpProbe struct {
Expand Down
17 changes: 17 additions & 0 deletions src/types/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (p *Project) Validate() {
func (p *Project) ValidateAfterMerge() error {
p.assignDefaultProcessValues()
p.cloneReplicas()
p.copyWorkingDirToProbes()
return p.validateNoCircularDependencies()
}

Expand Down Expand Up @@ -77,6 +78,22 @@ func (p *Project) assignDefaultProcessValues() {
}
}

func (p *Project) copyWorkingDirToProbes() {
for name, proc := range p.Processes {
if proc.LivenessProbe != nil &&
proc.LivenessProbe.Exec != nil &&
proc.LivenessProbe.Exec.WorkingDir == "" {
proc.LivenessProbe.Exec.WorkingDir = proc.WorkingDir
}
if proc.ReadinessProbe != nil &&
proc.ReadinessProbe.Exec != nil &&
proc.ReadinessProbe.Exec.WorkingDir == "" {
proc.ReadinessProbe.Exec.WorkingDir = proc.WorkingDir
}
p.Processes[name] = proc
}
}

func (p *Project) cloneReplicas() {
procsToAdd := make([]ProcessConfig, 0)
procsToDel := make([]string, 0)
Expand Down

0 comments on commit a632c49

Please sign in to comment.