forked from benmanns/goworker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goworker.go
53 lines (42 loc) · 1.02 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
package goworker
import (
"github.com/cihub/seelog"
"os"
"strconv"
"sync"
"time"
)
var logger seelog.LoggerInterface
// Call this function to run goworker. 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 {
var err error
logger, err = seelog.LoggerFromWriterWithMinLevel(os.Stdout, seelog.InfoLvl)
if err != nil {
return err
}
if err := flags(); err != nil {
return err
}
quit := signals()
pool := newRedisPool(uri, connections, connections, time.Minute)
defer pool.Close()
poller, err := newPoller(queues, isStrict)
if err != nil {
return err
}
jobs := poller.poll(pool, time.Duration(interval), quit)
var monitor sync.WaitGroup
for id := 0; id < concurrency; id++ {
worker, err := newWorker(strconv.Itoa(id), queues)
if err != nil {
return err
}
worker.work(pool, jobs, &monitor)
}
monitor.Wait()
return nil
}