https://github.com/chainguard-dev/exitdir
Common packages.
https://github.com/chainguard-dev/exitdir
Last synced: 9 months ago
JSON representation
Common packages.
- Host: GitHub
- URL: https://github.com/chainguard-dev/exitdir
- Owner: chainguard-dev
- License: apache-2.0
- Created: 2022-04-13T18:34:08.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-21T19:12:06.000Z (10 months ago)
- Last Synced: 2025-03-22T00:05:26.700Z (10 months ago)
- Language: Go
- Homepage:
- Size: 41 KB
- Stars: 8
- Watchers: 1
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ExitDir
ExitDir is a library to signal a process should exit via the filesystem.
## Usage
To use this library, on the leader process:
```go
package main
import "chainguard.dev/exitdir"
func main() {
// Signal the other processes should exit via a new file in `EXIT_DIR`.
defer exitdir.Exit();
// ...rest of implementation.
}
```
And then one or more follower processes:
```go
package main
import (
"context"
"chainguard.dev/exitdir"
)
func main() {
// Decorate a context with ExitDir awareness. ExitDir will cancel the
// returned context when a new file in `EXIT_DIR` is created.
ctx := exitdir.Aware(context.Background())
// ...rest of implementation using `ctx` for lifecycle control.
}
```
## Process Demo
This functionally is shown locally by using a temp directory and two processes:
```shell
export EXIT_DIR=`mktemp -d`
go run ./cmd/follower &
go run ./cmd/leader
```
Results in:
```shell
$ export EXIT_DIR=`mktemp -d`
$ go run ./cmd/follower &
[1] 83528
$ go run ./cmd/leader
[Leader] Doing work...
[Follower] Tick 0
[Follower] Tick 1
[Follower] Tick 2
[Follower] Tick 3
[Leader] Exiting...
[Follower] Exiting...
[1]+ Done go run ./cmd/follower
$
```
## Kubernetes Demo
Often in Kubernetes a job with multiple containers need to solve a problem of
signaling when to exit. If not coordinated, a resulting hung job looks like a
failure, but in-fact was successful except one container never exited.
```shell
ko apply -f - <