forked from contribsys/faktory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
working.go
186 lines (157 loc) · 3.64 KB
/
working.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
package server
import (
"encoding/json"
"sync"
"time"
"github.com/contribsys/faktory"
"github.com/contribsys/faktory/util"
)
var (
DefaultTimeout = 1800
// Hold the working set in memory so we don't need to burn CPU
// marshalling between Rocks and memory when doing 1000s of jobs/sec.
// When client ack's JID, we can lookup reservation
// and remove Rocks entry quickly.
workingMap = map[string]*Reservation{}
workingMutex = &sync.Mutex{}
)
type Reservation struct {
Job *faktory.Job `json:"job"`
Since string `json:"reserved_at"`
Expiry string `json:"expires_at"`
Wid string `json:"wid"`
tsince time.Time
texpiry time.Time
}
type TimedSet interface {
AddElement(string, string, []byte) error
RemoveElement(string, string) error
}
/*
* When we restart the server, we need to load the
* current set of Reservations back into memory so any
* outstanding jobs can be Acknowledged successfully.
*
* The alternative is that a server restart would re-execute
* all outstanding jobs, something to be avoided when possible.
*/
func (s *Server) loadWorkingSet() error {
workingMutex.Lock()
defer workingMutex.Unlock()
addedCount := 0
err := s.store.Working().Each(func(_ int, _ []byte, data []byte) error {
var res Reservation
err := json.Unmarshal(data, &res)
if err != nil {
return err
}
workingMap[res.Job.Jid] = &res
addedCount += 1
return nil
})
if err != nil {
return err
}
if addedCount > 0 {
util.Debugf("Bootstrapped working set, loaded %d", addedCount)
}
return err
}
func acknowledge(jid string, set TimedSet) (*faktory.Job, error) {
workingMutex.Lock()
res, ok := workingMap[jid]
if !ok {
workingMutex.Unlock()
util.Infof("No such job to acknowledge %s", jid)
return nil, nil
}
delete(workingMap, jid)
workingMutex.Unlock()
err := set.RemoveElement(res.Expiry, jid)
return res.Job, err
}
type busyReaper struct {
s *Server
count int
}
func (r *busyReaper) Name() string {
return "Busy"
}
func (r *busyReaper) Execute() error {
count := 0
jobs, err := r.s.store.Working().RemoveBefore(util.Nows())
if err != nil {
return err
}
for _, data := range jobs {
var job faktory.Job
err := json.Unmarshal(data, &job)
if err != nil {
util.Error("Unable to unmarshal job", err, nil)
continue
}
q, err := r.s.store.GetQueue(job.Queue)
if err != nil {
util.Error("Unable to retrieve queue", err, nil)
continue
}
workingMutex.Lock()
_, ok := workingMap[job.Jid]
if ok {
delete(workingMap, job.Jid)
}
workingMutex.Unlock()
if ok {
err = q.Push(data)
if err != nil {
util.Error("Unable to push job", err, nil)
continue
}
count += 1
}
}
r.count += count
return nil
}
func (r *busyReaper) Stats() map[string]interface{} {
return map[string]interface{}{
"size": len(workingMap),
"reaped": r.count,
}
}
func reserve(wid string, job *faktory.Job, set TimedSet) error {
now := time.Now()
timeout := job.ReserveFor
if timeout == 0 {
timeout = DefaultTimeout
}
if timeout < 60 {
timeout = DefaultTimeout
util.Warnf("Timeout too short %d, 60 seconds minimum", timeout)
}
if timeout > 86400 {
timeout = DefaultTimeout
util.Warnf("Timeout too long %d, one day maximum", timeout)
}
exp := now.Add(time.Duration(timeout) * time.Second)
var res = &Reservation{
Job: job,
Since: util.Thens(now),
Expiry: util.Thens(exp),
Wid: wid,
tsince: now,
texpiry: exp,
}
data, err := json.Marshal(res)
if err != nil {
return err
}
err = set.AddElement(res.Expiry, job.Jid, data)
if err != nil {
return err
}
workingMutex.Lock()
workingMap[job.Jid] = res
workingMutex.Unlock()
return nil
}