-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffered_queue.go
More file actions
77 lines (67 loc) · 1.66 KB
/
buffered_queue.go
File metadata and controls
77 lines (67 loc) · 1.66 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
req "github.com/simplegear/rate-envelope-queue"
)
func main() {
log.Printf("Starting buffered rate envelope queue example now %s", time.Now().Format(time.RFC3339))
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.NewRateEnvelopeQueue(
parent,
"some_queue",
req.WithLimitOption(3),
req.WithWaitingOption(false), // always true in stop mode 'Drain' and 'Stop'
req.WithStopModeOption(req.Drain),
//req.WithStopModeOption(req.Stop),
)
envelope1, _ := req.NewEnvelope(
req.WithType("task_type_1"),
req.WithId(1),
req.WithInvoke(func(ctx context.Context, envelope *req.Envelope) error {
fmt.Println("envelope 1: invoked")
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(5 * time.Second):
}
fmt.Println("envelope 1: completed")
return nil
}),
req.WithDeadline(20*time.Second),
req.WithScheduleModeInterval(0),
)
envelope2, _ := req.NewEnvelope(
req.WithType("task_type_2"),
req.WithId(2),
req.WithInvoke(func(ctx context.Context, envelope *req.Envelope) error {
fmt.Println("envelope 2: invoked")
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(7 * time.Second):
}
fmt.Println("envelope 2: completed")
return nil
}),
req.WithDeadline(20*time.Second),
req.WithScheduleModeInterval(0),
)
_ = queue.Send(envelope1, envelope2)
queue.Start()
queue.Stop()
queue.Terminate()
}