-
Notifications
You must be signed in to change notification settings - Fork 12
/
health.go
155 lines (130 loc) · 3.45 KB
/
health.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
148
149
150
151
152
153
154
155
package main
import (
"fmt"
"log"
"net/http"
"regexp"
"strings"
"sync"
"time"
"github.com/frou/stdext"
"github.com/tzdybal/go-disk-usage/du"
)
// Define HTTP handlers that an automated monitoring system can access to keep
// an eye on the health of the daemon.
//
// e.g.
//
// Request:
// /health/disk_low
// Response:
// CONCERN
//
// Request:
// /health
// Response:
// disk_low OK
// ytdl_old CONCERN
// feeds_stale OK
const (
httpHealthPrefix = "/health/"
)
//nolint:gochecknoglobals
var (
healthConcerns = map[string]healthFunc{
"disk_low": diskLow,
"ytdl_old": downloaderOld,
"feeds_stale": feedsStale,
}
lastDownloaderVersionCheck struct {
mu sync.Mutex
when time.Time
result string
}
)
const (
// @todo #0 Make diskLowThreshold & ytdlOldThreshold customizable in the config file.
diskLowThreshold = 1024 * 1024 * 1024 // 1GB
downloaderOldThreshold = time.Hour * 24 * 60 // 60 days
// @todo #0 Make feedsStaleThreshold customizable per-podcast in the config file.
feedsStaleThreshold = time.Hour * 24 * 10 // 10 days
downloaderVersionCheckCacheDuration = time.Minute * 5
)
func healthHandler(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, httpHealthPrefix)
check := func(name string, f healthFunc) string {
flag, err := f()
if err != nil {
log.Printf("health: %v: %v", name, err)
}
if err != nil || flag {
return "CONCERN"
}
return "OK"
}
f, found := healthConcerns[name]
if found {
fmt.Fprintln(w, check(name, f))
} else {
if name == "" {
for name, f := range healthConcerns {
fmt.Fprintln(w, name, "\t", check(name, f))
}
} else {
http.NotFound(w, r)
}
}
}
// ------------------------------------------------------------
// The bool flag being true or there being an error means cause for concern.
type healthFunc func() (bool, error)
func diskLow() (bool, error) {
ok := du.NewDiskUsage(".").Available() < diskLowThreshold
return ok, nil
}
var yearMonthDayRevnumVersionRE = regexp.MustCompile(`^(\d+\.\d+\.\d+)(\.\d+)?$`)
func downloaderOld() (bool, error) {
var version string
lastDownloaderVersionCheck.mu.Lock()
defer lastDownloaderVersionCheck.mu.Unlock()
if time.Since(lastDownloaderVersionCheck.when) < downloaderVersionCheckCacheDuration {
version = lastDownloaderVersionCheck.result
} else {
var err error
version, err = getDownloaderCommandVersion()
if err != nil {
return false, err
}
lastDownloaderVersionCheck.when = time.Now()
lastDownloaderVersionCheck.result = version
}
submatches := yearMonthDayRevnumVersionRE.FindStringSubmatch(version)
if submatches == nil {
return false, fmt.Errorf("Can't parse downloader command's version output %q because it has an unexpected format", version)
}
versionTime, err := time.Parse("2006.1.2", submatches[1])
if err != nil {
return false, err
}
age := time.Since(versionTime)
return age > downloaderOldThreshold, nil
}
func feedsStale() (bool, error) {
return time.Since(lastTimeAnyFeedWritten.Get()) > feedsStaleThreshold, nil
}
// ------------------------------------------------------------
type concTime struct {
a *stdext.ConcAtom
}
func newConcTime() *concTime {
var zval time.Time
return &concTime{a: stdext.NewConcAtom(zval)}
}
func (t *concTime) Get() time.Time {
return t.a.Deref().(time.Time)
}
func (t *concTime) Set(val time.Time) {
t.a.Replace(val)
}
//nolint:gochecknoglobals
var lastTimeAnyFeedWritten = newConcTime()