forked from cmaster11/overseer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_enqueue.go
156 lines (138 loc) · 3.62 KB
/
cmd_enqueue.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Enqueue
//
// The enqueue sub-command adds parsed tests to a central redis queue.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/cmaster11/overseer/parser"
"github.com/cmaster11/overseer/test"
"github.com/go-redis/redis"
"github.com/google/subcommands"
)
type enqueueCmd struct {
RedisDB int
RedisHost string
RedisPassword string
RedisSocket string
RedisDialTimeout time.Duration
_r *redis.Client
}
//
// Glue
//
func (*enqueueCmd) Name() string { return "enqueue" }
func (*enqueueCmd) Synopsis() string { return "Enqueue a parsed configuration file" }
func (*enqueueCmd) Usage() string {
return `enqueue :
Add the tests from a parsed configuration file to a central redis queue.
`
}
//
// Flag setup.
//
func (p *enqueueCmd) SetFlags(f *flag.FlagSet) {
//
// Create the default options here
//
// This is done so we can load defaults via a configuration-file
// if present.
//
var defaults enqueueCmd
defaults.RedisHost = "localhost:6379"
defaults.RedisPassword = ""
defaults.RedisDB = 0
defaults.RedisSocket = ""
defaults.RedisDialTimeout = 5 * time.Second
//
// If we have a configuration file then load it
//
if len(os.Getenv("OVERSEER")) > 0 {
cfg, err := ioutil.ReadFile(os.Getenv("OVERSEER"))
if err == nil {
err = json.Unmarshal(cfg, &defaults)
if err != nil {
fmt.Printf("WARNING: Error loading overseer.json - %s\n",
err.Error())
}
} else {
fmt.Printf("WARNING: Failed to read configuration-file - %s\n", err.Error())
}
}
f.IntVar(&p.RedisDB, "redis-db", defaults.RedisDB, "Specify the database-number for redis.")
f.StringVar(&p.RedisHost, "redis-host", defaults.RedisHost, "Specify the address of the redis queue.")
f.StringVar(&p.RedisPassword, "redis-pass", defaults.RedisPassword, "Specify the password for the redis queue.")
f.StringVar(&p.RedisSocket, "redis-socket", defaults.RedisSocket, "If set, will be used for the redis connections.")
f.DurationVar(&p.RedisDialTimeout, "redis-timeout", defaults.RedisDialTimeout, "Redis connection timeout.")
}
//
// This is a callback invoked by the parser when a job
// has been successfully parsed.
//
func (p *enqueueCmd) enqueueTest(tst test.Test) error {
_, err := p._r.RPush("overseer.jobs", tst.Input).Result()
return err
}
//
// Entry-point.
//
func (p *enqueueCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
//
// Connect to the redis-host.
//
if p.RedisSocket != "" {
p._r = redis.NewClient(&redis.Options{
Network: "unix",
Addr: p.RedisSocket,
Password: p.RedisPassword,
DB: p.RedisDB,
DialTimeout: p.RedisDialTimeout,
})
} else {
p._r = redis.NewClient(&redis.Options{
Addr: p.RedisHost,
Password: p.RedisPassword,
DB: p.RedisDB,
DialTimeout: p.RedisDialTimeout,
})
}
//
// And run a ping, just to make sure it worked.
//
_, err := p._r.Ping().Result()
if err != nil {
fmt.Printf("Redis connection failed: %s\n", err.Error())
return subcommands.ExitFailure
}
//
// For each file on the command-line we can now parse and
// enqueue the jobs
//
for _, file := range f.Args() {
//
// Create an object to parse our file.
//
helper := parser.New()
//
// For each parsed job call `enqueueTest`.
//
errParse := helper.ParseFile(file, p.enqueueTest)
//
// Did we see an error?
//
if errParse != nil {
fmt.Printf("Error parsing file: %s\n", errParse)
return subcommands.ExitFailure
}
// Did we read from stdin?
if file == "-" {
break
}
}
return subcommands.ExitSuccess
}