-
Notifications
You must be signed in to change notification settings - Fork 24
/
provide.go
115 lines (98 loc) · 2.79 KB
/
provide.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
package jobs
import (
"context"
"go.uber.org/fx"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/fluxninja/aperture/v2/pkg/log"
)
// swagger:operation POST /liveness common-configuration Liveness
// ---
// x-fn-config-env: true
// parameters:
// - name: service
// in: body
// schema:
// "$ref": "#/definitions/JobConfig"
// - name: scheduler
// in: body
// schema:
// "$ref": "#/definitions/JobGroupConfig"
// swagger:operation POST /readiness common-configuration Readiness
// ---
// x-fn-config-env: true
// parameters:
// - name: service
// in: body
// schema:
// "$ref": "#/definitions/JobConfig"
// - name: scheduler
// in: body
// schema:
// "$ref": "#/definitions/JobGroupConfig"
// Default jobs groups and names.
const (
livenessGroup = "liveness"
livenessMultiJobName = "service"
readinessGroup = "readiness"
readinessMultiJobName = "service"
)
// Module is a fx module that provides default jobs for internal service components.
func Module() fx.Option {
return fx.Options(
provideServiceLivenessCheck(),
provideServiceReadinessCheck(),
fx.Invoke(registerSelfChecks),
)
}
// Liveness job for internal service components (MultiJob).
func provideServiceLivenessCheck() fx.Option {
mjc := MultiJobConstructor{
Name: livenessMultiJobName,
JobGroupName: livenessGroup,
}
return fx.Options(
JobGroupConstructor{Name: livenessGroup}.Annotate(),
mjc.Annotate(),
)
}
// Readiness job for internal service components (MultiJob).
func provideServiceReadinessCheck() fx.Option {
mjc := MultiJobConstructor{
Name: readinessMultiJobName,
JobGroupName: readinessGroup,
}
return fx.Options(
JobGroupConstructor{Name: readinessGroup}.Annotate(),
mjc.Annotate(),
)
}
// SelfChecksIn holds parameters for RegisterSelfChecks.
type SelfChecksIn struct {
fx.In
Liveness *MultiJob `name:"liveness.service"`
Readiness *MultiJob `name:"readiness.service"`
}
// RegisterSelfChecks registers self check jobs (liveness, readiness) for internal service components.
func registerSelfChecks(sc SelfChecksIn) {
liveness := NewBasicJob("job-module", checkSelfLiveness)
err := sc.Liveness.RegisterJob(liveness)
if err != nil {
log.Error().Err(err).Msg("Unable to register liveness job")
return
}
readiness := NewBasicJob("job-module", checkSelfReadiness)
err = sc.Readiness.RegisterJob(readiness)
if err != nil {
log.Error().Err(err).Msg("Unable to register readiness job")
return
}
}
func checkSelfLiveness(context.Context) (proto.Message, error) {
message := wrapperspb.String("Liveness job module is alive")
return message, nil
}
func checkSelfReadiness(context.Context) (proto.Message, error) {
message := wrapperspb.String("Readiness job module is alive")
return message, nil
}