forked from oliamb/goworker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenqueue.go
53 lines (47 loc) · 1.19 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
package goworker
import (
"encoding/json"
"fmt"
)
// Enqueue function let you asynchronously enqueue a new job in Resque given
// the queue, the class name and the parameters.
//
// param queue: name of the queue (not including the namespace)
// param class: name of the Worker that can handle this job
// param args: arguments to pass to the handler function. Must be the non-marshalled version.
//
// return an error if args cannot be marshalled
func Enqueue(queue string, class string, args []interface{}) (err error) {
data := &payload{
Class: class,
Args: args,
}
b, err := json.Marshal(data)
if err != nil {
return
}
var conn *redisConn
// Retrieve a connection from the pool or create a new one if no pool is opened.
if pool != nil && !pool.IsClosed() {
resource, err := pool.Get()
if err != nil {
return err
}
conn = resource.(*redisConn)
defer pool.Put(conn)
} else {
// non-optimized mode, create a pool to avoid getting there.
conn, err = redisConnFromUri(uri)
if err != nil {
return err
}
defer conn.Close()
}
// Push job in redis
err = conn.Send("RPUSH", fmt.Sprintf("%squeue:%s", namespace, queue), b)
if err != nil {
return
}
conn.Flush()
return
}