-
Notifications
You must be signed in to change notification settings - Fork 9
/
worker_client.go
61 lines (49 loc) · 1.26 KB
/
worker_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"encoding/json"
"errors"
"github.com/jrallison/go-workers"
)
// WorkerClient is an interface for enqueueing workers
type WorkerClient interface {
// Push pushes a worker onto the queue
Push(class, args string) (string, error)
}
type redisWorkerClient struct {
queue string
}
// NewRedisWorkerClient creates a worker client that pushes the worker to redis
func NewRedisWorkerClient(redis RedisConfig) (WorkerClient, error) {
if redis.Host == "" {
return nil, errors.New("Redis host required")
}
if redis.Queue == "" {
return nil, errors.New("Sidekiq queue required")
}
workerConfig := map[string]string{
"server": redis.Host,
"database": "0",
"pool": "20",
"process": "1",
}
if redis.Namespace != "" {
workerConfig["namespace"] = redis.Namespace
}
if redis.Password != "" {
workerConfig["password"] = redis.Password
}
workers.Configure(workerConfig)
return &redisWorkerClient{queue: redis.Queue}, nil
}
func (r *redisWorkerClient) Push(class, args string) (string, error) {
// This will hopefully deserialize on the ruby end as a hash
jsonArgs := json.RawMessage([]byte(args))
return workers.EnqueueWithOptions(
r.queue,
class,
[]*json.RawMessage{&jsonArgs},
workers.EnqueueOptions{
Retry: true,
},
)
}