Skip to content

Commit

Permalink
Merge pull request #6 from kuno1/issue/5
Browse files Browse the repository at this point in the history
Add maximum lifetime feature
Close #5.
  • Loading branch information
Harai Akihiro authored Jan 13, 2019
2 parents 011e27a + e0eb50c commit b3017e4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# Immortalize
## Usage

```
immortalize -min-lifetime 30 -max-lifetime 60 -command test-bin/command
```
25 changes: 20 additions & 5 deletions immortalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

var log = logrus.New()

func run(minLifetime int, command string) {
func run(minLifetime uint, maxLifetime uint, command string) {
cmd := exec.Command(command)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
Expand All @@ -29,6 +29,15 @@ func run(minLifetime int, command string) {

lifetimeChan := make(chan bool)

if 0 < maxLifetime {
go func() {
time.Sleep(time.Duration(maxLifetime) * time.Second)
log.Info("Maximum lifetime has passed.")
cmd.Process.Signal(syscall.SIGTERM)
log.Info("Signal 'terminated' has been sent to the process.")
}()
}

go func() {
time.Sleep(time.Duration(minLifetime) * time.Second)
log.Info("Minimum lifetime has passed.")
Expand All @@ -40,11 +49,11 @@ func run(minLifetime int, command string) {
go func() {
for s := range signalChan {
if s == syscall.SIGTERM {
log.Info("SIGTERM has been received.")
log.Infof("Signal '%v' has been received.", s)
<-lifetimeChan
}
log.Infof("Signal '%v' has been forwarded to the process.", s)
cmd.Process.Signal(s)
log.Infof("Signal '%v' has been forwarded to the process.", s)
}
}()

Expand All @@ -71,8 +80,10 @@ func configLog(level string) error {
}

func main() {
minLifetimePtr := flag.Int("min-lifetime", 0,
minLifetimePtr := flag.Uint("min-lifetime", 0,
"Time duration for minimum process lifetime in seconds")
maxLifetimePtr := flag.Uint("max-lifetime", 0,
"Time duration for maximum process lifetime in seconds")
commandPtr := flag.String("command", "command", "command to immortalize")
levelPtr := flag.String(
"log-level", "info", "Log level: 'info', 'debug', or 'trace'")
Expand All @@ -82,5 +93,9 @@ func main() {

log.Debugf("PID: %v", os.Getpid())

run(*minLifetimePtr, *commandPtr)
if 0 < *maxLifetimePtr && *maxLifetimePtr < *minLifetimePtr {
log.Fatal("min-lifetime cannot be higher than max-lifetime.")
}

run(*minLifetimePtr, *maxLifetimePtr, *commandPtr)
}
61 changes: 53 additions & 8 deletions test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ measure () {
# Measure immortalize and redirect stdout/stderr to file descriptor 3.
# Details:
# https://github.com/bats-core/bats-core#file-descriptor-3-read-this-if-bats-hangs
"$time_cmd" -f '%e' -o work/time ./work/immortalize "$@" >&3 2>&1 &
"$time_cmd" --quiet -f '%e' -o work/time ./work/immortalize "$@" >&3 2>&1 &
# Store time's PID
echo "$!" > work/pid
}
Expand All @@ -33,21 +33,47 @@ sigterm () {
pkill -P "$(< work/pid)"
}

# MIN -> COM
wait_exit () {
set +e
wait "$(< work/pid)"
code="$?"
set -e
>&3 echo "Exited with exit code $code."
return "$code"
}

# * `COM`: Command exited
# * `MAX`: Maximum lifetime exceeded
# * `MIN`: Minimum lifetime exceeded
# * `SIG`: SIGTERM received
#
# MIN -> COM -> MAX -> SIG
# MIN -> COM -> SIG -> MAX
@test "MIN -> COM" {
measure -min-lifetime 2 -command test-bin/command-zero
wait
wait_exit
[ "$(result)" == 4 ]
}

# COM -> MIN
@test "COM -> MIN" {
# COM -> MIN -> MAX -> SIG
# COM -> MIN -> SIG -> MAX
# COM -> SIG -> MIN -> MAX
@test "COM" {
measure -min-lifetime 6 -command test-bin/command-zero
wait
wait_exit
[ "$(result)" == 4 ]
}

# SIG -> MIN
# MIN -> MAX -> COM -> SIG
# MIN -> MAX -> SIG -> COM
@test "MIN -> MAX" {
measure -max-lifetime 2 -command test-bin/command-zero
wait
[ "$(result)" == 2 ]
}

# SIG -> MIN -> MAX -> COM
# SIG -> MIN -> COM -> MAX
@test "SIG -> MIN" {
measure -min-lifetime 2 -command test-bin/command-zero
sleep 1
Expand All @@ -56,11 +82,30 @@ sigterm () {
[ "$(result)" == 2 ]
}

# MIN -> SIG
# MIN -> SIG -> COM -> MAX
# MIN -> SIG -> MAX -> COM
@test "MIN -> SIG" {
measure -min-lifetime 2 -command test-bin/command-zero
sleep 3
sigterm
wait
[ "$(result)" == 3 ]
}

# SIG -> COM -> MIN -> MAX
@test "SIG -> COM" {
measure -min-lifetime 6 -command test-bin/command-zero
sleep 2
sigterm
wait
[ "$(result)" == 4 ]
}

# Invalid order
@test "MAX -> MIN" {
measure -min-lifetime 4 -max-lifetime 2 -command test-bin/command-zero
if wait_exit; then
exit 1
fi
[ "$(result)" == 0 ]
}

0 comments on commit b3017e4

Please sign in to comment.