This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
forked from kavu/go-resque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resque.go
73 lines (57 loc) · 1.64 KB
/
resque.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
package resque
import (
"encoding/json"
"time"
"github.com/kiddom/go-resque/driver"
)
var drivers = make(map[string]driver.Enqueuer)
type jobArg interface{}
type MockRedisDriver struct {
driver.Enqueuer
}
type job struct {
Queue string `json:"queue,omitempty"`
Class string `json:"class"`
Args []jobArg `json:"args"`
}
func Register(name string, driver driver.Enqueuer) {
if _, d := drivers[name]; d {
panic("Register called twice for driver " + name)
}
drivers[name] = driver
}
func NewRedisEnqueuer(drvName string, client interface{}, nameSpace string) *RedisEnqueuer {
drv, ok := drivers[drvName]
if !ok {
panic("No such driver: " + drvName)
}
drv.SetClient(nameSpace, client)
drv.Poll()
return &RedisEnqueuer{drv: drv}
}
type RedisEnqueuer struct {
drv driver.Enqueuer
}
func (enqueuer *RedisEnqueuer) Enqueue(queue, jobClass string, args ...jobArg) (int64, error) {
// NOTE: Dirty hack to make a [{}] JSON struct
if len(args) == 0 {
args = append(make([]jobArg, 0), make(map[string]jobArg, 0))
}
jobJSON, err := json.Marshal(&job{Class: jobClass, Args: args})
if err != nil {
return -1, err
}
return enqueuer.drv.ListPush(queue, string(jobJSON))
}
// EnqueueIn enque a job at a duration
func (enqueuer *RedisEnqueuer) EnqueueIn(delay time.Duration, queue, jobClass string, args ...jobArg) (bool, error) {
enqueueTime := time.Now().Add(delay)
if len(args) == 0 {
args = append(make([]jobArg, 0), make(map[string]jobArg, 0))
}
jobJSON, err := json.Marshal(&job{Class: jobClass, Args: args, Queue: queue})
if err != nil {
return false, err
}
return enqueuer.drv.ListPushDelay(enqueueTime, queue, string(jobJSON))
}