-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
52 lines (44 loc) · 942 Bytes
/
main.go
File metadata and controls
52 lines (44 loc) · 942 Bytes
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
package main
import (
"log"
"net/http"
"time"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
)
func main() {
var s Settings
err := envconfig.Process("", &s)
if err != nil {
err = errors.Wrap(err, "processing config")
log.Fatal(err)
}
for _ = range time.Tick(time.Second * 5) {
alive, err := serverIsAlive(&s)
if err != nil {
err = errors.Wrap(err, "checking server liveness")
log.Println("[ERROR]", err)
}
if !alive {
err = resetServer(&s)
if err != nil {
err = errors.Wrap(err, "resetting server")
log.Println("[ERROR]", err)
}
continue
}
log.Println("[INFO] server is alive")
}
}
func serverIsAlive(s *Settings) (bool, error) {
resp, err := http.Get(s.TrueNasPingUrl)
if err != nil {
err = errors.Wrap(err, "connecting to TrueNAS server")
return false, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return false, nil
}
return true, nil
}