-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjob.go
84 lines (74 loc) · 2.03 KB
/
job.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
package goalpost
import (
"bytes"
"encoding/gob"
"fmt"
)
//JobStatus is a enumerated int representing the processing status of a Job
type JobStatus int
// JobStatus types
const (
Ack JobStatus = iota + 1
Uack
Nack
Failed
)
//Job wraps arbitrary data for processing
type Job struct {
Status JobStatus
//Unique identifier for a Job
ID uint64
//Data contains the bytes that were pushed using Queue.PushBytes()
Data []byte
//RetryCount is the number of times the job has been retried
//If your work can have a temporary failure state, it is recommended
//that you check retry count and return a fatal error after a certain
//number of retries
RetryCount int
//Message is primarily used for debugging. It contains status info
//about what was last done with the job.
Message string
}
//DecodeJob decodes a gob encoded byte array into a Job struct and returns a pointer to it
func DecodeJob(b []byte) *Job {
//TODO: this should return an error in the event decoder.Decode returns an err
buffer := bytes.NewReader(b)
decoder := gob.NewDecoder(buffer)
job := Job{}
decoder.Decode(&job)
return &job
}
//Bytes returns a gob encoded byte array representation of *j
func (j *Job) Bytes() []byte {
buffer := &bytes.Buffer{}
encoder := gob.NewEncoder(buffer)
encoder.Encode(j)
return buffer.Bytes()
}
// State returns the job status in human readable form
func (j *Job) State() string {
switch j.Status {
case Ack:
return "completed"
case Uack:
return "pending"
case Nack:
return "retrying"
case Failed:
return "failed"
default:
return "unknown"
}
}
//RecoverableWorkerError defines an error that a worker DoWork func
//can return that indicates the message should be retried
type RecoverableWorkerError struct {
message string
}
func (e RecoverableWorkerError) Error() string {
return fmt.Sprintf("Worker encountered a temporary error: %s", e.message)
}
//NewRecoverableWorkerError creates a new RecoverableWorkerError
func NewRecoverableWorkerError(message string) RecoverableWorkerError {
return RecoverableWorkerError{message}
}