-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
119 lines (96 loc) · 2.86 KB
/
client.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
package redisq
import (
"encoding/json"
"errors"
"fmt"
"github.com/garyburd/redigo/redis"
"time"
)
const (
QUEUE_TASK = "task"
LIST_QUEUE = "queue"
LIST_FAILURE = "failure"
LIST_FAILURE_FINAL = "failure_final"
LIST_PROCESSING = "processing"
LIST_FAILURE_PROCESSING = "failure_processing"
)
type TaskDetails struct {
Arguments []string `json:"arguments"`
CreatedAt string `json:"createdAt"`
Attempts int `json:"attempts"`
Type string `json:"type"`
LastAttempt string `json:"lastAttempt"`
LastError string `json:"lastError"`
}
// increments attempts and updates `LastAttempt` property to the current date
func (td *TaskDetails) NewAttempt() {
td.Attempts++
td.LastAttempt = time.Now().UTC().Format(time.RFC3339)
}
type RedisClient struct {
conn redis.Conn
prefix string
taskType string
}
func NewRedisClient(conn redis.Conn, prefix, taskType string) *RedisClient {
return &RedisClient{
conn: conn,
prefix: prefix,
taskType: taskType,
}
}
// pick an item from the queue
func (rc *RedisClient) PickTask(from, to string) (string, error) {
result, err := rc.conn.Do(
"BRPOPLPUSH",
fmt.Sprintf("%s:%s:%s", rc.prefix, from, rc.taskType),
fmt.Sprintf("%s:%s:%s", rc.prefix, to, rc.taskType),
0,
)
if err != nil {
return "", err
}
// interpret result as []byte
uuid, ok := result.([]byte)
if !ok {
return "", errors.New("Interpreting task uuid interface{} as []byte failed")
}
return string(uuid), nil
}
// get task details for a given task uuid
func (rc *RedisClient) GetTaskDetails(uuid string) (*TaskDetails, error) {
taskResult, err := rc.conn.Do("GET", fmt.Sprintf("%s:%s:%s:%s", rc.prefix, QUEUE_TASK, rc.taskType, uuid))
if err != nil {
return nil, err
}
// interpret result as []byte
taskJson, ok := taskResult.([]byte)
if !ok {
return nil, errors.New("Interpreting task data interface{} as []byte failed")
}
var taskDetails TaskDetails
err = json.Unmarshal(taskJson, &taskDetails)
if err != nil {
return nil, err
}
return &taskDetails, nil
}
func (rc *RedisClient) PushTaskToList(uuid string, list string) error {
_, err := rc.conn.Do("LPUSH", fmt.Sprintf("%s:%s:%s", rc.prefix, list, rc.taskType), uuid)
return err
}
func (rc *RedisClient) SaveTaskDetails(uuid string, taskDetails *TaskDetails) error {
newResult, err := json.Marshal(taskDetails)
if err == nil {
_, err = rc.conn.Do("SET", fmt.Sprintf("%s:%s:%s:%s", rc.prefix, QUEUE_TASK, rc.taskType, uuid), newResult)
}
return err
}
func (rc *RedisClient) DeleteTask(uuid string) error {
_, err := rc.conn.Do("DEL", fmt.Sprintf("%s:%s:%s:%s", rc.prefix, QUEUE_TASK, rc.taskType, uuid))
return err
}
func (rc *RedisClient) RemoveOneFromList(uuid, listName string) error {
_, err := rc.conn.Do("LREM", fmt.Sprintf("%s:%s:%s", rc.prefix, listName, rc.taskType), 1, uuid)
return err
}