forked from vmihailenco/taskq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.go
71 lines (61 loc) · 1.14 KB
/
tasks.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
package api_worker
import (
"log"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
"github.com/go-redis/redis/v8"
"github.com/vmihailenco/taskq/v3"
"github.com/vmihailenco/taskq/v3/redisq"
)
var Redis = redis.NewClient(&redis.Options{
Addr: ":6379",
})
var (
QueueFactory = redisq.NewFactory()
MainQueue = QueueFactory.RegisterQueue(&taskq.QueueOptions{
Name: "api-worker",
Redis: Redis,
})
CountTask = taskq.RegisterTask(&taskq.TaskOptions{
Name: "counter",
Handler: func() error {
IncrLocalCounter()
time.Sleep(time.Millisecond)
return nil
},
})
)
var counter int32
func GetLocalCounter() int32 {
return atomic.LoadInt32(&counter)
}
func IncrLocalCounter() {
atomic.AddInt32(&counter, 1)
}
func LogStats() {
var prev int32
for range time.Tick(3 * time.Second) {
n := GetLocalCounter()
log.Printf("processed %d tasks (%d/s)", n, (n-prev)/3)
prev = n
}
}
func WaitSignal() os.Signal {
ch := make(chan os.Signal, 2)
signal.Notify(
ch,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTERM,
)
for {
sig := <-ch
switch sig {
case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM:
return sig
}
}
}