This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathsignal_service.go
147 lines (125 loc) · 4.42 KB
/
signal_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
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
package rpc
import (
"context"
"fmt"
"runtime/debug"
manager "github.com/flyteorg/flyteadmin/pkg/manager/impl"
"github.com/flyteorg/flyteadmin/pkg/manager/interfaces"
"github.com/flyteorg/flyteadmin/pkg/repositories"
"github.com/flyteorg/flyteadmin/pkg/repositories/errors"
"github.com/flyteorg/flyteadmin/pkg/rpc/adminservice/util"
runtimeIfaces "github.com/flyteorg/flyteadmin/pkg/runtime/interfaces"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/service"
"github.com/flyteorg/flytestdlib/logger"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/golang/protobuf/proto"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type SignalMetrics struct {
scope promutils.Scope
panicCounter prometheus.Counter
create util.RequestMetrics
get util.RequestMetrics
}
func NewSignalMetrics(scope promutils.Scope) SignalMetrics {
return SignalMetrics{
scope: scope,
panicCounter: scope.MustNewCounter("handler_panic",
"panics encountered while handling requests to the admin service"),
create: util.NewRequestMetrics(scope, "create_signal"),
get: util.NewRequestMetrics(scope, "get_signal"),
}
}
type SignalService struct {
service.UnimplementedSignalServiceServer
signalManager interfaces.SignalInterface
metrics SignalMetrics
}
func NewSignalServer(ctx context.Context, configuration runtimeIfaces.Configuration, adminScope promutils.Scope) *SignalService {
panicCounter := adminScope.MustNewCounter("initialization_panic",
"panics encountered initializing the signal service")
defer func() {
if err := recover(); err != nil {
panicCounter.Inc()
logger.Fatalf(ctx, fmt.Sprintf("caught panic: %v [%+v]", err, string(debug.Stack())))
}
}()
databaseConfig := configuration.ApplicationConfiguration().GetDbConfig()
logConfig := logger.GetConfig()
db, err := repositories.GetDB(ctx, databaseConfig, logConfig)
if err != nil {
logger.Fatal(ctx, err)
}
dbScope := adminScope.NewSubScope("database")
repo := repositories.NewGormRepo(
db, errors.NewPostgresErrorTransformer(adminScope.NewSubScope("errors")), dbScope)
signalManager := manager.NewSignalManager(repo, adminScope.NewSubScope("signal_manager"))
logger.Info(ctx, "Initializing a new SignalService")
return &SignalService{
signalManager: signalManager,
metrics: NewSignalMetrics(adminScope),
}
}
// Intercepts all admin requests to handle panics during execution.
func (s *SignalService) interceptPanic(ctx context.Context, request proto.Message) {
err := recover()
if err == nil {
return
}
s.metrics.panicCounter.Inc()
logger.Fatalf(ctx, "panic-ed for request: [%+v] with err: %v with Stack: %v", request, err, string(debug.Stack()))
}
func (s *SignalService) GetOrCreateSignal(
ctx context.Context, request *admin.SignalGetOrCreateRequest) (*admin.Signal, error) {
defer s.interceptPanic(ctx, request)
if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "Incorrect request, nil requests not allowed")
}
var response *admin.Signal
var err error
s.metrics.create.Time(func() {
response, err = s.signalManager.GetOrCreateSignal(ctx, *request)
})
if err != nil {
return nil, util.TransformAndRecordError(err, &s.metrics.create)
}
s.metrics.create.Success()
return response, nil
}
func (s *SignalService) ListSignals(
ctx context.Context, request *admin.SignalListRequest) (*admin.SignalList, error) {
defer s.interceptPanic(ctx, request)
if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "Incorrect request, nil requests not allowed")
}
var response *admin.SignalList
var err error
s.metrics.get.Time(func() {
response, err = s.signalManager.ListSignals(ctx, *request)
})
if err != nil {
return nil, util.TransformAndRecordError(err, &s.metrics.get)
}
s.metrics.get.Success()
return response, nil
}
func (s *SignalService) SetSignal(
ctx context.Context, request *admin.SignalSetRequest) (*admin.SignalSetResponse, error) {
defer s.interceptPanic(ctx, request)
if request == nil {
return nil, status.Errorf(codes.InvalidArgument, "Incorrect request, nil requests not allowed")
}
var response *admin.SignalSetResponse
var err error
s.metrics.get.Time(func() {
response, err = s.signalManager.SetSignal(ctx, *request)
})
if err != nil {
return nil, util.TransformAndRecordError(err, &s.metrics.get)
}
s.metrics.get.Success()
return response, nil
}