added pidfile, hup handling, truncate option

master
Jordan Orelli 10 years ago
parent 0c99600235
commit 6ea315ab11

@ -5,14 +5,17 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"os/signal"
"syscall"
"time" "time"
) )
var ( var (
fname string fname string
fappend bool ftruncate bool
reopen bool reopen bool
freq float64 freq float64
pidfile string
) )
// generates a pseudorandom string of length n that is composed of alphanumeric // generates a pseudorandom string of length n that is composed of alphanumeric
@ -31,11 +34,11 @@ func outFile() (*os.File, error) {
return os.Stdout, nil return os.Stdout, nil
} }
options := os.O_WRONLY | os.O_CREATE options := os.O_WRONLY | os.O_CREATE
if fappend { if ftruncate {
options |= os.O_APPEND options |= os.O_TRUNC
} else { } else {
options |= os.O_TRUNC options |= os.O_APPEND
} }
return os.OpenFile(fname, options, 0644) return os.OpenFile(fname, options, 0644)
} }
@ -55,16 +58,25 @@ func reopenWrite(c chan string) {
} }
func regularWrite(c chan string) { func regularWrite(c chan string) {
hup := make(chan os.Signal, 1)
signal.Notify(hup, syscall.SIGHUP, syscall.SIGUSR1)
START:
f, err := outFile() f, err := outFile()
if err != nil { if err != nil {
fmt.Printf("ERROR: unable to open outfile: %v", err) fmt.Printf("ERROR: unable to open outfile: %v", err)
return return
} }
defer f.Close()
for line := range c { for {
if _, err := f.WriteString(line); err != nil { select {
fmt.Printf("ERROR: unable to write line: %v", err) case line := <-c:
if _, err := f.WriteString(line); err != nil {
fmt.Printf("ERROR: unable to write line: %v", err)
}
case <-hup:
f.Close()
goto START
} }
} }
} }
@ -77,8 +89,21 @@ func writeLines(c chan string) {
} }
} }
func writePid() {
if pidfile == "" {
return
}
f, err := os.OpenFile(pidfile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR unable to open pidfile: %v", err)
return
}
fmt.Fprintf(f, "%d\n", os.Getpid())
}
func main() { func main() {
flag.Parse() flag.Parse()
writePid()
c := make(chan string) c := make(chan string)
writeLines(c) writeLines(c)
@ -90,8 +115,9 @@ func main() {
func init() { func init() {
flag.StringVar(&fname, "file", "", "destination file to which random data will be written") flag.StringVar(&fname, "file", "", "destination file to which random data will be written")
flag.BoolVar(&fappend, "append", false, "append to file instead of truncating file on open") flag.StringVar(&pidfile, "pidfile", "", "file to which a pid is written")
flag.BoolVar(&ftruncate, "truncate", false, "truncate file on opening instead of appending")
flag.BoolVar(&reopen, "reopen", false, "reopen file handle on every write instead of using a persistent handle") flag.BoolVar(&reopen, "reopen", false, "reopen file handle on every write instead of using a persistent handle")
flag.Float64Var(&freq, "freq", 1, "frequency in hz at which lines will be written") flag.Float64Var(&freq, "freq", 10, "frequency in hz at which lines will be written")
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
} }

Loading…
Cancel
Save