-
Notifications
You must be signed in to change notification settings - Fork 5
/
monitoring_service.go
44 lines (39 loc) · 1.09 KB
/
monitoring_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
package sqsd
import (
"context"
"time"
)
// MonitoringService provides grpc handler for MonitoringService.
type MonitoringService struct {
UnimplementedMonitoringServiceServer
worker *worker
}
// NewMonitoringService returns new MonitoringService object.
func NewMonitoringService(consumer *worker) *MonitoringService {
return &MonitoringService{
worker: consumer,
}
}
// CurrentWorkings handles CurrentWorkings grpc request using actor system.
func (s *MonitoringService) CurrentWorkings(ctx context.Context, _ *CurrentWorkingsRequest) (*CurrentWorkingsResponse, error) {
tasks := s.worker.CurrentWorkings(ctx)
return &CurrentWorkingsResponse{Tasks: tasks}, nil
}
// WaitUntilAllEnds waits until all worker tasks finishes.
func (s *MonitoringService) WaitUntilAllEnds(timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
if err := ctx.Err(); err != nil {
return err
}
resp, err := s.CurrentWorkings(ctx, nil)
if err != nil {
return err
}
if len(resp.Tasks) == 0 {
return nil
}
time.Sleep(time.Second)
}
}