Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
93 lines (79 loc) · 1.79 KB
/
Copy pathmain.go
File metadata and controls
93 lines (79 loc) · 1.79 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"time"
"github.com/thoas/bokchoy"
"github.com/thoas/bokchoy/contrib/rpc"
"github.com/thoas/bokchoy/middleware"
"google.golang.org/grpc/codes"
)
const (
queueName = "tasks.message"
)
func main() {
var (
err error
ctx = context.Background()
run string
rpcPort int
)
flag.StringVar(&run, "run", "", "service to run")
flag.IntVar(&rpcPort, "rpc-port", 9090, "port for rpc server")
flag.Parse()
engine, err := bokchoy.New(ctx, bokchoy.Config{
Broker: bokchoy.BrokerConfig{
Type: "redis",
Redis: bokchoy.RedisConfig{
Type: "client",
Client: bokchoy.RedisClientConfig{
Addr: "localhost:6379",
},
},
},
})
if err != nil {
log.Fatal(err)
}
engine.Use(middleware.DefaultLogger)
switch run {
case "client":
clt := rpc.NewClient(fmt.Sprintf(":%d", rpcPort), rpc.ClientOptions{
MaxRetries: 3,
PerRetryTimeout: 1 * time.Second,
RetryCodes: []codes.Code{codes.Unavailable},
})
task, err := clt.PublishTask(ctx, queueName, []byte(`{"data": "hello world"}`))
if err != nil {
log.Fatalf("could not retrieve result: %v", err)
}
log.Printf("Task published: %+v", task)
case "producer":
task, err := engine.Queue(queueName).Publish(ctx, map[string]string{
"data": "hello world",
})
if err != nil {
log.Fatal(err)
}
fmt.Println(task, "has been published")
case "worker":
engine.Queue(queueName).HandleFunc(func(r *bokchoy.Request) error {
return nil
})
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
log.Print("Received signal, gracefully stopping")
engine.Stop(ctx)
}
}()
engine.Run(ctx, bokchoy.WithServers([]bokchoy.Server{
rpc.NewServer(engine, rpcPort),
}))
}
}