forked from benmanns/goworker
/
client.go
47 lines (37 loc) · 1.05 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
package goworker
//go:generate ffjson $GOFILE
import (
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/pquerna/ffjson/ffjson"
)
// Client can be used to enqueue jobs to Resque.
type Client struct {
pool *redis.Pool
prefix string
}
type backJob struct {
Class string `json:"class"`
Args []interface{} `json:"args"`
}
// NewClient initiates and returns the Client.
func NewClient(pool *redis.Pool, namespace string) *Client {
return &Client{pool: pool, prefix: namespace}
}
// NewClientFromPrefs initiates and returns the Client
// using settings from PoolPrefs.
func NewClientFromPrefs(pool *redis.Pool, prefs PoolPrefs) *Client {
return &Client{pool: pool, prefix: prefs.RedisNamespace}
}
// Enqueue adds job to the queue.
func (c *Client) Enqueue(queue string, data ...interface{}) error {
buf, err := ffjson.Marshal(backJob{Class: queue, Args: data})
if err != nil {
return err
}
rc := c.pool.Get()
defer rc.Close()
defer ffjson.Pool(buf)
_, err = rc.Do("RPUSH", fmt.Sprintf("%vqueue:%v", c.prefix, queue), buf)
return err
}