Skip to content

Commit

Permalink
feat: availability.exit_on_end (#78)
Browse files Browse the repository at this point in the history
* feat: availability.exit_on_end

a bool flag, if set to true, shuts down all processes once
given process ends (regardless of exit code)

this can be used together with `availability.restart = on_failure` to
have the process restarted on failure and only exit PC on success

closes #77

* docs: document availability.exit_on_end
  • Loading branch information
adrian-gierakowski authored Jul 21, 2023
1 parent 328fecd commit bd60041
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ process2:

##### Terminate Process Compose on Failure

There are cases when you would like the `process-compose` to terminate immediately when one of the processes exits with non `0` exit code. This can be useful when you would like to perform "pre-flight" validation checks on the environment.
There are cases when you might like `process-compose` to terminate immediately when one of the processes exits with non `0` exit code. This can be useful when you would like to perform "pre-flight" validation checks on the environment.

To achieve that, use `exit_on_failure` restart policy. If defined, `process-compose` will gracefully shut down all the other running processes and exit with the same exit code as the failed process.

Expand All @@ -491,7 +491,41 @@ other_proc:
condition: process_completed_successfuly
```

##### Terminate Process Compose once given process ends

There are cases when you might like `process-compose` to terminate immediately when one of the processes exits (regardless of exit code). For example when running tests which depend on other processes like databases etc.. You might want the processes, on which the test process depends, to start first, then run the tests, and finally terminate all processes once the test process exits, reporting the code returned by the test process.

To achieve that, set `availability.exit_on_end` to `true`, and `process-compose` will gracefully shut down all the other running processes and exit with the same exit code as the given process.

```yaml
tests:
command: tests-run
availability:
# NOTE: `restart: exit_on_failure` is not needed since
# exit_on_end implies it.
exit_on_end: true
depends_on:
redis: process_healthy
postgres: process_healthy

redis:
command: redis-start
readiness_probe:
exec:
command: redis-health-check

postgres:
command: postgres-start
readiness_probe:
exec:
command: postgres-health-check
```
> **Note**
> setting `restart: exit_on_failure` together with `exit_on_end: true` is not needed as the latter causes termination regardless of the exit code. However it might be sometimes useful to `exit_on_end` with `restart: on_failure` and `max_restarts` in case you want the process to recover from failure and only cause termination on success.

> **Note**
> `exit_on_end` can be set on more than one process, for example when running multiple tasks in parallel and wishing to terminate as soon as any one finished.

#### Environment Variables

Expand Down
3 changes: 2 additions & 1 deletion src/app/project_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ func (p *ProjectRunner) waitIfNeeded(process types.ProcessConfig) error {
}

func (p *ProjectRunner) onProcessEnd(exitCode int, procConf types.ProcessConfig) {
if exitCode != 0 && procConf.RestartPolicy.Restart == types.RestartPolicyExitOnFailure {
if (exitCode != 0 && procConf.RestartPolicy.Restart == types.RestartPolicyExitOnFailure) ||
procConf.RestartPolicy.ExitOnEnd {
p.ShutDownProject()
p.exitCode = exitCode
}
Expand Down
1 change: 1 addition & 0 deletions src/types/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type RestartPolicyConfig struct {
Restart string `yaml:",omitempty"`
BackoffSeconds int `yaml:"backoff_seconds,omitempty"`
MaxRestarts int `yaml:"max_restarts,omitempty"`
ExitOnEnd bool `yaml:"exit_on_end,omitempty"`
}

type ShutDownParams struct {
Expand Down

0 comments on commit bd60041

Please sign in to comment.