-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_queue_with_simple_dynamic_envelopes.go
More file actions
79 lines (69 loc) · 1.32 KB
/
simple_queue_with_simple_dynamic_envelopes.go
File metadata and controls
79 lines (69 loc) · 1.32 KB
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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
req "github.com/simplegear/rate-envelope-queue"
)
func main() {
parent, cancel := context.WithCancel(context.Background())
defer cancel()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
go func() {
<-sig
fmt.Println("signal shutdown")
cancel()
}()
queue := req.NewSimpleDrainQueue(
parent,
"some_queue",
5,
)
queue.Start()
go func() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
i := 0
for {
select {
case <-parent.Done():
fmt.Println("sender: done")
return
case <-ticker.C:
envelope, err := req.NewDynamicEnvelope(
1*time.Second,
func(ctx context.Context, envelope *req.Envelope) error {
i++
fmt.Println("📧 Email v1", i, time.Now())
return nil
},
nil,
)
if err != nil {
fmt.Println("main: can't create envelope", err)
continue
}
err = queue.Send(envelope)
if err != nil {
fmt.Println("main: can't send envelope to queue", err)
continue
}
}
}
}()
go func() {
select {
case <-time.After(25 * time.Second):
fmt.Println("main: timeout")
cancel()
}
}()
<-parent.Done()
fmt.Println("parent: done")
queue.Stop()
fmt.Println("queue: done")
}