2020-03-01 16:06:34 +00:00
|
|
|
package wait
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2020-09-06 12:31:23 +00:00
|
|
|
|
|
|
|
"github.com/docker/go-connections/nat"
|
2020-03-01 16:06:34 +00:00
|
|
|
)
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
var _ Strategy = (*waitForSql)(nil)
|
|
|
|
var _ StrategyTimeout = (*waitForSql)(nil)
|
|
|
|
|
|
|
|
const defaultForSqlQuery = "SELECT 1"
|
|
|
|
|
|
|
|
// ForSQL constructs a new waitForSql strategy for the given driver
|
|
|
|
func ForSQL(port nat.Port, driver string, url func(host string, port nat.Port) string) *waitForSql {
|
2020-03-01 16:06:34 +00:00
|
|
|
return &waitForSql{
|
2020-09-06 12:31:23 +00:00
|
|
|
Port: port,
|
|
|
|
URL: url,
|
|
|
|
Driver: driver,
|
|
|
|
startupTimeout: defaultStartupTimeout(),
|
2021-01-17 18:00:46 +00:00
|
|
|
PollInterval: defaultPollInterval(),
|
2023-08-21 21:04:28 +00:00
|
|
|
query: defaultForSqlQuery,
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type waitForSql struct {
|
2023-08-21 21:04:28 +00:00
|
|
|
timeout *time.Duration
|
|
|
|
|
|
|
|
URL func(host string, port nat.Port) string
|
2020-03-01 16:06:34 +00:00
|
|
|
Driver string
|
|
|
|
Port nat.Port
|
|
|
|
startupTimeout time.Duration
|
2021-01-17 18:00:46 +00:00
|
|
|
PollInterval time.Duration
|
2023-08-21 21:04:28 +00:00
|
|
|
query string
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
// WithStartupTimeout can be used to change the default startup timeout
|
|
|
|
func (w *waitForSql) WithStartupTimeout(timeout time.Duration) *waitForSql {
|
|
|
|
w.timeout = &timeout
|
2020-03-01 16:06:34 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
// WithPollInterval can be used to override the default polling interval of 100 milliseconds
|
2021-01-17 18:00:46 +00:00
|
|
|
func (w *waitForSql) WithPollInterval(pollInterval time.Duration) *waitForSql {
|
|
|
|
w.PollInterval = pollInterval
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
// WithQuery can be used to override the default query used in the strategy.
|
|
|
|
func (w *waitForSql) WithQuery(query string) *waitForSql {
|
|
|
|
w.query = query
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *waitForSql) Timeout() *time.Duration {
|
|
|
|
return w.timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitUntilReady repeatedly tries to run "SELECT 1" or user defined query on the given port using sql and driver.
|
|
|
|
//
|
|
|
|
// If it doesn't succeed until the timeout value which defaults to 60 seconds, it will return an error.
|
2020-03-01 16:06:34 +00:00
|
|
|
func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget) (err error) {
|
2023-08-21 21:04:28 +00:00
|
|
|
timeout := defaultStartupTimeout()
|
|
|
|
if w.timeout != nil {
|
|
|
|
timeout = *w.timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
2020-03-01 16:06:34 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
host, err := target.Host(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:00:46 +00:00
|
|
|
ticker := time.NewTicker(w.PollInterval)
|
2020-03-01 16:06:34 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
var port nat.Port
|
|
|
|
port, err = target.MappedPort(ctx, w.Port)
|
|
|
|
|
|
|
|
for port == "" {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return fmt.Errorf("%s:%w", ctx.Err(), err)
|
|
|
|
case <-ticker.C:
|
|
|
|
if err := checkTarget(ctx, target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
port, err = target.MappedPort(ctx, w.Port)
|
|
|
|
}
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 21:04:28 +00:00
|
|
|
db, err := sql.Open(w.Driver, w.URL(host, port))
|
2020-03-01 16:06:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("sql.Open: %v", err)
|
|
|
|
}
|
2021-01-17 18:00:46 +00:00
|
|
|
defer db.Close()
|
2020-03-01 16:06:34 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case <-ticker.C:
|
2023-08-21 21:04:28 +00:00
|
|
|
if err := checkTarget(ctx, target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := db.ExecContext(ctx, w.query); err != nil {
|
2020-03-01 16:06:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|