forked from jrallison/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 2
/
enqueue.go
143 lines (115 loc) · 2.78 KB
/
enqueue.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
package workers
import (
"encoding/json"
"fmt"
"time"
"github.com/garyburd/redigo/redis"
"github.com/google/uuid"
)
const (
NanoSecondPrecision = 1000000000.0
)
var (
ErrJidExists = fmt.Errorf("jid has already exists")
)
type EnqueueData struct {
Queue string `json:"queue,omitempty"`
Class string `json:"class"`
Args interface{} `json:"args"`
Jid string `json:"jid"`
EnqueuedAt float64 `json:"enqueued_at"`
EnqueueParam
}
type EnqueueParam struct {
RetryCount int `json:"retry_count,omitempty"`
At float64 `json:"at,omitempty"`
MaxRetries int `json:"max_retries,omitempty"`
Jid string `json:"jid,omitempty"`
}
func generateJid() string {
return uuid.New().String()
}
func Enqueue(queue, class string, args interface{}, opts ...EnqueueOptions) (string, error) {
var param EnqueueParam
defaultEnqueueOpt(¶m)
for _, opt := range opts {
opt.Apply(¶m)
}
now := nowToSecondsWithNanoPrecision()
data := EnqueueData{
Queue: queue,
Class: class,
Args: args,
Jid: param.Jid,
EnqueuedAt: now,
EnqueueParam: param,
}
bytes, err := json.Marshal(data)
if err != nil {
return "", err
}
if now < param.At {
err := enqueueAt(data.Jid, data.At, bytes)
return data.Jid, err
}
conn := Config.Pool.Get()
defer conn.Close()
_, err = conn.Do("sadd", Config.Namespace+"queues", queue)
if err != nil {
return "", err
}
queue = Config.Namespace + "queue:" + queue
var ok bool
ok, err = redis.Bool(enqueueScript.Do(conn, queue, ARGV_VALUE_KEY, data.Jid, bytes))
if err != nil {
return "", err
}
if !ok {
return "", ErrJidExists
}
return data.Jid, nil
}
func enqueueAt(jid string, at float64, bytes []byte) error {
conn := Config.Pool.Get()
defer conn.Close()
ok, err := redis.Bool(enqueueAtScript.Do(
conn,
Config.Namespace+SCHEDULED_JOBS_KEY,
ARGV_VALUE_KEY,
jid, at, bytes,
))
if err != nil {
return err
}
if !ok {
return ErrJidExists
}
return nil
}
func timeToSecondsWithNanoPrecision(t time.Time) float64 {
return float64(t.UnixNano()) / NanoSecondPrecision
}
func durationToSecondsWithNanoPrecision(d time.Duration) float64 {
return float64(d.Nanoseconds()) / NanoSecondPrecision
}
func nowToSecondsWithNanoPrecision() float64 {
return timeToSecondsWithNanoPrecision(time.Now())
}
func JobExists(jid string) (bool, error) {
conn := Config.Pool.Get()
defer conn.Close()
exists, err := redis.Bool(conn.Do("hexists", ARGV_VALUE_KEY, jid))
if err != nil {
return false, err
}
return exists, nil
}
func CancelJob(jid string) error {
conn := Config.Pool.Get()
defer conn.Close()
_, err := conn.Do("SET", fmt.Sprintf("%s-%s", Config.Namespace+CANCEL_KEY, jid), true, "EX", inprogressTimeout)
if err != nil {
return err
}
return nil
}