Skip to content

Instantly share code, notes, and snippets.

@thenoviceoof
Created June 25, 2013 10:51
Show Gist options
  • Save thenoviceoof/5857602 to your computer and use it in GitHub Desktop.
Save thenoviceoof/5857602 to your computer and use it in GitHub Desktop.
This is a small exercise, for making a program that will keep un-cooperative processes running when the processes are just lazy
/*
* phylactery.go
*******************************************************************************
* This is a small exercise, for making a program that will keep
* un-cooperative processes running when the processes are just lazy
*
* Do what you want with this code
*/
package main
import (
"log"
"os"
"os/exec"
"strings"
"time"
)
func run_command(program string, args []string) {
cmd := exec.Command(program, args...)
err := cmd.Run()
if err != nil {
log.Printf("%s crashed! Error: %s", program, err)
} else {
log.Printf("%s exited normally", program)
}
}
const RATE_LIMIT = 1000 * time.Millisecond
func main() {
// throw away this binary's name
args := os.Args[1:]
for {
log.Printf("Running %s...", strings.Join(args, " "))
before := time.Now()
run_command(args[0], args[1:])
after := time.Now()
// rate limit the retry
if after.Sub(before) < RATE_LIMIT {
difference := RATE_LIMIT - (after.Sub(before))
time.Sleep(difference)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment