Skip to content

Commit 947c929

Browse files
committed
[chore]: added dependency prober
1 parent de1d9e0 commit 947c929

3 files changed

Lines changed: 45 additions & 18 deletions

File tree

inits.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/naughtygopher/errors"
1111
"github.com/naughtygopher/proberesponder"
12+
"github.com/naughtygopher/proberesponder/extensions/depprober"
1213
proberespHTTP "github.com/naughtygopher/proberesponder/extensions/http"
1314
"github.com/naughtygopher/webgo/v7"
1415

@@ -47,6 +48,12 @@ func startServers(svr api.Server, cfgs *configs.Configs, fatalErr chan<- error)
4748
}
4849

4950
go func() {
51+
defer func() {
52+
rec := recover()
53+
if rec != nil {
54+
fatalErr <- errors.New(fmt.Sprintf("%+v", rec))
55+
}
56+
}()
5057
err = hserver.Start()
5158
if err != nil {
5259
fatalErr <- errors.Wrap(err, "failed to start HTTP server")
@@ -98,6 +105,7 @@ func startHealthResponder(ctx context.Context, ps *proberesponder.ProbeResponder
98105

99106
func start(
100107
ctx context.Context,
108+
probestatus *proberesponder.ProbeResponder,
101109
cfgs *configs.Configs,
102110
fatalErr chan<- error,
103111
) (hserver *xhttp.HTTP, gserver *grpc.GRPC) {
@@ -107,6 +115,18 @@ func start(
107115
panic(errors.Wrap(err))
108116
}
109117

118+
depprober.Start(time.Minute, probestatus, &depprober.Probe{
119+
ID: "postgres",
120+
AffectedStatuses: []proberesponder.Statuskey{proberesponder.StatusLive, proberesponder.StatusReady},
121+
Checker: depprober.CheckerFunc(func(ctx context.Context) error {
122+
err := pqdriver.Ping(ctx)
123+
if err != nil {
124+
return errors.Wrap(err, "postgres ping failed")
125+
}
126+
return nil
127+
}),
128+
})
129+
110130
userPGstore := users.NewPostgresStore(pqdriver, cfgs.UserPostgresTable())
111131
userSvc := users.NewService(userPGstore)
112132
svrAPIs := api.NewServer(userSvc, nil)

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
// So that even if the main function panics we can produce required logs for troubleshooting
1919
var exitErr error
2020

21-
func recoverer(ctx context.Context) {
21+
func recoverer() {
2222
exitCode := 0
2323
var exitInfo any
2424
rec := recover()
@@ -39,6 +39,7 @@ func recoverer(ctx context.Context) {
3939
exitCode = 0
4040
}
4141

42+
ctx := context.Background()
4243
// logging this because we have info logs saying "listening to" various port numbers
4344
// based on the server type (gRPC, HTTP etc.). But it's unclear *from the logs*
4445
// if the server is up and running, if it exits for any reason
@@ -52,14 +53,14 @@ func recoverer(ctx context.Context) {
5253
}
5354

5455
func main() {
56+
defer recoverer()
5557
var (
5658
ctx = context.Background()
5759
fatalErr = make(chan error, 1)
5860
shutdownGraceperiod = time.Minute
5961
probeInterval = time.Second * 3
6062
probestatus = proberesponder.New()
6163
)
62-
defer recoverer(ctx)
6364

6465
cfgs, err := configs.New()
6566
if err != nil {
@@ -78,7 +79,7 @@ func main() {
7879
panic(err)
7980
}
8081

81-
hserver, gserver := start(ctx, cfgs, fatalErr)
82+
hserver, gserver := start(ctx, probestatus, cfgs, fatalErr)
8283

8384
// by now all the intended servers, subscribers etc. are up and running.
8485
probestatus.SetNotStarted(false)

shutdown.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,32 @@ func shutdownDependenciesAndServices(
7070
apmIns *apm.APM,
7171
) {
7272
wgroup := &sync.WaitGroup{}
73-
wgroup.Add(1)
74-
go func() {
75-
defer wgroup.Done()
76-
_ = httpServer.Shutdown(ctx)
77-
}()
73+
if httpServer != nil {
74+
wgroup.Add(1)
75+
go func() {
76+
defer wgroup.Done()
77+
_ = httpServer.Shutdown(ctx)
78+
}()
79+
}
7880

79-
wgroup.Add(1)
80-
go func() {
81-
defer wgroup.Done()
82-
_ = grpcServer.Shutdown(ctx)
83-
}()
81+
if grpcServer != nil {
82+
wgroup.Add(1)
83+
go func() {
84+
defer wgroup.Done()
85+
_ = grpcServer.Shutdown(ctx)
86+
}()
87+
}
8488

8589
// after all the APIs of the application are shutdown (e.g. HTTP, gRPC, Pubsub listener etc.)
8690
// we should close connections to dependencies like database, cache etc.
8791
// This should only be done after the APIs are shutdown completely
88-
wgroup.Add(1)
89-
go func() {
90-
defer wgroup.Done()
91-
_ = apmIns.Shutdown(ctx)
92-
}()
92+
if apmIns != nil {
93+
wgroup.Add(1)
94+
go func() {
95+
defer wgroup.Done()
96+
_ = apmIns.Shutdown(ctx)
97+
}()
98+
}
9399

94100
wgroup.Wait()
95101
}

0 commit comments

Comments
 (0)