-
Notifications
You must be signed in to change notification settings - Fork 22
/
runner.go
72 lines (62 loc) · 1.42 KB
/
runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"time"
)
var graLogDir = flag.String(
"graLogDir",
"",
"Specifies the directory from which to purge GRA log files.",
)
var graLogDaysToKeep = flag.Int(
"graLogDaysToKeep",
60,
"Specifies the maximum age of the GRA log files allowed.",
)
var pidfile = flag.String(
"pidfile",
"",
"The location for the pidfile",
)
func main() {
flag.Parse()
err := ioutil.WriteFile(*pidfile, []byte(strconv.Itoa(os.Getpid())), 0644)
if err != nil {
panic(err)
}
for {
out, err := runCommand("sh", "/var/vcap/jobs/gra-log-purger/bin/gra-log-purger.sh", *graLogDir, strconv.Itoa(*graLogDaysToKeep))
if err != nil {
LogErrorWithTimestamp(err)
}
LogWithTimestamp(out)
LogWithTimestamp("Sleeping for one hour\n")
time.Sleep(1 * time.Hour)
}
}
// Runs command with stdout and stderr pipes connected to process
func runCommand(executable string, args ...string) (string, error) {
cmd := exec.Command(executable, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return string(out), err
}
return string(out), nil
}
func LogWithTimestamp(format string, args ...interface{}) {
fmt.Printf("[%s] - ", time.Now().Local())
if nil == args {
fmt.Printf(format)
} else {
fmt.Printf(format, args...)
}
}
func LogErrorWithTimestamp(err error) {
fmt.Fprintf(os.Stderr, "[%s] - ", time.Now().Local())
fmt.Fprintf(os.Stderr, err.Error()+"\n")
}