...
|
...
|
@@ -10,6 +10,7 @@ import ( |
|
|
"opp/models"
|
|
|
"opp/protocol"
|
|
|
"reflect"
|
|
|
"sync"
|
|
|
"sync/atomic"
|
|
|
"time"
|
|
|
)
|
...
|
...
|
@@ -63,22 +64,49 @@ func ComputeRankScore() (err error) { |
|
|
defer func() {
|
|
|
atomic.CompareAndSwapInt32(&ComputeRankScoreFlag, 1, 0)
|
|
|
}()
|
|
|
//TODO:启用多个协程执行
|
|
|
if err = computeRankScore(); err != nil {
|
|
|
log.Error(err)
|
|
|
//启用多个协程执行
|
|
|
var wg sync.WaitGroup
|
|
|
for i := 0; i < RankGoroutineNum; i++ {
|
|
|
index := i
|
|
|
work := func() {
|
|
|
if err = computeRankScore(index, wg); err != nil {
|
|
|
log.Error(err)
|
|
|
}
|
|
|
}
|
|
|
go work()
|
|
|
}
|
|
|
wg.Wait()
|
|
|
|
|
|
//更新状态
|
|
|
//结束过期的到期的榜单
|
|
|
//结束进行中 已到期的榜单
|
|
|
updateRankPeriodStatus(protocol.RankPeriodBegin, protocol.RankPeriodEnd)
|
|
|
//开始等待的榜单
|
|
|
updateRankPeriodStatus(protocol.RankPeriodWaiting, protocol.RankPeriodBegin)
|
|
|
return
|
|
|
}
|
|
|
func computeRankScore() (err error) {
|
|
|
func updateRankPeriodStatus(fromStatus int, toStatus int) {
|
|
|
var (
|
|
|
sql = `update rank_period set status=?,update_at=NOW() where status=? and UNIX_TIMESTAMP(end_time)<? `
|
|
|
)
|
|
|
if toStatus == protocol.RankPeriodBegin {
|
|
|
sql = `update rank_period set status=?,update_at=NOW() where status=? and UNIX_TIMESTAMP(begin_time)>? `
|
|
|
}
|
|
|
orm := orm.NewOrm()
|
|
|
if err := utils.ExecuteSQLWithOrmer(orm, sql, toStatus, fromStatus, time.Now().Unix()); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func computeRankScore(index int, wg sync.WaitGroup) (err error) {
|
|
|
var (
|
|
|
periods []*models.RankPeriod
|
|
|
rankRanges []*models.RankRange
|
|
|
rankRangeDatas []*models.RankRangeData
|
|
|
)
|
|
|
wg.Add(1)
|
|
|
defer wg.Done()
|
|
|
|
|
|
defer func() {
|
|
|
if p := recover(); p != nil {
|
|
|
log.Error(p)
|
...
|
...
|
@@ -91,6 +119,9 @@ func computeRankScore() (err error) { |
|
|
//2.查询对应 rank_type_id 的rank_data
|
|
|
for i := range periods {
|
|
|
period := periods[i]
|
|
|
if (period.Id % RankGoroutineNum) != index {
|
|
|
return
|
|
|
}
|
|
|
if rankRanges, err = models.GetRankRanges(period.CompanyId, period.RankTypeId); err == orm.ErrNoRows {
|
|
|
continue
|
|
|
}
|
...
|
...
|
@@ -125,6 +156,7 @@ func computeRankScore() (err error) { |
|
|
}
|
|
|
updateRankByRelationIds(relationIds, period, rankRange)
|
|
|
}
|
|
|
logProcessInfo(period)
|
|
|
}
|
|
|
|
|
|
//4.调用统计接口列表
|
...
|
...
|
@@ -134,6 +166,10 @@ func computeRankScore() (err error) { |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func logProcessInfo(period *models.RankPeriod) {
|
|
|
log.Debug(fmt.Sprintf("【排行榜统计】 周期编号:%v 赛季名称:%v (榜单类型:%v) ", period.Id, period.SeasonName, period.RankTypeId))
|
|
|
}
|
|
|
|
|
|
//更新排行榜按关联id
|
|
|
func updateRankByRelationIds(relationIds []int64, period *models.RankPeriod, rankRange *models.RankRange) {
|
|
|
var option RankOption
|
...
|
...
|
|