-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
91 lines (73 loc) · 1.74 KB
/
check.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
package froach
import (
"errors"
"fmt"
"log/slog"
"os"
"os/exec"
"strings"
"time"
"github.com/KarpelesLab/fleet"
"github.com/KarpelesLab/runutil"
)
// monitor will check if cockroachdb is launched every 1 min and launch it if needed
func monitor() {
time.Sleep(5 * time.Second)
cockroachCheck()
// initialize ticker only after running once since first run can take longer (cockroach download, etc)
t := time.NewTicker(time.Minute)
for _ = range t.C {
cockroachCheck()
}
}
func cockroachCheck() {
defer func() {
if e := recover(); e != nil {
slog.Error(fmt.Sprintf("cockroach check panic: %s", e), "event", "froach:check:panic")
}
}()
if err := check(); err != nil {
slog.Error(fmt.Sprintf("cockroach run error: %s", err), "event", "froach:check:error")
}
}
func check() error {
// first, let's check if cockroach is already running
pids := runutil.PidOf("cockroach")
if len(pids) > 0 {
// already got a cockroach process out there
return nil
}
exe, err := Exe()
if err != nil {
return err
}
// let's get a list of peers
peers := getAddrs()
if len(peers) == 0 {
return errors.New("need at least 1 peer to run cockroach")
}
_, domain := fleet.Self().Name()
clusterName := domain
if pos := strings.IndexByte(clusterName, '.'); pos > 0 {
clusterName = clusterName[:pos]
} else if pos == 0 {
// domain shouldn't start with a dot
clusterName = "database"
}
// make cmdline
cmdline := makeCmdline(clusterName, peers)
// prepare command
c := exec.Command(exe, cmdline...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
slog.Debug(fmt.Sprintf("[pgdb] about to launch: %s", c), "event", "froach:run")
err = c.Start()
if err != nil {
return err
}
go func() {
c.Wait()
}()
return nil
}