-
Notifications
You must be signed in to change notification settings - Fork 1
/
ready.go
54 lines (50 loc) · 1 KB
/
ready.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
package main
import (
"fmt"
"time"
"github.com/cenkalti/log"
"github.com/streadway/amqp"
)
func readyMysql(cfg DatabaseConfig, timeout time.Duration, exec string) error {
db, err := openDatabase(cfg)
if err != nil {
return err
}
defer logCloseDB(log.DefaultLogger, db)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
timeoutC := time.After(timeout)
for {
select {
case <-ticker.C:
err = db.Ping()
if err != nil {
continue
}
if exec != "" {
_, err = db.Exec(exec)
return err
}
return nil
case <-timeoutC:
return fmt.Errorf("mysql did not become ready in %s", timeout)
}
}
}
func readyRabbitmq(cfg AMQPConfig, timeout time.Duration) error {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
timeoutC := time.After(timeout)
for {
select {
case <-ticker.C:
conn, err := amqp.Dial(cfg.URL)
if err != nil {
continue
}
return conn.Close()
case <-timeoutC:
return fmt.Errorf("mysql did not become ready in %s", timeout)
}
}
}