/
main.go
78 lines (59 loc) · 1.54 KB
/
main.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
package main
import (
"context"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/mseld/sqs-consumer/consumer"
"github.com/mseld/sqs-consumer/examples/utils"
)
func main() {
log.Println("Worker Started")
client := utils.NewSqsClient()
ctx, cancel := context.WithCancel(context.Background())
wg := &sync.WaitGroup{}
queueUrl := "https://sqs.eu-west-1.amazonaws.com/0000000000/demo-queue"
consumerWorker := consumer.New(client, queueUrl).
WithContext(ctx).
WithWaitGroup(wg).
WithBatchSize(10).
WithReceiveWaitTimeSeconds(5).
WithReceiveVisibilityTimeout(30).
WithTerminateVisibilityTimeout(5).
WithInterval(100).
WithEnableDebug(true)
consumerWorker.WorkerPool(consumer.HandlerFunc(handler), 4)
exit := make(chan os.Signal, 1)
signal.Notify(exit, syscall.SIGINT, syscall.SIGTERM)
sig := <-exit
log.Println("Worker Received Shutdown Signal", sig)
consumerWorker.Close()
time.AfterFunc(time.Second*5, cancel)
setTimeout(wg, time.Second*30)
log.Println("All workers done, shutting down!")
}
func handler(ctx context.Context, record *sqs.Message) error {
log.Println("Message received : ", *record.MessageId, *record.Body)
time.Sleep(time.Second * 10)
log.Println("Message Proccessed")
return nil
}
func setTimeout(wg *sync.WaitGroup, timeout time.Duration) {
done := make(chan struct{})
go func() {
defer close(done)
wg.Wait()
}()
select {
case <-done:
log.Println("Wait group Done")
return
case <-time.After(timeout):
log.Println("timeout")
return
}
}