-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe_liveness.go
66 lines (53 loc) · 1.74 KB
/
probe_liveness.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
package cmd
import (
"context"
"database/sql"
"fmt"
"github.com/danielmoisa/trip-planner/internal/api/handlers/common"
"github.com/danielmoisa/trip-planner/internal/config"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
// livenessCmd represents the server command
var livenessCmd = &cobra.Command{
Use: "liveness",
Short: "Runs liveness probes",
Long: `Runs connection livenesss probes
This command triggers the same livenesss probes as in
/-/healthy (apart from the actual server.ready
probe) and prints the results to stdout. Fails with
non zero exitcode on encountered errors.
A typical usecase of this command are liveness probes
to take action if dependant services (e.g. DB, NFS
mounts) become unstable. You may also use this to
ensure all requirements are fulfilled before starting
the app server.`,
Run: func(cmd *cobra.Command, _ []string /* args */) {
verbose, err := cmd.Flags().GetBool(verboseFlag)
if err != nil {
log.Fatal().Err(err).Msg("Failed to parse args")
}
runLiveness(verbose)
},
}
func init() {
probeCmd.AddCommand(livenessCmd)
livenessCmd.Flags().BoolP(verboseFlag, "v", false, "Show verbose output.")
}
func runLiveness(verbose bool) {
config := config.DefaultServiceConfigFromEnv()
db, err := sql.Open("postgres", config.Database.ConnectionString())
if err != nil {
log.Fatal().Err(err).Msg("Failed to connect to the database")
}
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), config.Management.LivenessTimeout)
defer cancel()
str, errs := common.ProbeLiveness(ctx, db, config.Management.ProbeWriteablePathsAbs, config.Management.ProbeWriteableTouchfile)
if verbose {
fmt.Print(str)
}
if len(errs) > 0 {
log.Fatal().Errs("errs", errs).Msg("Unhealthy.")
}
}