main.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // prime reruns a command until it finishes under a certain limit.
  2. // usefull for filling caches.
  3. package main
  4. import (
  5. "flag"
  6. "fmt"
  7. "io"
  8. "log"
  9. "os"
  10. "os/exec"
  11. "time"
  12. "github.com/cryptix/go/logging"
  13. )
  14. var limit = flag.Duration("d", 10*time.Second, "what is the limit to stop at")
  15. func main() {
  16. flag.Parse()
  17. args := flag.Args()
  18. if len(args) == 0 {
  19. log.Fatalln("Not enough arguments")
  20. }
  21. for {
  22. start := time.Now()
  23. cmd := exec.Command(args[0], args[1:]...)
  24. stdout, err := cmd.StdoutPipe()
  25. logging.CheckFatal(err)
  26. stderr, err := cmd.StderrPipe()
  27. logging.CheckFatal(err)
  28. logging.CheckFatal(cmd.Start())
  29. go io.Copy(os.Stdout, stdout)
  30. go io.Copy(os.Stderr, stderr)
  31. logging.CheckFatal(cmd.Wait())
  32. if !cmd.ProcessState.Success() {
  33. fmt.Fprintf(os.Stderr, "exec failed. stopping.")
  34. os.Exit(0)
  35. }
  36. took := time.Since(start)
  37. if took < *limit {
  38. fmt.Fprintf(os.Stderr, "limit reached. (took %v)", took)
  39. os.Exit(0)
  40. }
  41. fmt.Fprintf(os.Stderr, "Done. (took %v)\n", took)
  42. }
  43. }