-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
283 lines (228 loc) · 7.96 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Package main is the main entrypoint to the worker cleaner
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"regexp"
"strings"
"time"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/wearemolecule/worker_cleaner/kubernetes"
"gopkg.in/redis.v4"
)
const (
resqueWorkerKey = "resque:worker"
resqueWorkersKey = "resque:workers"
resqueQueueKey = "resque:queue"
resqueQueuesKey = "resque:queues"
resqueFailedQueueKey = "resque:failed"
statProcessedKey = "resque:stat:processed"
statFailedKey = "resque:stat:failed"
)
var (
blacklistedResqueQueues []string
namespace string
labelSelector string
kubeConfig string
)
func init() {
flag.Parse()
blacklistedResqueQueues = strings.Split(os.Getenv("BLACKLISTED_QUEUES"), ",")
namespace = os.Getenv("NAMESPACE")
labelSelector = os.Getenv("LABEL_SELECTOR")
kubeConfig = os.Getenv("KUBE_CONFIG")
}
func main() {
glog.V(0).Info("Kubernetes-Resque Worker Cleanup Service")
kubeClient, err := kubernetes.NewClient(kubeConfig)
if err != nil {
glog.Fatal(errors.Wrap(err, "Unable to create kubernetes client").Error())
}
redisClient := getRedisClient()
ticker := time.NewTicker(time.Minute * 5)
defer ticker.Stop()
for _ = range ticker.C {
glog.V(1).Infof("starting cleanup at %v...", time.Now())
kubernetesWorkers, err := getWorkersFromKubernetes(kubeClient)
if err != nil {
glog.Warning(errors.Wrap(err, "Unable to get workers from kubernetes").Error())
continue
}
redisWorkers, err := getWorkersFromRedis(redisClient)
if err != nil {
glog.Warning(errors.Wrap(err, "Unable to get workers from redis").Error())
continue
}
deadRedisWorkers := redisWorkersNotInKubernetes(redisWorkers, kubernetesWorkers)
glog.V(2).Infof("Found %d kubernetes workers", len(kubernetesWorkers))
glog.V(2).Info(kubernetesWorkers)
glog.V(2).Infof("Found %d redis workers", len(redisWorkers))
glog.V(2).Info(redisWorkers)
glog.V(2).Infof("Found %d dead redis workers", len(deadRedisWorkers))
glog.V(2).Info(deadRedisWorkers)
for _, redisWorker := range deadRedisWorkers {
if err := removeDeadRedisWorker(redisClient, redisWorker); err != nil {
glog.Warning(errors.Wrap(err, fmt.Sprintf("Unable to delete %s", redisWorker)).Error())
}
}
failedResqueJobs, err := getDirtyExitFailedJobsFromRedis(redisClient)
if err != nil {
glog.Warning(errors.Wrap(err, "Unable to get failed jobs from redis").Error())
continue
}
glog.V(2).Infof("Found %d failed resque jobs", len(failedResqueJobs))
glog.V(2).Info(failedResqueJobs)
for _, failedResqueJob := range failedResqueJobs {
if err := removeFailedResqueJob(redisClient, failedResqueJob); err != nil {
glog.Warning(errors.Wrap(err, "Unable to retry failed resque job").Error())
}
}
glog.V(1).Infof("finished cleanup at %v...", time.Now())
}
}
func getWorkersFromKubernetes(kubeClient *kubernetes.Client) ([]string, error) {
podList, err := kubeClient.ListPods(namespace, labelSelector)
if err != nil {
return []string{}, errors.Wrap(err, "Unable to list pods")
}
var podNames []string
for _, pod := range podList.Items {
podNames = append(podNames, pod.Name)
}
return podNames, nil
}
func getWorkersFromRedis(redisClient *redis.Client) ([]redisWorker, error) {
workers, err := redisClient.SMembers(resqueWorkersKey).Result()
if err != nil {
return []redisWorker{}, errors.Wrap(err, "Failed to get resque workers")
}
var redisWorkers []redisWorker
OUTER:
for _, worker := range workers {
workerName := strings.SplitN(worker, ":", 2)[0]
queueName := strings.SplitN(worker, ":", 2)[1]
for _, blacklistedQueue := range blacklistedResqueQueues {
if strings.Contains(blacklistedQueue, queueName) || strings.Contains(queueName, blacklistedQueue) {
continue OUTER
}
}
redisWorkers = append(redisWorkers, redisWorker{workerName, worker})
}
return redisWorkers, nil
}
type redisWorker struct {
name string
info string
}
func getDirtyExitFailedJobsFromRedis(redisClient *redis.Client) ([]redisWorker, error) {
failedJobsLength, err := redisClient.LLen(resqueFailedQueueKey).Result()
if err != nil {
return []redisWorker{}, errors.Wrap(err, "Failed to get failed resque jobs length")
}
failedJobs, err := redisClient.LRange(resqueFailedQueueKey, 0, failedJobsLength).Result()
if err != nil {
return []redisWorker{}, errors.Wrap(err, "Failed to get failed resque jobs")
}
var failedResqueJobs []redisWorker
for _, failedJob := range failedJobs {
var job failedResqueJob
if err := json.Unmarshal([]byte(failedJob), &job); err != nil {
continue
}
if regexp.MustCompile(`^Resque::.*DirtyExit`).MatchString(job.Exception) {
failedResqueJobs = append(failedResqueJobs, redisWorker{failedJob, failedJob})
}
}
return failedResqueJobs, nil
}
type failedResqueJob struct {
Exception string `json:"exception"`
}
func redisWorkersNotInKubernetes(redisWorkers []redisWorker, kubernetesWorkers []string) []redisWorker {
var deadRedisWorkers []redisWorker
OUTER:
for _, redisWorker := range redisWorkers {
for _, kubernetesWorker := range kubernetesWorkers {
if kubernetesWorker == redisWorker.name {
continue OUTER
}
}
deadRedisWorkers = append(deadRedisWorkers, redisWorker)
}
return deadRedisWorkers
}
func removeDeadRedisWorker(redisClient *redis.Client, redisWorker redisWorker) error {
bytes, err := redisClient.Get(fmt.Sprintf("%s:%s", resqueWorkerKey, redisWorker.info)).Bytes()
if err != nil && err != redis.Nil {
return errors.Wrap(err, fmt.Sprintf("Error getting %s from redis", redisWorker))
}
if err != nil && err == redis.Nil {
// Redis key not present so the issue probably corrected itself
return nil
}
if err := retryDeadWorker(redisClient, bytes); err != nil {
return errors.Wrap(err, fmt.Sprintf("Unable to retry %s", redisWorker))
}
redisClient.Pipelined(func(pipe *redis.Pipeline) error {
pipe.SRem(resqueWorkersKey, redisWorker.info)
pipe.Del(fmt.Sprintf("%s:%s", resqueWorkerKey, redisWorker.info))
pipe.Del(fmt.Sprintf("%s:%s:started", resqueWorkerKey, redisWorker.info))
pipe.Del(fmt.Sprintf("%s:%s:shutdown", resqueWorkerKey, redisWorker.info))
pipe.Del(fmt.Sprintf("%s:%s", statProcessedKey, redisWorker.info))
pipe.Del(fmt.Sprintf("%s:%s", statFailedKey, redisWorker.info))
return nil
})
return nil
}
func removeFailedResqueJob(redisClient *redis.Client, redisWorker redisWorker) error {
if err := retryDeadWorker(redisClient, []byte(redisWorker.info)); err != nil {
return errors.Wrap(err, fmt.Sprintf("Unable to retry %s", redisWorker.info))
}
redisClient.Pipelined(func(pipe *redis.Pipeline) error {
pipe.LRem(resqueFailedQueueKey, 1, redisWorker.info)
return nil
})
return nil
}
func retryDeadWorker(redisClient *redis.Client, workerData []byte) error {
resqueJob, err := newResqueJob(workerData)
if err != nil {
return errors.Wrap(err, "Unable to deserialize job")
}
glog.V(3).Infof("Going to retry job on queue: %s, with payload: %s", resqueJob.Queue, resqueJob.Payload)
if err := redisClient.SAdd(resqueQueuesKey, resqueJob.Queue).Err(); err != nil {
return errors.Wrap(err, fmt.Sprintf("Unable to create resque queue %s", resqueJob.Queue))
}
resqueJobJSON, err := resqueJob.Payload.MarshalJSON()
if err != nil {
return errors.Wrap(err, "Could not serialize job payload")
}
if err = redisClient.RPush(fmt.Sprintf("%s:%s", resqueQueueKey, resqueJob.Queue), string(resqueJobJSON)).Err(); err != nil {
return errors.Wrap(err, "Failed to insert job into resque queue")
}
return nil
}
func newResqueJob(data []byte) (resqueJob, error) {
var j resqueJob
err := json.Unmarshal(data, &j)
return j, err
}
type resqueJob struct {
Queue string `json:"queue"`
Payload json.RawMessage
}
func getRedisClient() *redis.Client {
if kubeConfig == "" {
addr := fmt.Sprintf("%s:%s", os.Getenv("REDIS_HOST"), os.Getenv("REDIS_PORT"))
return redis.NewClient(&redis.Options{
Addr: addr,
})
} else {
return redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
}
}