-
Notifications
You must be signed in to change notification settings - Fork 0
/
datagather_service.go
110 lines (96 loc) · 2.95 KB
/
datagather_service.go
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
package app
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
"github.com/ijsong/farseer/internal/service"
"github.com/ijsong/farseer/pkg/queue"
"google.golang.org/grpc"
)
const (
datagatherTopic = "_topic_datagather"
)
type DatagatherService struct {
numberOfProducers int
producers []*queue.EmbeddedQueueProducer
}
func NewDatagatherService(producers []*queue.EmbeddedQueueProducer) *DatagatherService {
return &DatagatherService{
numberOfProducers: len(producers),
producers: producers,
}
}
func (ds *DatagatherService) RegisterService(grpcServer *grpc.Server) {
service.RegisterDatagatherServiceServer(grpcServer, ds)
}
func (ds *DatagatherService) CreateEvent(ctx context.Context, req *service.CreateEventRequest) (*types.Empty, error) {
// TODO: Check message
if req.Event == nil {
return nil, service.NewNotInitiatedMessageError("CreateEvent")
//return nil, fmt.Errorf("nil event")
}
if req.Event.Timestamp.IsZero() {
req.Event.Timestamp = time.Now()
}
if err := ds.publishDatagatherRequest(req); err != nil {
return nil, err
}
return &types.Empty{}, nil
}
func (ds *DatagatherService) CreateItem(ctx context.Context, req *service.CreateItemRequest) (*types.Empty, error) {
if req.Item == nil {
return nil, fmt.Errorf("nil event")
}
if err := ds.publishDatagatherRequest(req); err != nil {
return nil, err
}
return &types.Empty{}, nil
}
func (ds *DatagatherService) DeleteItem(ctx context.Context, req *service.DeleteItemRequest) (*types.Empty, error) {
if err := ds.publishDatagatherRequest(req); err != nil {
return nil, err
}
return &types.Empty{}, nil
}
func (ds *DatagatherService) UpdateItem(ctx context.Context, req *service.UpdateItemRequest) (*types.Empty, error) {
if err := ds.publishDatagatherRequest(req); err != nil {
return nil, err
}
return &types.Empty{}, nil
}
func (ds *DatagatherService) CreateUser(ctx context.Context, req *service.CreateUserRequest) (*types.Empty, error) {
if err := ds.publishDatagatherRequest(req); err != nil {
return nil, err
}
return &types.Empty{}, nil
}
func (ds *DatagatherService) DeleteUser(ctx context.Context, req *service.DeleteUserRequest) (*types.Empty, error) {
if err := ds.publishDatagatherRequest(req); err != nil {
return nil, err
}
return &types.Empty{}, nil
}
func (ds *DatagatherService) UpdateUser(ctx context.Context, req *service.UpdateUserRequest) (*types.Empty, error) {
if err := ds.publishDatagatherRequest(req); err != nil {
return nil, err
}
return &types.Empty{}, nil
}
func (ds *DatagatherService) publishDatagatherRequest(req interface{}) error {
dataReq := &service.DatagatherRequest{}
if !dataReq.SetValue(req) {
return fmt.Errorf("invalid argument: %v", req)
}
bytes, err := proto.Marshal(dataReq)
if err != nil {
return err
}
producer := ds.producers[rand.Intn(ds.numberOfProducers)]
if err := producer.Publish(bytes); err != nil {
return err
}
return nil
}