https://github.com/yukihirop/eg_foreman
Example in rust of the core part of foreman
https://github.com/yukihirop/eg_foreman
example foreman io multiprocess multithread pipe rust signals
Last synced: 11 months ago
JSON representation
Example in rust of the core part of foreman
- Host: GitHub
- URL: https://github.com/yukihirop/eg_foreman
- Owner: yukihirop
- Created: 2020-11-15T14:53:39.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-11-23T08:02:27.000Z (over 5 years ago)
- Last Synced: 2025-01-02T01:43:02.154Z (over 1 year ago)
- Topics: example, foreman, io, multiprocess, multithread, pipe, rust, signals
- Language: Rust
- Homepage:
- Size: 55.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Example in rust of the core part of foreman
It is an implementation example in rust of IO processing and signal processing in multithread as done by [foreman](https://github.com/ddollar/foreman) of ruby.
- IO process in multi thread
- handle signlas in another thread
- child wait in another thread
Since it is a sample, unlike `foreman`, it does not have the flexibility to define a process in `Procfile`.
The processes that can be executed are as follows.
|process|concurrency|command|
|---|-----------|-------|
|exit_0|1| `sleep 5 && echo 'success' && exit 0;`|
|exit_1|1| `sleep 5 && echo 'failed' && exit 1;`|
|loop|2|`while :; do sleep 1 && echo 'hello world'; done;`|
The behavior is that when exit_0 or exit_1 exits after 5 seconds, the remaining child processes will be signaled with a `SIGTERM` and killed.

```
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.08s
Running `target/debug/eg_foreman`
system | exit_0.1 start at pid: 11350
system | loop.1 start at pid: 11351
system | exit_1.1 start at pid: 11352
system | loop.2 start at pid: 11353
loop.2 | hello world
loop.1 | hello world
loop.1 | hello world
loop.2 | hello world
loop.2 | hello world
loop.1 | hello world
loop.1 | hello world
loop.2 | hello world
exit_1.1 | failed
exit_0.1 | success
system | sending SIGTERM for loop.1 at pid 11351
system | sending SIGTERM for exit_1.1 at pid 11352
system | sending SIGTERM for loop.2 at pid 11353
system | exit 0
```
If ctrl-c is detected within 5 seconds, `SIGTERM` will be sent to all child processes and the process will be killed.

```
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.09s
Running `target/debug/eg_foreman`
system | exit_0.1 start at pid: 43204
system | loop.1 start at pid: 43205
system | exit_1.1 start at pid: 43206
system | loop.2 start at pid: 43207
loop.2 | hello world
loop.1 | hello world
loop.1 | hello world
loop.2 | hello world
^Csystem | ctrl-c detected
system | sending SIGTERM for children
system | sending SIGTERM for exit_0.1 at pid 43204
system | sending SIGTERM for loop.1 at pid 43205
system | sending SIGTERM for exit_1.1 at pid 43206
system | sending SIGTERM for loop.2 at pid 43207
system | exit 0
```
If you generalize this, you can make a foreman. You did it. 🎉
## Development
Executte Test
```bash
cargo test
# or
cargo test -- --nocapture
```
## Environment
|name|desc|defaul|
|----|----|------|
|COLOR|Color the output|true|
## Reference
I really referred to the implementation of the following repository.
- [fors](https://github.com/jtdowney/fors)
- [arpx](https://github.com/jaredgorski/arpx)