Browse Source

#2225

add log
tags/v1.22.9.2^2
chenyifan01 3 years ago
parent
commit
ead1bbfd15
10 changed files with 43 additions and 13 deletions
  1. +1
    -0
      models/action.go
  2. +7
    -4
      modules/setting/setting.go
  3. +2
    -3
      routers/repo/cloudbrain.go
  4. +2
    -0
      routers/reward/point/account.go
  5. +2
    -0
      routers/reward/point/limit.go
  6. +6
    -0
      routers/reward/point/point.go
  7. +2
    -0
      routers/task/config.go
  8. +8
    -4
      services/reward/cloudbrain_deduct.go
  9. +10
    -2
      services/reward/period_task.go
  10. +3
    -0
      services/task/task.go

+ 1
- 0
models/action.go View File

@@ -273,6 +273,7 @@ func (a *Action) ToShow() *ActionShow {
}

func GetTaskOptType(action Action) ActionType {
//Convert all types of cloudbrain tasks action into ActionCreateCloudbrainTask
if action.IsCloudbrainAction() {
return ActionCreateCloudbrainTask
}


+ 7
- 4
modules/setting/setting.go View File

@@ -559,9 +559,11 @@ var (
WechatAuthSwitch bool

//point config
CloudBrainPaySwitch bool
CloudBrainPayDelay time.Duration
CloudBrainPayInterval time.Duration
CloudBrainPaySwitch bool
CloudBrainPayDelay time.Duration
CloudBrainPayInterval time.Duration
DeductTaskRange time.Duration
DeductTaskRangeForFirst time.Duration

//wechat auto reply config
UserNameOfWechatReply string
@@ -1409,7 +1411,8 @@ func NewContext() {
CloudBrainPaySwitch = sec.Key("CLOUDBRAIN_PAY_SWITCH").MustBool(false)
CloudBrainPayDelay = sec.Key("CLOUDBRAIN_PAY_DELAY").MustDuration(30 * time.Minute)
CloudBrainPayInterval = sec.Key("CLOUDBRAIN_PAY_INTERVAL").MustDuration(60 * time.Minute)

DeductTaskRange = sec.Key("DEDUCT_TASK_RANGE").MustDuration(30 * time.Minute)
DeductTaskRangeForFirst = sec.Key("DEDUCT_TASK_RANGE_FOR_FIRST").MustDuration(3 * time.Hour)
SetRadarMapConfig()

sec = Cfg.Section("warn_mail")


+ 2
- 3
routers/repo/cloudbrain.go View File

@@ -2,8 +2,8 @@ package repo

import (
"bufio"
"code.gitea.io/gitea/services/reward/point/account"
"code.gitea.io/gitea/modules/grampus"
"code.gitea.io/gitea/services/reward/point/account"
"encoding/json"
"errors"
"fmt"
@@ -859,8 +859,7 @@ func CloudBrainStop(ctx *context.Context) {
for {
if task.Status == string(models.JobStopped) || task.Status == string(models.JobFailed) || task.Status == string(models.JobSucceeded) {
log.Error("the job(%s) has been stopped", task.JobName, ctx.Data["msgID"])
resultCode = "-1"
errorMsg = "system error"
resultCode = task.Status
break
}



+ 2
- 0
routers/reward/point/account.go View File

@@ -3,6 +3,7 @@ package point
import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/routers/response"
"code.gitea.io/gitea/services/reward/point/account"
"net/http"
@@ -13,6 +14,7 @@ func SearchPointAccount(ctx *context.Context) {
page := ctx.QueryInt("page")
resopnse, err := account.SearchPointAccount(models.SearchPointAccountOpts{ListOptions: models.ListOptions{Page: page, PageSize: 20}, Keyword: q})
if err != nil {
log.Error("SearchPointAccount error.%v", err)
ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
return
}


+ 2
- 0
routers/reward/point/limit.go View File

@@ -3,6 +3,7 @@ package point
import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/routers/response"
"code.gitea.io/gitea/services/reward/limiter"
"net/http"
@@ -26,6 +27,7 @@ func GetSingleDailyPointLimitConfig(ctx *context.Context) {
func SetSingleDailyPointLimitConfig(ctx *context.Context, config models.LimitConfigVO) {
err := limiter.SetSingleDailyPointLimitConfig(config.LimitNum, ctx.User)
if err != nil {
log.Error("Set single daily point limit config error. %v", err)
ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
return
}


+ 6
- 0
routers/reward/point/point.go View File

@@ -4,6 +4,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/routers/response"
"code.gitea.io/gitea/services/reward"
"code.gitea.io/gitea/services/reward/point/account"
@@ -59,6 +60,7 @@ func GetPointRecordList(ctx *context.Context) {
UserName: ctx.User.Name,
})
if err != nil {
log.Error("GetPointRecordList error.%v", err)
ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
return
}
@@ -75,6 +77,7 @@ func OperatePointAccountBalance(ctx *context.Context, req models.AdminRewardOper
}
err := reward.AdminBalanceOperate(req, ctx.User)
if err != nil {
log.Error("OperatePointAccountBalance error.%v", err)
ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
return
}
@@ -92,6 +95,7 @@ func GetRulePage(ctx *context.Context) {
func GetAdminRewardList(ctx *context.Context) {
opts, err := buildAdminRewardRecordListOpts(ctx)
if err != nil {
log.Error("buildAdminRewardRecordListOpts error.%v", err)
ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
return
}
@@ -100,6 +104,7 @@ func GetAdminRewardList(ctx *context.Context) {
if username != "" {
user, err := models.GetUserByName(username)
if err != nil {
log.Error("GetUserByName error.%v", err)
if models.IsErrUserNotExist(err) {
ctx.JSON(http.StatusOK, response.ServerError("user not exist"))
} else {
@@ -113,6 +118,7 @@ func GetAdminRewardList(ctx *context.Context) {

r, err := reward.GetRewardRecordList(opts)
if err != nil {
log.Error("GetRewardRecordList error.%v", err)
ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
return
}


+ 2
- 0
routers/task/config.go View File

@@ -20,6 +20,7 @@ func GetTaskConfigList(ctx *context.Context) {
ActionType: action,
})
if err != nil {
log.Error("GetTaskConfigList error.%v", err)
ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
return
}
@@ -62,5 +63,6 @@ func BatchAddTaskConfig(ctx *context.Context, list models.BatchLimitConfigVO) {
r := make(map[string]int, 2)
r["successCount"] = successCount
r["failCount"] = failCount
log.Debug("BatchAddTaskConfig success.result=%v", r)
ctx.JSON(http.StatusOK, response.SuccessWithData(r))
}

+ 8
- 4
services/reward/cloudbrain_deduct.go View File

@@ -60,11 +60,11 @@ func StartCloudbrainPointDeductTask() {
}()
log.Debug("try to run CloudbrainPointDeductTask")
end := time.Now()
start := end.Add(-30 * time.Minute)
start := end.Add(-1 * setting.DeductTaskRange)
if firstTimeFlag {
//When it is executed for the first time, it needs to process the tasks of the last 1 hours.
//When it is executed for the first time, it needs to process the tasks of the last 3 hours.
//This is done to prevent the application from hanging for a long time
start = end.Add(-3 * time.Hour)
start = end.Add(-1 * setting.DeductTaskRangeForFirst)
firstTimeFlag = false
}
taskList, err := models.GetStartedCloudbrainTaskByUpdatedUnix(start, end)
@@ -79,11 +79,13 @@ func StartCloudbrainPointDeductTask() {
for _, t := range taskList {
DeductPoint4Cloudbrain(t, end)
}
log.Debug("CloudbrainPointDeductTask completed")
}

func DeductPoint4Cloudbrain(t models.Cloudbrain, now time.Time) error {
log.Debug("start to deduct point for cloudbrain[%d]", t.ID)
if t.StartTime == 0 {
log.Debug("cloudbrain[%d] task not start", t.ID)
return nil
}

@@ -93,6 +95,7 @@ func DeductPoint4Cloudbrain(t models.Cloudbrain, now time.Time) error {
return err
}
if task == nil {
log.Debug("cloudbrain[%d] deduct task is nil")
return nil
}
if task.Status == models.PeriodicTaskStatusFinished {
@@ -107,5 +110,6 @@ func DeductPoint4Cloudbrain(t models.Cloudbrain, now time.Time) error {
} else {
RunRewardTask(*task, now)
}
log.Debug("finished deduct point for cloudbrain[%d]", t.ID)
return nil
}

+ 10
- 2
services/reward/period_task.go View File

@@ -50,7 +50,7 @@ func StartRewardTask() {

func RunRewardTask(t models.RewardPeriodicTask, now time.Time) error {
lock := redis_lock.NewDistributeLock(redis_key.RewardTaskRunningLock(t.ID))
isOk, _ := lock.LockWithWait(3*time.Second, 3*time.Second)
isOk, _ := lock.LockWithWait(5*time.Second, 5*time.Second)
if !isOk {
log.Error("get RewardTaskRunningLock failed,t=%+v", t)
return errors.New("get RewardTaskRunningLock failed")
@@ -78,7 +78,9 @@ func RunRewardTask(t models.RewardPeriodicTask, now time.Time) error {
return errors.New("operator of reward type is not exist")
}
nextTime := timeutil.TimeStamp(int64(t.NextExecuteTime) + t.IntervalSeconds)
log.Debug("RunRewardTask n=%d", n)
for i := 1; int64(i) <= n; i++ {
log.Debug("operator.Operate i=%d n=%d", i, n)
err = operator.Operate(&models.RewardOperateContext{
SourceType: models.SourceTypeRunCloudbrainTask,
SourceId: t.OperateSerialNo,
@@ -117,7 +119,13 @@ func countExecuteTimes(t models.RewardPeriodicTask, now time.Time) (int64, timeu
return 0, 0
}
diff := now.Unix() - nextTime
n := diff/interval + 1
var n int64
if diff%interval == 0 {
n = diff / interval
} else {
n = diff/interval + 1
}

newNextTime := timeutil.TimeStamp(nextTime + n*interval)
return n, newNextTime
}

+ 3
- 0
services/task/task.go View File

@@ -17,10 +17,12 @@ func Accomplish(action models.Action) {
}()
action.OpType = models.GetTaskOptType(action)
switch action.OpType {
//only creating public repo can be rewarded
case models.ActionCreateRepo:
if action.Repo.IsPrivate {
return
}
//only creating public image can be rewarded
case models.ActionCreateImage:
if action.IsPrivate {
return
@@ -33,6 +35,7 @@ func Accomplish(action models.Action) {
}
//if wechatOpenId has been bound before,the action can not get reward
if n > 1 {
log.Debug("the wechat account has been bound before,wechatOpenId = %s", action.Content)
return
}



Loading…
Cancel
Save