-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
66 lines (47 loc) · 1.34 KB
/
task.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
package tq
import (
"encoding/json"
"log"
)
type Task struct {
// The message ID that gets generated by the broker.
// This field is only added when an event gets consumed.
Id string `json:"id"`
// The key of the task function that will execute the job.
Key string `json:"key"`
// Holds the serialized signature.
Payload []byte `json:"payload"`
// Maximum retry count for task execution to succeed.
RetryCount int `json:"retry_count,string"`
// Time duration between each retry attempt.
RetryTimeout int `json:"retry_timeout,string"`
// If task processing doesn't complete within the timeout, the task will be retried
Timeout int64 `json:"timeout,string"`
// Encode the result into the task.
// This allows us to only with a single Stream interface, rather than a TaskStream and ResultStream.
Result
}
func (s Task) Encode() map[string]interface{} {
var values map[string]interface{}
data, err := json.Marshal(s)
if err != nil {
log.Fatalf("Unable to marshall task: %s", err)
}
err = json.Unmarshal(data, &values)
if err != nil {
panic(err)
}
return values
}
func DecodeTask(msgId string, val map[string]interface{}) *Task {
jsonbody, err := json.Marshal(val)
if err != nil {
panic(err)
}
msg := Task{}
if err := json.Unmarshal(jsonbody, &msg); err != nil {
panic(err)
}
msg.Id = msgId
return &msg
}