-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (64 loc) · 1.64 KB
/
main.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"
"log"
"strings"
"time"
)
var (
lockWaitTimeMS = flag.Int(
"lockwaittime",
500,
"Configures the wait time for a lock in milliseconds",
)
failOnLockWaitExpiration = flag.Bool(
"failwithoutlock",
false,
"Exists with status code 1 if the lock was not received within lockwaittime",
)
minLockTimeMS = flag.Int(
"minlocktime",
5000,
"Configures the minimum time in milliseconds a lock is held",
)
maxExecutionTimeMS = flag.Int(
"maxexecutiontime",
0,
"Configures the maximum time in milliseconds the execution of the given command can take",
)
endpoint = flag.String("endpoint", "http://localhost:8500", "endpoint")
token = flag.String("token", "", "consul authentication token. leave blank if none applicable")
key = flag.String("key", "none", "key to monitor, e.g. cronjobs/any_service/cron_name")
)
func checkFlag(f *string, name string) {
if *f == "none" || *f == "" {
log.Fatalf("Setting %s is mandatory", name)
}
}
func main() {
flag.Parse()
// Ensure flags are given
checkFlag(endpoint, "endpoint")
checkFlag(key, "key")
// original command
command := strings.Join(flag.Args(), " ")
// Initiate command locker
ccl, err := NewConsulCommandLocker(
*endpoint,
*token,
time.Duration(*lockWaitTimeMS)*time.Millisecond,
time.Duration(*minLockTimeMS)*time.Millisecond,
time.Duration(*maxExecutionTimeMS)*time.Millisecond,
*failOnLockWaitExpiration,
)
if err != nil {
log.Fatalf("%v", err)
}
output, err := ccl.LockAndExecute(*key, command)
if err != nil {
log.Fatalf("%v\nCommand output: %s", err, output)
}
fmt.Println("Command output:")
fmt.Print(output)
}