forked from benmanns/goworker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workers.go
49 lines (41 loc) · 977 Bytes
/
workers.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
package goworker
import (
"encoding/json"
"fmt"
)
var (
workers map[string]workerFunc
)
func init() {
workers = make(map[string]workerFunc)
}
// Register registers a goworker worker function. Class
// refers to the Ruby name of the class which enqueues the
// job. Worker is a function which accepts a queue and an
// arbitrary array of interfaces as arguments.
func Register(class string, worker workerFunc) {
workers[class] = worker
}
func Enqueue(job *Job) error {
err := Init()
if err != nil {
return err
}
conn, err := GetConn()
if err != nil {
logger.Criticalf("Error on getting connection on enqueue")
return err
}
defer PutConn(conn)
buffer, err := json.Marshal(job.Payload)
if err != nil {
logger.Criticalf("Cant marshal payload on enqueue")
return err
}
err = conn.Send("RPUSH", fmt.Sprintf("%squeue:%s", namespace, job.Queue), buffer)
if err != nil {
logger.Criticalf("Cant push to queue")
return err
}
return conn.Flush()
}