From 1ba5e16957fdf3fd1216365ca87b5b356f84631b Mon Sep 17 00:00:00 2001 From: KOUNOIKE Yuusuke Date: Sun, 26 Mar 2023 15:07:35 +0900 Subject: [PATCH] =?UTF-8?q?backoff=E3=81=A7migration=E6=99=82=E3=81=AB?= =?UTF-8?q?=E3=83=90=E3=83=83=E3=82=AF=E3=82=AA=E3=83=95=E3=83=AA=E3=83=88?= =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 ++ go.sum | 4 ++++ main.go | 26 ++++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 53d4a95..ba65d25 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 992e970..82f73bd 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 8318096..ae1def3 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,9 @@ package main import ( "context" "database/sql" + "errors" "fmt" + "time" _ "github.com/go-sql-driver/mysql" "github.com/jinzhu/configor" @@ -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" @@ -56,6 +59,14 @@ 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)) @@ -63,7 +74,19 @@ func main() { } 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 @@ -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))