Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重构ci #30

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ e2e:

.PHONY: e2e_up
e2e_up:
docker compose -f .script/integration_test_compose.yml up -d
docker compose -p webook -f .script/integration_test_compose.yml up -d

.PHONY: e2e_down
e2e_down:
docker compose -f .script/integration_test_compose.yml down -v
docker compose -p webook -f .script/integration_test_compose.yml down -v

.PHONY: mock
mock:
Expand Down
7 changes: 4 additions & 3 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ wechat:
appSecretKey: abc

mysql:
dsn: "root:root@tcp(localhost:13316)/mysql"
dsn: "root:root@tcp(mysql8:13316)/mysql"

redis:
addr: "localhost:6379"
addr: "redis:6379"

web:
port: 8080
mode: debug

session:
sessionEncryptedKey: "abcd"
sessionEncryptedKey: "abcd"

36 changes: 2 additions & 34 deletions internal/test/ioc/db.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package testioc

import (
"context"
"database/sql"
"time"

"github.com/ecodeclub/ekit/retry"
"github.com/ecodeclub/webook/ioc"
"github.com/ego-component/egorm"
"github.com/gotomicro/ego/core/econf"
)
Expand All @@ -17,35 +13,7 @@ func InitDB() *egorm.Component {
return db
}
econf.Set("mysql.user", map[string]string{"dsn": "root:root@tcp(localhost:13316)/webook"})
WaitForDBSetup()
ioc.WaitForDBSetup(econf.GetStringMapString("mysql.user")["dsn"])
db = egorm.Load("mysql.user").Build()
return db
}

func WaitForDBSetup() {
sqlDB, err := sql.Open("mysql", econf.GetStringMapString("mysql.user")["dsn"])
if err != nil {
panic(err)
}
const maxInterval = 10 * time.Second
const maxRetries = 10
strategy, err := retry.NewExponentialBackoffRetryStrategy(time.Second, maxInterval, maxRetries)
if err != nil {
panic(err)
}

const timeout = 5 * time.Second
for {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
err = sqlDB.PingContext(ctx)
cancel()
if err == nil {
break
}
next, ok := strategy.Next()
if !ok {
panic("WaitForDBSetup 重试失败......")
}
time.Sleep(next)
}
}
35 changes: 35 additions & 0 deletions ioc/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,44 @@
package ioc

import (
"context"
"database/sql"
"time"

"github.com/ecodeclub/ekit/retry"
"github.com/ego-component/egorm"
"github.com/gotomicro/ego/core/econf"
)

func InitDB() *egorm.Component {
WaitForDBSetup(econf.GetString("mysql.dsn"))
return egorm.Load("mysql").Build()
}

func WaitForDBSetup(dsn string) {
sqlDB, err := sql.Open("mysql", dsn)
if err != nil {
panic(err)
}
const maxInterval = 10 * time.Second
const maxRetries = 10
strategy, err := retry.NewExponentialBackoffRetryStrategy(time.Second, maxInterval, maxRetries)
if err != nil {
panic(err)
}

const timeout = 5 * time.Second
for {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
err = sqlDB.PingContext(ctx)
cancel()
if err == nil {
break
}
next, ok := strategy.Next()
if !ok {
panic("WaitForDBSetup 重试失败......")
}
time.Sleep(next)
}
}
Loading