-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd_splay.go
76 lines (58 loc) · 1.85 KB
/
cmd_splay.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
73
74
75
76
package main
import (
"flag"
"fmt"
"math/rand"
"strconv"
"time"
)
// Structure for our options and state.
type splayCommand struct {
max int
verbose bool
}
// Arguments adds per-command args to the object.
func (s *splayCommand) Arguments(f *flag.FlagSet) {
f.IntVar(&s.max, "maximum", 300, "The maximum amount of time to sleep for")
f.BoolVar(&s.verbose, "verbose", false, "Should we be verbose")
}
// Info returns the name of this subcommand.
func (s *splayCommand) Info() (string, string) {
return "splay", `Sleep for a random time.
Details:
This command allows you to stagger execution of things via the introduction
of random delays.
The expected use-case is that you have a number of hosts which each wish
to perform a cron-job, but you don't want to overwhelm a central system
by having all those events occur at precisely the same time (which is
likely to happen if you're running with good clocks).
Give each script a random-delay via adding a call to the splay subcommand.
Usage:
We prefer users to specify the splay-time with a parameter, but to allow
natural usage you may specify as the first argument:
$ sysbox splay --maximum=10 [-verbose]
$ sysbox splay 10 [-verbose]`
}
// Execute is invoked if the user specifies `splay` as the subcommand.
func (s *splayCommand) Execute(args []string) int {
// If the user gave an argument then use it.
//
// Because people might expect this to work.
if len(args) > 0 {
// First argument will be a number
num, err := strconv.Atoi(args[0])
if err != nil {
fmt.Printf("error converting %s to integer: %s\n", args[0], err.Error())
}
// Save it away.
s.max = num
}
// Get the delay-time.
delay := rand.Intn(s.max)
if s.verbose {
fmt.Printf("Sleeping for for %d seconds, from max splay-time of %d\n", delay, s.max)
}
// Sleep
time.Sleep(time.Duration(delay) * time.Second)
return 0
}