-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.go
67 lines (55 loc) · 1.49 KB
/
work.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
package gofherd
import "sync/atomic"
// Status represents the outcome of "processing" Work.
// It can be one of Success, Retry, Failure.
type Status int
const (
// Success represents a successful processing outcome. It won't be retried.
Success Status = iota
// Retry represents a failed processing outcome, but is retriable. It will be retried for MaxRetries.
Retry
// Failure represents a failed processing outcome and should not be retried again.
Failure
)
var statusStrings = map[Status]string{
Success: "success",
Retry: "retry",
Failure: "failure",
}
func (r Status) String() string {
if val, ok := statusStrings[r]; ok {
return val
}
return "unknown"
}
// Work is the struct representing the work unit in Gofherd.
// It has an ID field which is a string, `Body` and `Result` which are an
// interface to store the "problem" and "solution" respectively.
type Work struct {
ID string
retry int64
status Status
Body interface{}
result interface{}
}
func (w *Work) retryCount() int64 {
return w.retry
}
func (w *Work) incrementRetries() {
atomic.AddInt64(&(w.retry), 1)
}
// SetResult is used to set the result for the Work unit.
func (w *Work) SetResult(result interface{}) {
w.result = result
}
func (w *Work) setStatus(status Status) {
w.status = status
}
// Status is used to access the status of the Work unit.
func (w *Work) Status() Status {
return w.status
}
// Result is used to access the Result of the Work unit.
func (w *Work) Result() interface{} {
return w.result
}