-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_queue_with_simple_combine_envelopes.go
More file actions
139 lines (125 loc) · 2.6 KB
/
simple_queue_with_simple_combine_envelopes.go
File metadata and controls
139 lines (125 loc) · 2.6 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
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
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(500 * time.Millisecond)
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() {
envelope1, err := req.NewScheduleEnvelope(
2*time.Second,
1*time.Second,
func(ctx context.Context, envelope *req.Envelope) error {
fmt.Println("📧 Email v1", time.Now())
return nil
},
nil,
)
if err != nil {
fmt.Println("main: can't create envelope1", err)
return
}
err = queue.Send(envelope1)
if err != nil {
fmt.Println("main: can't send envelope1 to queue", err)
return
}
envelope2, err := req.NewScheduleEnvelope(
4*time.Second,
3*time.Second,
func(ctx context.Context, envelope *req.Envelope) error {
fmt.Println("📧 Email v2", time.Now())
return nil
},
nil,
)
if err != nil {
fmt.Println("main: can't create envelope2", err)
return
}
err = queue.Send(envelope2)
if err != nil {
fmt.Println("main: can't send envelope2 to queue", err)
return
}
envelope3, err := req.NewScheduleEnvelope(
6*time.Second,
2*time.Second,
func(ctx context.Context, envelope *req.Envelope) error {
fmt.Println("📧 Email v3", time.Now())
return nil
},
nil,
)
if err != nil {
fmt.Println("main: can't create envelope3", err)
return
}
err = queue.Send(envelope3)
if err != nil {
fmt.Println("main: can't send envelope3 to queue", err)
return
}
}()
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")
}