-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_queue_with_simple_schedule_envelopes.go
More file actions
107 lines (94 loc) · 1.94 KB
/
simple_queue_with_simple_schedule_envelopes.go
File metadata and controls
107 lines (94 loc) · 1.94 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
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() {
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")
}