forked from zimulala/goqless
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
218 lines (183 loc) · 4.33 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package goqless
import (
"encoding/json"
"errors"
"fmt"
"github.com/garyburd/redigo/redis"
"strconv"
)
type TaggedReply struct {
Total int
Jobs StringSlice
}
type Client struct {
conn redis.Conn
host string
port string
events *Events
lua *Lua
}
func NewClient(host, port string) *Client {
return &Client{host: host, port: port}
}
func Dial(host, port string) (*Client, error) {
c := NewClient(host, port)
conn, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", host, port))
if err != nil {
return nil, err
}
c.lua = NewLua(conn)
dir, err := GetCurrentDir()
if err != nil {
println(err.Error())
conn.Close()
return nil, err
}
//println(dir + "/qless-core")
err = c.lua.LoadScripts(dir + "/qless-core") // make get from lib path
if err != nil {
println(err.Error())
conn.Close()
return nil, err
}
c.conn = conn
return c, nil
}
func (c *Client) Close() {
c.conn.Close()
}
func (c *Client) Events() *Events {
if c.events != nil {
return c.events
}
c.events = NewEvents(c.host, c.port)
return c.events
}
func (c *Client) Do(name string, keysAndArgs ...interface{}) (interface{}, error) {
return c.lua.Do(name, keysAndArgs...)
}
func (c *Client) Queue(name string) *Queue {
q := NewQueue(c)
q.Name = name
return q
}
func (c *Client) Queues(name string) ([]*Queue, error) {
args := []interface{}{0, "queues", timestamp()}
if name != "" {
args = append(args, name)
}
byts, err := redis.Bytes(c.Do("qless", args...))
if err != nil {
return nil, err
}
qr := []*Queue{NewQueue(c)}
if name == "" {
err = json.Unmarshal(byts, &qr)
for _, q := range qr {
q.cli = c
}
} else {
err = json.Unmarshal(byts, &qr[0])
}
if err != nil {
return nil, err
}
return qr, err
}
// Track the jid
func (c *Client) Track(jid string) (bool, error) {
return Bool(c.Do("qless", 0, "track", timestamp(), "track", jid, ""))
}
// Untrack the jid
func (c *Client) Untrack(jid string) (bool, error) {
return Bool(c.Do("qless", 0, "track", timestamp(), 0, "untrack", jid))
}
// Returns all the tracked jobs
func (c *Client) Tracked() (string, error) {
return redis.String(c.Do("qless", 0, "track", timestamp()))
}
func (c *Client) Get(jid string) (interface{}, error) {
job, err := c.GetJob(jid)
if err == redis.ErrNil {
rjob, err := c.GetRecurringJob(jid)
return rjob, err
}
return job, err
}
func (c *Client) GetJob(jid string) (*Job, error) {
byts, err := redis.Bytes(c.Do("qless", 0, "get", timestamp(), jid))
if err != nil {
return nil, err
}
job := NewJob(c)
err = json.Unmarshal(byts, job)
if err != nil {
return nil, err
}
return job, err
}
func (c *Client) GetRecurringJob(jid string) (*RecurringJob, error) {
byts, err := redis.Bytes(c.Do("qless", 0, "recur", timestamp(), "get", jid))
if err != nil {
return nil, err
}
job := NewRecurringJob(c)
err = json.Unmarshal(byts, job)
if err != nil {
return nil, err
}
return job, err
}
func (c *Client) Completed(start, count int) ([]string, error) {
reply, err := redis.Values(c.Do("qless", 0, "jobs", timestamp(), "complete"))
if err != nil {
return nil, err
}
ret := []string{}
for _, val := range reply {
s, _ := redis.String(val, err)
ret = append(ret, s)
}
return ret, err
}
func (c *Client) Tagged(tag string, start, count int) (*TaggedReply, error) {
byts, err := redis.Bytes(c.Do("qless", 0, "tag", timestamp(), "get", tag, start, count))
if err != nil {
return nil, err
}
t := &TaggedReply{}
err = json.Unmarshal(byts, t)
return t, err
}
func (c *Client) GetConfig(option string) (string, error) {
interf, err := c.Do("qless", 0, "config.get", timestamp(), option)
if err != nil {
return "", err
}
var contentStr string
switch interf.(type) {
case []uint8:
contentStr, err = redis.String(interf, nil)
case int64:
var contentInt64 int64
contentInt64, err = redis.Int64(interf, nil)
if err == nil {
contentStr = strconv.Itoa(int(contentInt64))
}
default:
err = errors.New("The redis return type is not []uint8 or int64")
}
if err != nil {
return "", err
}
return contentStr, err
}
func (c *Client) SetConfig(option string, value interface{}) {
intf, err := c.Do("qless", 0, "config.set", timestamp(), option, value)
if err != nil {
fmt.Println("setconfig, c.Do fail. interface:", intf, " err:", err)
}
}
func (c *Client) UnsetConfig(option string) {
c.Do("qless", 0, "config.unset", timestamp(), option)
}