Skip to content

Commit

Permalink
backoffでmigration時にバックオフリトライする
Browse files Browse the repository at this point in the history
  • Loading branch information
kounoike committed Mar 26, 2023
1 parent a0a9e29 commit 1ba5e16
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-sql-driver/mysql v1.7.0
github.com/jinzhu/configor v1.2.1
github.com/json-iterator/go v1.1.12
github.com/lestrrat-go/backoff/v2 v2.0.8
github.com/pkg/errors v0.9.1
github.com/r3labs/sse/v2 v2.10.0
github.com/rubenv/sql-migrate v1.4.0
Expand All @@ -21,6 +22,7 @@ require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/lestrrat-go/option v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lestrrat-go/backoff/v2 v2.0.8 h1:oNb5E5isby2kiro9AgdHLv5N5tint1AnDVVf2E2un5A=
github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBBOiM/uP402WwN8Y=
github.com/lestrrat-go/option v1.0.0 h1:WqAWL8kh8VcSoD6xjSH34/1m8yxluXQbDeKNfvFeEO4=
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"context"
"database/sql"
"errors"
"fmt"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/configor"
Expand All @@ -15,6 +17,7 @@ import (
"github.com/kounoike/dtv-discord-go/dtv"
"github.com/kounoike/dtv-discord-go/mirakc/mirakc_client"
"github.com/kounoike/dtv-discord-go/mirakc/mirakc_handler"
"github.com/lestrrat-go/backoff/v2"
migrate "github.com/rubenv/sql-migrate"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -56,14 +59,34 @@ func main() {

logger.Info("Starting dtv-discord-go", zap.String("version", version))

p := backoff.Exponential(
backoff.WithMinInterval(time.Second),
backoff.WithMaxInterval(time.Minute),
backoff.WithJitterFactor(0.05),
)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=true", config.DB.User, config.DB.Password, config.DB.Host, config.DB.Name))
if err != nil {
logger.Error("can't connect to db server", zap.Error(err))
return
}
queries := sqlcdb.New(db)
migrations := migrate.FileMigrationSource{Dir: "db/migrations"}
n, err := migrate.Exec(db, "mysql", migrations, migrate.Up)

retryMigrationFunc := func(db *sql.DB, migrations migrate.FileMigrationSource) (int, error) {
b := p.Start(ctx)
for backoff.Continue(b) {
n, err := migrate.Exec(db, "mysql", migrations, migrate.Up)
if err == nil {
return n, nil
}
}
return 0, errors.New("failed to migration")
}

n, err := retryMigrationFunc(db, migrations)
if err != nil {
logger.Error("db migration error", zap.Error(err))
return
Expand Down Expand Up @@ -94,7 +117,6 @@ func main() {

discordHandler := discord_handler.NewDiscordHandler(usecase, discordClient.Session(), logger)

ctx := context.Background()
err = usecase.InitializeServiceChannels(ctx)
if err != nil {
logger.Error("can't create program infomation channel", zap.Error(err))
Expand Down

0 comments on commit 1ba5e16

Please sign in to comment.