From d7fa77165c11c643c18c2a40cfde63303118c868 Mon Sep 17 00:00:00 2001 From: Howl Date: Wed, 11 May 2016 18:50:03 +0200 Subject: [PATCH] Lock tables in cron --- cron.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cron.go b/cron.go index fe2d8bc..5a8f2be 100644 --- a/cron.go +++ b/cron.go @@ -27,6 +27,7 @@ type config struct { CalculatePP bool FixScoreDuplicates bool `description:"might take a VERY long time"` + Lock bool `description:"Do this in prod if you have a lot of users. It will avoid memes of leaderboard update, but it will be slower (no multi mysql connections)"` LogQueries bool `description:"You don't wanna do this in prod."` Workers int `description:"The number of goroutines which should execute queries. Increasing it may make cron faster, depending on your system."` } @@ -78,6 +79,9 @@ func main() { defer db.Close() // spawn some workers + if c.Lock { + c.Workers = 1 + } fmt.Print("Spawning necessary workers...") for i := 0; i < c.Workers; i++ { chanWg.Add(1) @@ -87,6 +91,20 @@ func main() { timeAtStart := time.Now() + if c.Lock { + locks := "LOCK TABLES " + for _, i := range []string{"leaderboard_std", "leaderboard_ctb", "leaderboard_mania", "leaderboard_taiko", "users_stats", "scores", "users", "password_recovery"} { + locks += i + " WRITE, " + } + locks = locks[:len(locks)-2] + _, err = db.Exec(locks) + if err != nil { + queryError(err, locks) + return + } + db.SetMaxOpenConns(1) + } + if c.CalculateAccuracy { fmt.Print("Starting accuracy calculator worker...") wg.Add(1) @@ -133,6 +151,7 @@ func main() { color.Green("Data elaboration has been terminated.") color.Green("Execution time: %.4fs", time.Now().Sub(timeAtStart).Seconds()) color.Yellow("Waiting for workers to finish...") + op("UNLOCK TABLES") close(execOperations) chanWg.Wait() } @@ -148,7 +167,7 @@ func op(query string, params ...interface{}) { } // Operations that can be executed with a simple db.Exec, distributed across 8 workers. -var execOperations = make(chan operation, 10000) +var execOperations = make(chan operation, 100000) func worker() { for op := range execOperations { -- GitLab