forked from contribsys/faktory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retry.go
143 lines (119 loc) · 3.07 KB
/
retry.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
package manager
import (
"encoding/json"
"fmt"
"math/rand"
"strings"
"time"
"github.com/contribsys/faktory/client"
"github.com/contribsys/faktory/storage"
"github.com/contribsys/faktory/util"
)
type FailPayload struct {
Jid string `json:"jid"`
ErrorMessage string `json:"message"`
ErrorType string `json:"errtype"`
Backtrace []string `json:"backtrace"`
}
func (m *manager) Fail(failure *FailPayload) error {
if failure == nil {
return fmt.Errorf("No failure")
}
jid := failure.Jid
if jid == "" {
return fmt.Errorf("Missing JID")
}
cleanse(failure)
return m.processFailure(jid, failure)
}
func cleanse(failure *FailPayload) {
failure.ErrorType = strings.TrimSpace(failure.ErrorType)
failure.ErrorMessage = strings.TrimSpace(failure.ErrorMessage)
if failure.ErrorType != "" {
if len(failure.ErrorType) > 100 {
failure.ErrorType = failure.ErrorType[0:100]
}
} else {
failure.ErrorType = "unknown"
}
if failure.ErrorMessage != "" {
if len(failure.ErrorMessage) > 1000 {
failure.ErrorMessage = failure.ErrorMessage[0:1000]
}
} else {
failure.ErrorMessage = "unknown"
}
if failure.Backtrace == nil {
failure.Backtrace = []string{}
}
if len(failure.Backtrace) > 50 {
failure.Backtrace = failure.Backtrace[0:50]
}
}
func (m *manager) clearReservation(jid string) *Reservation {
m.workingMutex.Lock()
res, ok := m.workingMap[jid]
if !ok {
m.workingMutex.Unlock()
return nil
}
delete(m.workingMap, jid)
m.workingMutex.Unlock()
return res
}
func (m *manager) processFailure(jid string, failure *FailPayload) error {
res := m.clearReservation(jid)
if res == nil {
return fmt.Errorf("Job not found %s", jid)
}
err := m.store.Working().RemoveElement(res.Expiry, jid)
if err != nil {
return err
}
m.store.Failure()
job := res.Job
if job.Retry == 0 {
// no retry, no death, completely ephemeral, goodbye
return nil
}
if job.Failure != nil {
job.Failure.RetryCount++
job.Failure.ErrorMessage = failure.ErrorMessage
job.Failure.ErrorType = failure.ErrorType
job.Failure.Backtrace = failure.Backtrace
} else {
job.Failure = &client.Failure{
RetryCount: 0,
FailedAt: util.Nows(),
ErrorMessage: failure.ErrorMessage,
ErrorType: failure.ErrorType,
Backtrace: failure.Backtrace,
}
}
if job.Failure.RetryCount < job.Retry {
return retryLater(m.store, job)
}
return sendToMorgue(m.store, job)
}
func retryLater(store storage.Store, job *client.Job) error {
when := util.Thens(nextRetry(job))
job.Failure.NextAt = when
bytes, err := json.Marshal(job)
if err != nil {
return err
}
return store.Retries().AddElement(when, job.Jid, bytes)
}
func sendToMorgue(store storage.Store, job *client.Job) error {
bytes, err := json.Marshal(job)
if err != nil {
return err
}
expiry := util.Thens(time.Now().Add(DeadTTL))
return store.Dead().AddElement(expiry, job.Jid, bytes)
}
func nextRetry(job *client.Job) time.Time {
count := job.Failure.RetryCount
secs := (count * count * count * count) + 15 + (rand.Intn(30) * (count + 1))
return time.Now().Add(time.Duration(secs) * time.Second)
}