blob: 21442a010b04f9d1e7c51762384559bc58f7c82e (
plain)
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
|
package main
import (
"fmt"
"os"
"github.com/spiral/broadcast/v2"
rr "github.com/spiral/roadrunner/cmd/rr/cmd"
"github.com/spiral/roadrunner/service/rpc"
"golang.org/x/sync/errgroup"
)
type logService struct {
broadcast *broadcast.Service
stop chan interface{}
}
func (l *logService) Init(service *broadcast.Service) (bool, error) {
l.broadcast = service
return true, nil
}
func (l *logService) Serve() error {
l.stop = make(chan interface{})
client := l.broadcast.NewClient()
if err := client.SubscribePattern("tests/*"); err != nil {
return err
}
logFile, _ := os.Create("log.txt")
g := &errgroup.Group{}
g.Go(func() error {
for msg := range client.Channel() {
_, err := logFile.Write([]byte(fmt.Sprintf(
"%s: %s\n",
msg.Topic,
string(msg.Payload),
)))
if err != nil {
return err
}
err = logFile.Sync()
if err != nil {
return err
}
}
return nil
})
<-l.stop
err := logFile.Close()
if err != nil {
return err
}
err = client.Close()
if err != nil {
return err
}
return g.Wait()
}
func (l *logService) Stop() {
close(l.stop)
}
func main() {
rr.Container.Register(rpc.ID, &rpc.Service{})
rr.Container.Register(broadcast.ID, &broadcast.Service{})
rr.Container.Register("log", &logService{})
rr.Execute()
}
|