diff --git a/cron/handler.go b/cron/handler.go index a2cd3b5..5ceb09a 100644 --- a/cron/handler.go +++ b/cron/handler.go @@ -202,40 +202,26 @@ func (h *handler) updateServersData() { return } - var wg sync.WaitGroup - max := runtime.NumCPU() - count := 0 - for _, server := range servers { url, ok := urls[server.Key] if !ok { log.Printf("No one URL associated with key: %s, skipping...", server.Key) continue } - if count >= max { - wg.Wait() - count = 0 - } + sh := &updateServerDataHandler{ db: h.db.WithParam("SERVER", pg.Safe(server.Key)), baseURL: url, server: server, } - count++ - wg.Add(1) - go func(server *models.Server, sh *updateServerDataHandler) { - defer wg.Done() - log.Printf("%s: updating data", server.Key) - if err := sh.update(); err != nil { - log.Println(errors.Wrap(err, server.Key)) - return - } else { - log.Printf("%s: data updated", server.Key) - } - }(server, sh) + log.Printf("%s: updating data", server.Key) + if err := sh.update(); err != nil { + log.Println(errors.Wrap(err, server.Key)) + return + } else { + log.Printf("%s: data updated", server.Key) + } } - - wg.Wait() } func (h *handler) updateServersHistory() { diff --git a/cron/update_server_data_handler.go b/cron/update_server_data_handler.go index cf3a1c7..10a2675 100644 --- a/cron/update_server_data_handler.go +++ b/cron/update_server_data_handler.go @@ -405,7 +405,7 @@ func (h *updateServerDataHandler) isDateTheSameAsServerHistoryUpdatedAt(t time.T t.Day() == h.server.HistoryUpdatedAt.Day() } -func (h *updateServerDataHandler) calculateODDifference(od1 models.OpponentsDefeated, od2 models.OpponentsDefeated) models.OpponentsDefeated { +func (h *updateServerDataHandler) calculateODifference(od1 models.OpponentsDefeated, od2 models.OpponentsDefeated) models.OpponentsDefeated { return models.OpponentsDefeated{ RankAtt: (od1.RankAtt - od2.RankAtt) * -1, ScoreAtt: od1.ScoreAtt - od2.ScoreAtt, @@ -437,7 +437,7 @@ func (h *updateServerDataHandler) calculateDailyTribeStats(tribes []*models.Trib Rank: (tribe.Rank - historyRecord.Rank) * -1, Dominance: tribe.Dominance - historyRecord.Dominance, CreateDate: historyRecord.CreateDate, - OpponentsDefeated: h.calculateODDifference(tribe.OpponentsDefeated, historyRecord.OpponentsDefeated), + OpponentsDefeated: h.calculateODifference(tribe.OpponentsDefeated, historyRecord.OpponentsDefeated), }) } } @@ -461,7 +461,7 @@ func (h *updateServerDataHandler) calculateDailyPlayerStats(players []*models.Pl Points: player.Points - historyRecord.Points, Rank: (player.Rank - historyRecord.Rank) * -1, CreateDate: historyRecord.CreateDate, - OpponentsDefeated: h.calculateODDifference(player.OpponentsDefeated, historyRecord.OpponentsDefeated), + OpponentsDefeated: h.calculateODifference(player.OpponentsDefeated, historyRecord.OpponentsDefeated), }) } } @@ -542,8 +542,8 @@ func (h *updateServerDataHandler) update() error { for _, tribe := range tribes { ids = append(ids, tribe.ID) } - if _, err := tx.Model(&models.Tribe{}). - Where("id NOT IN (?)", pg.In(ids)). + if _, err := tx.Model(&tribes). + Where("tribe.id NOT IN (?)", pg.In(ids)). Set("exists = false"). Update(); err != nil && err != pg.ErrNoRows { return errors.Wrap(err, "cannot update not exist tribes") diff --git a/go.mod b/go.mod index 09077f6..8c82a60 100644 --- a/go.mod +++ b/go.mod @@ -9,8 +9,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.1 github.com/segmentio/encoding v0.1.14 // indirect - github.com/tribalwarshelp/shared v0.0.0-20200629123803-0cd6cb6f1e87 - github.com/tribalwarshelp/twcron v0.0.0-20200629130116-b320263c4502 // indirect + github.com/tribalwarshelp/shared v0.0.0-20200707075151-722e4a520a3c golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 // indirect google.golang.org/grpc v1.30.0 // indirect diff --git a/go.sum b/go.sum index 67881fb..d3fff75 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,8 @@ github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYm github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs= github.com/tribalwarshelp/shared v0.0.0-20200629123803-0cd6cb6f1e87 h1:TMuZUk0wW+8dXSGVJVLRFFhLONKKYJRnLBFvbb9XTrE= github.com/tribalwarshelp/shared v0.0.0-20200629123803-0cd6cb6f1e87/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y= -github.com/tribalwarshelp/twcron v0.0.0-20200629130116-b320263c4502 h1:6sIsm8dw+9qY3mdVJUSaJOfXWcrWxJrlLA8u3uTcADs= -github.com/tribalwarshelp/twcron v0.0.0-20200629130116-b320263c4502/go.mod h1:dLYqb08YNQQUu4KNVm69nf7GOjwM96DoXNmI5CLuBLE= +github.com/tribalwarshelp/shared v0.0.0-20200707075151-722e4a520a3c h1:wenSReGHPux51q5s+9Ji0TOHCt8Zx6whxsjx8Iv7aZg= +github.com/tribalwarshelp/shared v0.0.0-20200707075151-722e4a520a3c/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y= github.com/vmihailenco/bufpool v0.1.5/go.mod h1:fL9i/PRTuS7AELqAHwSU1Zf1c70xhkhGe/cD5ud9pJk= github.com/vmihailenco/bufpool v0.1.11 h1:gOq2WmBrq0i2yW5QJ16ykccQ4wH9UyEsgLm6czKAd94= github.com/vmihailenco/bufpool v0.1.11/go.mod h1:AFf/MOy3l2CFTKbxwt0mp2MwnqjNEs5H/UxrkA5jxTQ=