-
Notifications
You must be signed in to change notification settings - Fork 230
/
mutate.go
167 lines (150 loc) · 3.62 KB
/
mutate.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
package server
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/contribsys/faktory/client"
"github.com/contribsys/faktory/manager"
"github.com/contribsys/faktory/storage"
)
var (
AlwaysMatch = func(value string) bool {
return true
}
)
func mutateKill(store storage.Store, op client.Operation) error {
ss := setForTarget(store, string(op.Target))
if ss == nil {
return fmt.Errorf("Invalid target for mutation command")
}
match, matchfn := matchForFilter(op.Filter)
return ss.Find(match, func(idx int, ent storage.SortedEntry) error {
if matchfn(string(ent.Value())) {
return ss.MoveTo(store.Dead(), ent, time.Now().Add(manager.DeadTTL))
}
return nil
})
}
func mutateRequeue(store storage.Store, op client.Operation) error {
ss := setForTarget(store, string(op.Target))
if ss == nil {
return fmt.Errorf("Invalid target for mutation command")
}
match, matchfn := matchForFilter(op.Filter)
return ss.Find(match, func(idx int, ent storage.SortedEntry) error {
if matchfn(string(ent.Value())) {
j, err := ent.Job()
if err != nil {
return err
}
q, err := store.GetQueue(j.Queue)
if err != nil {
return err
}
err = q.Push(ent.Value())
if err != nil {
return err
}
return ss.RemoveEntry(ent)
}
return nil
})
}
func mutateDiscard(store storage.Store, op client.Operation) error {
ss := setForTarget(store, string(op.Target))
if ss == nil {
return fmt.Errorf("Invalid target for mutation command")
}
if op.Filter == nil {
return ss.Clear()
}
match, matchfn := matchForFilter(op.Filter)
return ss.Find(match, func(idx int, ent storage.SortedEntry) error {
if matchfn(string(ent.Value())) {
return ss.RemoveEntry(ent)
}
return nil
})
}
func matchForFilter(filter *client.JobFilter) (string, func(value string) bool) {
if filter == nil {
return "*", AlwaysMatch
}
if filter.Regexp != "" {
if filter.Jobtype == "" {
return filter.Regexp, AlwaysMatch
} else {
// if a regexp and jobtype, pass the regexp to Redis and match jobtype
// here
typematch := fmt.Sprintf(`"jobtype":"%s"`, filter.Jobtype)
return filter.Regexp, func(value string) bool {
return strings.Index(value, typematch) > 0
}
}
}
if filter.Jobtype != "" {
return fmt.Sprintf(`*"jobtype":"%s"*`, filter.Jobtype), AlwaysMatch
}
if len(filter.Jids) > 0 {
return "*", func(value string) bool {
for idx := range filter.Jids {
if strings.Index(value, fmt.Sprintf(`"jid":"%s"`, filter.Jids[idx])) > 0 {
return true
}
}
return false
}
}
return "*", AlwaysMatch
}
func mutate(c *Connection, s *Server, cmd string) {
parts := strings.Split(cmd, " ")
if len(parts) != 2 {
_ = c.Error(cmd, fmt.Errorf("Invalid format"))
return
}
var err error
var op client.Operation
err = json.Unmarshal([]byte(parts[1]), &op)
if err != nil {
_ = c.Error(cmd, err)
return
}
switch op.Cmd {
case "clear":
err = mutateClear(s.Store(), string(op.Target))
case "kill":
err = mutateKill(s.Store(), op)
case "discard":
err = mutateDiscard(s.Store(), op)
case "requeue":
err = mutateRequeue(s.Store(), op)
default:
err = fmt.Errorf("Unknown mutate operation")
}
if err != nil {
_ = c.Error(cmd, err)
return
}
_ = c.Ok()
}
func mutateClear(store storage.Store, target string) error {
ss := setForTarget(store, target)
if ss == nil {
return fmt.Errorf("Invalid target for mutation command")
}
return ss.Clear()
}
func setForTarget(store storage.Store, name string) storage.SortedSet {
switch name {
case "retries":
return store.Retries()
case "dead":
return store.Dead()
case "scheduled":
return store.Scheduled()
default:
return nil
}
}