-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.go
More file actions
163 lines (147 loc) · 3.57 KB
/
cluster.go
File metadata and controls
163 lines (147 loc) · 3.57 KB
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
156
157
158
159
160
161
162
163
package tmppg
import (
"errors"
"fmt"
"log/slog"
"net/url"
"os"
"os/exec"
"path/filepath"
"syscall"
"time"
"github.com/authenticvision/tmppg/internal/errutil"
"github.com/authenticvision/tmppg/internal/logutil"
)
type Cluster struct {
pg *Postgres
dir string
cmd *exec.Cmd
exitErrCh chan error
}
type UninitializedError struct {
err error
}
func (e *UninitializedError) Error() string {
return fmt.Sprintf("cluster uninitialized: %s", e.err.Error())
}
func (e *UninitializedError) Unwrap() error {
return e.err
}
func OpenOrCreateCluster(pg *Postgres, dir string) (*Cluster, error) {
if c, err := OpenCluster(pg, dir); err == nil {
return c, nil
} else if !errutil.IsType[*UninitializedError](err) {
return nil, err
}
return CreateCluster(pg, dir)
}
func OpenCluster(pg *Postgres, dir string) (*Cluster, error) {
entries, err := os.ReadDir(dir)
if os.IsNotExist(err) {
return nil, &UninitializedError{err}
} else if err != nil {
return nil, fmt.Errorf("read dir cluster: %w", err)
} else if len(entries) == 0 {
return nil, &UninitializedError{errors.New("cluster dir exists but is empty")}
}
dir, err = filepath.Abs(dir)
if err != nil {
return nil, fmt.Errorf("absolute path for postgres dir: %w", err)
}
return &Cluster{
pg: pg,
dir: dir,
}, nil
}
func CreateCluster(pg *Postgres, dir string) (*Cluster, error) {
entries, err := os.ReadDir(dir)
if os.IsNotExist(err) {
err := os.MkdirAll(dir, 0755)
if err != nil {
return nil, fmt.Errorf("mkdir cluster: %w", err)
}
} else if err != nil {
return nil, fmt.Errorf("read dir cluster: %w", err)
} else if len(entries) > 0 {
return nil, fmt.Errorf("cluster dir exists and is not empty")
}
dir, err = filepath.Abs(dir)
if err != nil {
return nil, fmt.Errorf("absolute path for postgres dir: %w", err)
}
err = pg.initDB(dir)
if err != nil {
return nil, fmt.Errorf("initdb: %w", err)
}
return &Cluster{
pg: pg,
dir: dir,
}, nil
}
// pg_isready exit codes
const (
pqPingReject = 1
pqPingNoResponse = 2
pqPingNoAttempt = 3
)
func (c *Cluster) Start() error {
pgCmd, err := c.pg.start(c.dir)
if err != nil {
return fmt.Errorf("start cluster: %w", err)
}
c.cmd = pgCmd
c.exitErrCh = make(chan error, 1)
go func() {
c.exitErrCh <- pgCmd.Wait()
close(c.exitErrCh)
}()
for {
select {
case err := <-c.exitErrCh:
return fmt.Errorf("postgres exited unexpectedly: %w", err)
case <-time.After(100 * time.Millisecond):
}
cmd := c.pg.makeCmd("pg_isready", "-q", "-h", c.dir, "-d", "postgres")
err := cmd.Run()
var exitErr *exec.ExitError
if err == nil {
break
} else if errors.As(err, &exitErr) {
if exitErr.ExitCode() == pqPingReject || exitErr.ExitCode() == pqPingNoResponse {
slog.Debug("waiting for PostgreSQL to be ready")
} else {
if stopErr := c.Stop(); stopErr != nil {
slog.Error("failed to stop cluster", logutil.Err(stopErr))
}
return fmt.Errorf("pg_isready: %w", err)
}
}
}
return nil
}
func (c *Cluster) Stop() error {
if c.cmd == nil {
return fmt.Errorf("cluster is not running")
}
err := c.cmd.Process.Signal(syscall.SIGINT)
if err != nil {
return fmt.Errorf("terminate cluster: %w", err)
}
err = <-c.exitErrCh
if errutil.IsType[*exec.ExitError](err) {
return fmt.Errorf("postgres exited with error: %w", err)
} else if err != nil {
return fmt.Errorf("failed to wait for postgres: %w", err)
}
return nil
}
func (c *Cluster) URL() *url.URL {
return &url.URL{
Scheme: "postgres",
Path: "/postgres",
RawQuery: url.Values{
"host": {c.dir},
"sslmode": {"disable"},
}.Encode(),
}
}