作者 yangfu

补充消息修改,榜单榜作业多线程

... ... @@ -248,7 +248,7 @@ from user_msg where receive_user_id=? and source_type=1 and msg_type=? and send
func GetChanceReviseMsg(uid, lastId int64, pageSize int, msgType int, v interface{}) (total int, err error) {
sql := `select a.*,b.images,b.speechs,b.videos from (
select a.*,b.source_content,b.enable_status,b.user_id chance_user_id,b.create_at,b.review_status,b.status from (
select id msg_id,message content,source_type,source_id,is_read,create_at msg_time,chance_id,receive_user_id,sender_user_id from user_msg
select id msg_id,message,source_type,source_id,is_read,create_at msg_time,chance_id,receive_user_id,sender_user_id from user_msg
where receive_user_id =? and (?=0 or id<?) and msg_type=?
)a left outer join chance b on a.chance_id = b.id
)a left outer join chance_data b on a.chance_id = b.chance_id
... ...
... ... @@ -343,6 +343,7 @@ type ChanceReviseItemOrm struct {
type MsgItemOrm struct {
MsgId int64 `orm:"column(msg_id)"`
MsgTime time.Time `orm:"column(msg_time)"` //收藏时间
Message string `orm:"column(message)"`
//评论对象类型
SourceType int `orm:"column(source_type)"`
SourceId int64 `orm:"column(source_id)"`
... ...
... ... @@ -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
... ...
... ... @@ -31,6 +31,9 @@ func MessageCenter(header *protocol.RequestHeader, request *protocol.MessageCent
if request.MsgType&protocol.MsgTypeAuditBy == 0 {
request.MsgType |= protocol.MsgTypeAuditBy
}
if request.MsgType&protocol.MsgTypeChanceRevise == 0 {
request.MsgType |= protocol.MsgTypeChanceRevise
}
}
err = models.GetUserMsgTotals(header.UserId, header.CompanyId, request.MsgType, &rsp.Totals)
if rsp.Totals == nil || len(rsp.Totals) == 0 {
... ... @@ -620,6 +623,7 @@ func MsgChanceRevise(header *protocol.RequestHeader, request *protocol.MsgChance
commItem.Chance.Provider = provider
commItem.ChanceId = chance.ChanceId
commItem.ReviewStatus = chance.ReviewStatus
commItem.Message = chance.Message
rsp.List = append(rsp.List, commItem)
}
return
... ...