/
health_postgres.go
62 lines (53 loc) · 1.82 KB
/
health_postgres.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
// Copyright © 2019 by PACE Telematics GmbH. All rights reserved.
// Created at 2019/10/18 by Charlotte Pröller
package postgres
import (
"time"
"github.com/go-pg/pg"
"github.com/pace/bricks/maintenance/health/servicehealthcheck"
)
// HealthCheck checks the state of a postgres connection. It must not be changed
// after it was registered as a health check.
type HealthCheck struct {
state servicehealthcheck.ConnectionState
Pool *pg.DB
}
// Init initialises the test table
func (h *HealthCheck) Init() error {
_, errWrite := h.Pool.Exec(`CREATE TABLE IF NOT EXISTS ` + cfg.HealthCheckTableName + `(ok boolean);`)
return errWrite
}
// HealthCheck performs the read test on the database. If enabled, it performs a
// write test as well.
func (h *HealthCheck) HealthCheck() (bool, error) {
if time.Since(h.state.LastChecked()) <= cfg.HealthCheckResultTTL {
// the last result of the Health Check is still not outdated
return h.state.GetState()
}
// Readcheck
if _, err := h.Pool.Exec(`SELECT 1;`); err != nil {
h.state.SetErrorState(err)
return h.state.GetState()
}
// writecheck - add Data to configured Table
_, err := h.Pool.Exec("INSERT INTO " + cfg.HealthCheckTableName + "(ok) VALUES (true);")
if err != nil {
h.state.SetErrorState(err)
return h.state.GetState()
}
// and while we're at it, check delete as well (so as not to clutter the database
// because UPSERT is impractical here
_, err = h.Pool.Exec("DELETE FROM " + cfg.HealthCheckTableName + ";")
if err != nil {
h.state.SetErrorState(err)
return h.state.GetState()
}
// If no error occurred set the State of this Health Check to healthy
h.state.SetHealthy()
return h.state.GetState()
}
// CleanUp drops the test table.
func (h *HealthCheck) CleanUp() error {
_, err := h.Pool.Exec("DROP TABLE IF EXISTS " + cfg.HealthCheckTableName)
return err
}