-
Notifications
You must be signed in to change notification settings - Fork 2
/
grpc.go
216 lines (174 loc) · 5.61 KB
/
grpc.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package server
// Here we define all GRPC handlers
import (
"fmt"
"log"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"github.com/segmentio/ksuid"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection"
pb "github.com/dhrp/moulin/pkg/protobuf"
"github.com/dhrp/moulin/pkg/rouge"
)
type server struct {
rouge *rouge.Client
pb.UnimplementedAPIServer
}
// NewGRPCServer returns a new GRPC server
func NewGRPCServer(rougeClient *rouge.Client) *grpc.Server {
// Initialize the API Server
s := &server{rouge: rougeClient}
// Setup grpc server
grpcServer := grpc.NewServer()
// Register reflection service on gRPC server.
pb.RegisterAPIServer(grpcServer, s)
// Register reflection service on gRPC server.
reflection.Register(grpcServer)
return grpcServer
}
func (s *server) Healthz(ctx context.Context, in *empty.Empty) (*pb.StatusMessage, error) {
md, ok := metadata.FromIncomingContext(ctx)
if ok {
fmt.Println("read incoming context successful:")
fmt.Println(md)
fmt.Println(md["authentication"])
} else {
fmt.Println("failed to load context (meta)")
}
result, _ := s.rouge.Info()
return &pb.StatusMessage{
Status: pb.Status_SUCCESS,
Detail: result,
}, nil
}
func (s *server) PushTask(ctx context.Context, in *pb.Task) (*pb.StatusMessage, error) {
md, ok := metadata.FromIncomingContext(ctx)
if ok {
//ToDo: on http we get context, but authorization may not be set
var authorization string
// var ok bool
if _, ok = md["authorization"]; ok {
authorization = md["authorization"][0]
}
fmt.Println("authorization: " + authorization)
} else {
fmt.Println("failed to load context (meta)")
}
queueID := in.QueueID
taskID := ksuid.New().String()
taskMessage := rouge.TaskMessage{ID: taskID, Body: in.Body}
queueLength, err := s.rouge.AddTask(queueID, taskMessage)
if err != nil {
return nil, errors.Wrap(err, "Failed to push task")
}
result := fmt.Sprintf("queue %v now %d items long", queueID, queueLength)
return &pb.StatusMessage{
Status: pb.Status_SUCCESS,
Detail: result,
}, nil
}
// LoadTask returns a task from the redis queue
func (s *server) LoadTask(ctx context.Context, in *pb.RequestMessage) (*pb.Task, error) {
queueID := in.QueueID
taskMessage, err := s.rouge.Load(ctx, queueID, 300)
if err != nil {
err = errors.Wrap(err, "[grpc.go] error in loading message")
log.Println(err)
return &pb.Task{}, err
}
return &pb.Task{TaskID: taskMessage.ID, Body: taskMessage.Body}, nil
}
// LoadTask returns a task from the redis queue
func (s *server) HeartBeat(ctx context.Context, in *pb.Task) (*pb.StatusMessage, error) {
queueID := in.QueueID
taskID := in.TaskID
var expirationSec int32
if in.ExpirationSec != 0 {
expirationSec = in.ExpirationSec
} else {
expirationSec = 300 // default expiration is 5 min
}
expires, err := s.rouge.Heartbeat(queueID, taskID, expirationSec)
if err != nil {
// ToDo return error to client, it could be because item was just updated
log.Println(err)
return &pb.StatusMessage{
Status: pb.Status_FAILURE,
Detail: fmt.Sprintf("Heartbeat failed, task not found or updated in the same second"),
}, nil
}
return &pb.StatusMessage{
Status: pb.Status_SUCCESS,
Detail: fmt.Sprintf("Heartbeat successful, task will now expire at: %v", expires),
}, nil
}
// Complete moves a task from the running to the complete set
func (s *server) Complete(ctx context.Context, in *pb.Task) (*pb.StatusMessage, error) {
queueID := in.QueueID
taskID := in.TaskID
_, err := s.rouge.Complete(queueID, taskID)
if err != nil {
return &pb.StatusMessage{Status: pb.Status_FAILURE, Detail: err.Error()}, err
}
return &pb.StatusMessage{
Status: pb.Status_SUCCESS,
Detail: "successfully marked item as complete",
}, nil
}
// Fail moves a task from the running queue to the failed set
func (s *server) Fail(ctx context.Context, in *pb.Task) (*pb.StatusMessage, error) {
queueID := in.QueueID
taskID := in.TaskID
_, err := s.rouge.Fail(queueID, taskID)
if err != nil {
return &pb.StatusMessage{Status: pb.Status_FAILURE, Detail: err.Error()}, err
}
return &pb.StatusMessage{
Status: pb.Status_SUCCESS,
Detail: "successfully marked item as failed",
}, nil
}
// Progress returns a status struct about the requested queue
func (s *server) Progress(ctx context.Context, in *pb.RequestMessage) (*pb.QueueProgress, error) {
queueInfo, err := s.rouge.Progress(in.QueueID)
if err != nil {
return nil, grpc.Errorf(codes.Unknown, "could not get progress")
}
return queueInfo.ToBuff(), nil
}
func (s *server) ListQueues(ctx context.Context, in *empty.Empty) (*pb.QueueMap, error) {
queueMap := &pb.QueueMap{Queues: make(map[string]*pb.QueueProgress)}
queues, err := s.rouge.ListQueues()
if err != nil {
return nil, grpc.Errorf(codes.Unknown, "could not get progress")
}
// for each queue in the map, create a &pb.QueueProgress
for queueName, queueInfo := range queues {
queueMap.Queues[queueName] = queueInfo.ToBuff()
}
return queueMap, nil
}
// Peek returns a count and messageList
func (s *server) Peek(ctx context.Context, in *pb.RequestMessage) (*pb.TaskList, error) {
// var task pb.Task
taskList := &pb.TaskList{}
// get TaskMessageList
count, tml, err := s.rouge.Peek(in.QueueID, in.Phase, int(in.Limit))
if err != nil {
return nil, grpc.Errorf(codes.Unknown, "could not get progress")
}
taskList.TotalItems = int32(count)
for i := 0; i < len(tml); i++ {
task := &pb.Task{
TaskID: tml[i].ID,
Body: tml[i].Body,
Envs: tml[i].Envs,
}
taskList.Tasks = append(taskList.Tasks, task)
}
return taskList, nil
}