-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
57 lines (48 loc) · 1.02 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
package cerra
import (
"encoding/json"
"time"
)
type Task struct {
ID string `json:"id"`
Payload []byte `json:"payload"`
Timeout time.Duration `json:"timeout"`
RetryLimit int `json:"retry_limit"`
RetryCount int `json:"retry_count"`
}
func NewTask(payload []byte) *Task {
return &Task{
Payload: payload,
Timeout: 60 * time.Second,
}
}
func NewTaskWithID(id string, payload []byte) *Task {
return &Task{
ID: id,
Payload: payload,
Timeout: 60 * time.Second,
}
}
func (t *Task) SetID(id string) {
t.ID = id
}
func (t *Task) SetRetryLimit(limit int) {
if limit >= 0 {
t.RetryLimit = limit
}
}
func (t *Task) SetTimeout(timeout time.Duration) {
t.Timeout = timeout
}
func (t *Task) Encode() ([]byte, error) {
return json.Marshal(t)
}
func (t *Task) ToMap() map[string]interface{} {
return map[string]interface{}{
"id": t.ID,
"payload": t.Payload,
"timeout": t.Timeout,
"retry_limit": t.RetryLimit,
"retry_count": t.RetryCount,
}
}