forked from benmanns/goworker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goworker.go
214 lines (187 loc) · 4.87 KB
/
goworker.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package goworker
import (
"os"
"strconv"
"sync"
"time"
"golang.org/x/net/context"
"errors"
"github.com/cihub/seelog"
"net"
)
var (
logger seelog.LoggerInterface
sentinel *Sentinel
ctx context.Context
initMutex sync.Mutex
initialized bool
)
var workerSettings WorkerSettings
type WorkerSettings struct {
QueuesString string
Queues queuesFlag
IntervalFloat float64
Interval intervalFlag
Concurrency int
Connections int
URI string
Namespace string
ExitOnComplete bool
IsStrict bool
UseNumber bool
Timeout time.Duration
ConnectionRetries int
}
func SetSettings(settings WorkerSettings) {
workerSettings = settings
}
// Init initializes the goworker process. This will be
// called by the Work function, but may be used by programs
// that wish to access goworker functions and configuration
// without actually processing jobs.
func Init() error {
initMutex.Lock()
defer initMutex.Unlock()
if !initialized {
var err error
logger, err = seelog.LoggerFromWriterWithMinLevel(os.Stdout, seelog.InfoLvl)
if err != nil {
return err
}
if err := flags(); err != nil {
return err
}
ctx = context.Background()
sentinel, err = NewSentinel(
workerSettings.URI,
workerSettings.Connections,
workerSettings.Connections,
workerSettings.Timeout,
)
if err != nil {
return err
}
go func() {
for {
if err := sentinel.Discover(); err != nil {
logger.Errorf("Sentinel discovery failed with %v", err)
}
time.Sleep(time.Minute)
}
}()
if err != nil {
return err
}
initialized = true
}
return nil
}
// GetConn returns a connection from the goworker Redis
// connection pool. When using the pool, check in
// connections as quickly as possible, because holding a
// connection will cause concurrent worker functions to
// lock while they wait for an available connection.
//
// The connection pool holds connections to a specific
// master which might go down or be demoted to slave. If
// either happens, the connection is closed and removed
// from the pool which will provide a brand new connection
// when asked. GetConn tries to get a new connection
// several times and only if no attempt succeeds, it
// returns the error.
func GetConn() (*RedisConn, error) {
deadConnection := errors.New("Dead connection")
slaveConnection := errors.New("Stale connection (to slave, not master)")
try := func() (*RedisConn, error) {
conn, err := sentinel.GetConn(ctx)
if err != nil {
return nil, err
}
// close the connection and remove it from the pool so that new
// connections get created
// if the instance does not ping back
_, err = conn.Do("ping")
if err != nil {
conn.Close()
PutConn(nil)
return nil, deadConnection
}
// or if the instance is not a master anymore
role, err := role(conn.Do("role"))
if err != nil {
return nil, err
}
if role != "master" {
conn.Close()
PutConn(nil)
return nil, slaveConnection
}
return conn, nil
}
var conn *RedisConn
var err error
for i := 0; i < workerSettings.ConnectionRetries; i++ {
if conn, err = try(); err == nil {
return conn, nil
} else if err != slaveConnection && err != deadConnection {
if err, ok := err.(net.Error); ok && !err.Timeout() {
return nil, err
}
}
}
return conn, err
}
// PutConn puts a connection back into the connection pool.
// Run this as soon as you finish using a connection that
// you got from GetConn. Expect this API to change
// drastically.
func PutConn(conn *RedisConn) {
sentinel.PutConn(conn)
}
// Close cleans up resources initialized by goworker. This
// will be called by Work when cleaning up. However, if you
// are using the Init function to access goworker functions
// and configuration without processing jobs by calling
// Work, you should run this function when cleaning up. For
// example,
//
// if err := goworker.Init(); err != nil {
// fmt.Println("Error:", err)
// }
// defer goworker.Close()
func Close() {
initMutex.Lock()
defer initMutex.Unlock()
if initialized {
sentinel.Close()
initialized = false
}
}
// Work starts the goworker process. Check for errors in
// the return value. Work will take over the Go executable
// and will run until a QUIT, INT, or TERM signal is
// received, or until the queues are empty if the
// -exit-on-complete flag is set.
func Work() error {
err := Init()
if err != nil {
return err
}
defer Close()
quit := signals()
poller, err := newPoller(workerSettings.Queues, workerSettings.IsStrict)
if err != nil {
return err
}
jobs := poller.poll(time.Duration(workerSettings.Interval), quit)
var monitor sync.WaitGroup
for id := 0; id < workerSettings.Concurrency; id++ {
worker, err := newWorker(strconv.Itoa(id), workerSettings.Queues)
if err != nil {
return err
}
worker.work(jobs, &monitor)
}
monitor.Wait()
return nil
}