This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
298 lines (247 loc) · 6.42 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"bytes"
"database/sql"
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
var defaultPollIntervals = []int{
//1, 2, 2, // polls after 1sec, 2secs, 2secs, then stops
//1, -2, // polls after 1sec, then infinitely every 2 seconds
//0, -5, // polls immediately, then infinitely every 5 seconds
0, -1, // polls immediately, then infinitely every 1 seconds
// 1, 2, 2, 5, 5, 10, 10, 10, 10, 10, 10, 10, 10, 10,
// 1, 2, 2,
}
var gVerbose bool
func main() {
gVerbose = getVerbosity()
if len(os.Args) == 1 {
return
}
pollIntervals := getPollIntervals()
// first check if all urls are valid
err := isAllProtocolsValid(os.Args[1:])
if err != nil {
log.Fatalf("%v\n", err)
}
// dispatch polling in order received
for _, v := range os.Args[1:] {
smallv := strings.ToLower(v)
if strings.HasPrefix(smallv, "http") {
isUp, totSecs := pollHTTP200(smallv, pollIntervals)
if !isUp {
log.Fatalf("\tNot ok. Service %s polling timedout after %d seconds.\n", smallv, totSecs)
} else {
log.Printf("\tOk. Service %s is up after about %d seconds.\n", smallv, totSecs)
}
} else if strings.HasPrefix(smallv, "postgres") {
useDbPing := false
isUp, totSecs := pollPostgres(smallv, pollIntervals, useDbPing)
if !isUp {
log.Fatalf("\tNot ok. Service %s polling timedout after %d seconds.\n", smallv, totSecs)
} else {
log.Printf("\tOk. Service %s is up after about %d seconds.\n", smallv, totSecs)
}
}
}
}
func isAllProtocolsValid(args []string) error {
for _, v := range args {
smallv := strings.ToLower(v)
if !strings.HasPrefix(smallv, "http") && !strings.HasPrefix(smallv, "postgres") {
return fmt.Errorf("Unrecognized protocol for %s. Allowed list is [http, postgres]", v)
}
}
return nil
}
func pollHTTP200(url string, pollIntervals []int) (bool, int) {
// intervals in seconds.
log.Printf("Checking if %s is up.\n", url)
isUp := false
totSecs := 0
for i := 0; i < len(pollIntervals); i++ {
pollInt := pollIntervals[i]
if pollInt < 0 {
// infinitely keep adding the same entry
pollIntervals = append(pollIntervals, pollInt)
pollInt = -pollInt
}
log.Printf("\tNext poll after %d seconds.\n", pollInt)
totSecs += pollInt
time.Sleep(time.Second * time.Duration(pollInt))
if gVerbose {
log.Printf("\tGoing to check %s\n", url)
}
ok := httpPoll(url)
if ok {
isUp = true
break
}
}
if isUp {
// log.Printf("\t%s is Up\n", url)
return true, totSecs
}
// log.Printf("\t%s timeout\n", url)
return false, totSecs
}
func httpPoll(url string) bool {
resp, err := http.Get(url)
if err != nil {
return false
}
//return isIn([]int{200, 201, 202, 203, 204, 205, 206, 207, 208, 226}, resp.StatusCode)
return isIn([]int{200}, resp.StatusCode)
}
func pollPostgres(url string, pollIntervals []int, useDbPing bool) (bool, int) {
pg_isready := "pg_isready"
// check if pg_isready exists
err := isInPath(pg_isready)
if err != nil {
log.Printf("Cannot continue, required command %s not found in path.\n", pg_isready)
return false, -1
}
// split url into host and port
host, port, username, err := splitPostgresURL(url)
if err != nil {
log.Printf("Invalid postgres url. Should be in the form: postgres://host:port")
return false, -1
}
log.Printf("Checking if %s is up.\n", url)
isUp := false
totSecs := 0
for i := 0; i < len(pollIntervals); i++ {
pollInt := pollIntervals[i]
if pollInt < 0 {
// infinitely keep adding the same entry
pollIntervals = append(pollIntervals, pollInt)
pollInt = -pollInt
}
log.Printf("\tNext poll after %d seconds.\n", pollInt)
totSecs += pollInt
time.Sleep(time.Second * time.Duration(pollInt))
if !useDbPing {
var out []byte
username = strings.TrimSpace(username)
exe := pg_isready
args := []string{
"-h", host,
"-p", port,
}
if len(username) > 0 {
args = append(args, []string{"-U", username}...)
}
if gVerbose {
log.Printf("\tGoing to execute: %s, %v\n", exe, args)
}
out, _ = captureOutput(exec.Command(exe, args...))
log.Print("\tpg_isready response: " + string(out))
if bytes.Index(out, []byte("accepting connections")) >= 0 {
isUp = true
break
}
} else {
if postgresDBPing(url) {
isUp = true
break
}
}
}
if isUp {
// log.Printf("\t%s is Up\n", url)
return true, totSecs
}
// log.Printf("\t%s timeout\n", url)
return false, totSecs
}
// url format: fmt.Sprintf("postgres://user:password@host:port/db"
func postgresDBPing(url string) bool {
db, err := sql.Open("pgx", url)
if err != nil {
return false
}
err = db.Ping()
if err != nil {
return false
}
return true
}
func captureOutput(cmd *exec.Cmd) ([]byte, error) {
// out, err := cmd.Output()
return cmd.Output()
}
func isIn(list []int, val int) bool {
for _, v := range list {
if val == v {
return true
}
}
return false
}
func isInPath(cmd string) error {
_, err := exec.LookPath(cmd)
return err
}
// returns the host, port, username of the postgres url
func splitPostgresURL(pgURL string) (string, string, string, error) {
u, err := url.Parse(pgURL)
if err != nil {
return "", "", "", err
}
if u.Scheme != "postgres" {
return "", "", "", fmt.Errorf("Expected a postgres scheme. Received: %s", u.Scheme)
}
var host, port string
h := strings.Split(u.Host, ":")
if len(h) >= 1 {
host = h[0]
}
if len(h) >= 2 {
port = h[1]
}
username := ""
if u.User != nil {
username = u.User.Username()
}
return host, port, username, nil
}
func getPollIntervals() []int {
key := "DEPENDENCY_POLL_INTERVAL"
intervalStr := strings.TrimSpace(os.Getenv(key))
if len(intervalStr) == 0 {
return defaultPollIntervals
}
interval, err := strconv.Atoi(intervalStr)
if err != nil || interval <= 0 {
log.Printf("Error: Dependency service key %s has invalid value: %s. Should be a positive integer.\n", key, intervalStr)
os.Exit(1)
}
if gVerbose {
log.Printf("Got env value for poll interval. Will poll every %d seconds.\n", interval)
}
return []int{0, -interval}
}
func getVerbosity() bool {
key := "DEPENDENCY_LOG_VERBOSE"
verbosityStr := strings.TrimSpace(os.Getenv(key))
if len(verbosityStr) == 0 {
return false
}
verbosity, err := strconv.ParseBool(verbosityStr)
if err != nil {
log.Printf("Error: Dependency service key %s has invalid value: %s. Should be true/false.\n", key, verbosityStr)
os.Exit(1)
}
if verbosity {
log.Printf("Got env value verbose: %t.\n", verbosity)
}
return verbosity
}