You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

period_task.go 3.8 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package reward
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/redis/redis_key"
  6. "code.gitea.io/gitea/modules/redis/redis_lock"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. "code.gitea.io/gitea/routers/repo"
  9. "errors"
  10. "fmt"
  11. "time"
  12. )
  13. func NewRewardPeriodicTask(operateRecordId string, opts *models.StartPeriodicTaskOpts) error {
  14. task := &models.RewardPeriodicTask{}
  15. task.DelaySeconds = int64(opts.Delay.Seconds())
  16. task.IntervalSeconds = int64(opts.Interval.Seconds())
  17. task.Amount = opts.UnitAmount
  18. task.OperateSerialNo = operateRecordId
  19. task.Status = models.PeriodicTaskStatusRunning
  20. task.NextExecuteTime = timeutil.TimeStamp(opts.StartTime.Add(opts.Delay).Unix())
  21. _, err := models.InsertPeriodicTask(task)
  22. return err
  23. }
  24. func StartRewardTask() {
  25. defer func() {
  26. if err := recover(); err != nil {
  27. combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
  28. log.Error("PANIC:%v", combinedErr)
  29. }
  30. }()
  31. log.Debug("try to run reward tasks")
  32. now := time.Now()
  33. taskList, err := models.GetRunningRewardTask(now)
  34. if err != nil {
  35. log.Error("GetRunningRewardTask error. %v", err)
  36. return
  37. }
  38. if taskList == nil || len(taskList) == 0 {
  39. log.Debug("No GetRunningRewardTask need handled")
  40. return
  41. }
  42. for _, t := range taskList {
  43. RunRewardTask(t, now)
  44. }
  45. }
  46. func RunRewardTask(t models.RewardPeriodicTask, now time.Time) error {
  47. lock := redis_lock.NewDistributeLock(redis_key.RewardTaskRunningLock(t.ID))
  48. isOk, _ := lock.LockWithWait(3*time.Second, 3*time.Second)
  49. if !isOk {
  50. log.Error("get RewardTaskRunningLock failed,t=%+v", t)
  51. return errors.New("get RewardTaskRunningLock failed")
  52. }
  53. defer lock.UnLock()
  54. record, err := models.GetPointOperateRecordBySerialNo(t.OperateSerialNo)
  55. if err != nil {
  56. log.Error("RunRewardTask. GetPointOperateRecordBySerialNo error. %v", err)
  57. return errors.New("GetPointOperateRecordBySerialNo error")
  58. }
  59. if record.Status != models.OperateStatusOperating {
  60. log.Info("RunRewardTask. operate record is finished,record=%+v", record)
  61. return nil
  62. }
  63. n, _ := countExecuteTimes(t, now)
  64. if n == 0 {
  65. log.Info("countExecuteTimes result is 0")
  66. return nil
  67. }
  68. //get operator
  69. operator := GetOperator(models.GetRewardTypeInstance(record.RewardType))
  70. if operator == nil {
  71. log.Error("RunRewardTask. operator of reward type is not exist")
  72. return errors.New("operator of reward type is not exist")
  73. }
  74. nextTime := t.NextExecuteTime
  75. for i := 1; int64(i) <= n; i++ {
  76. err = operator.Operate(&models.RewardOperateContext{
  77. SourceType: models.SourceTypeRunCloudbrainTask,
  78. SourceId: t.OperateSerialNo,
  79. Reward: models.Reward{
  80. Amount: t.Amount,
  81. Type: models.GetRewardTypeInstance(record.RewardType),
  82. },
  83. TargetUserId: record.UserId,
  84. OperateType: models.GetRewardOperateTypeInstance(record.OperateType),
  85. })
  86. if err != nil {
  87. log.Error("RunRewardTask.operator operate error.%v", err)
  88. if models.IsErrInsufficientPointsBalance(err) {
  89. task, err := models.GetCloudbrainByID(record.SourceId)
  90. if err != nil {
  91. log.Error("RunRewardTask GetCloudbrainByID error. %v", err)
  92. return err
  93. }
  94. repo.StopJobs([]*models.Cloudbrain{task})
  95. models.StopPeriodicTask(task.ID, t.OperateSerialNo, time.Now())
  96. return nil
  97. }
  98. return nil
  99. }
  100. models.IncrRewardTaskSuccessCount(t, 1, nextTime)
  101. nextTime = timeutil.TimeStamp(int64(nextTime) + t.IntervalSeconds)
  102. }
  103. return nil
  104. }
  105. func countExecuteTimes(t models.RewardPeriodicTask, now time.Time) (int64, timeutil.TimeStamp) {
  106. interval := t.IntervalSeconds
  107. nextTime := int64(t.NextExecuteTime)
  108. if nextTime > now.Unix() {
  109. return 0, 0
  110. }
  111. diff := now.Unix() - nextTime
  112. n := diff/interval + 1
  113. newNextTime := timeutil.TimeStamp(nextTime + n*interval)
  114. return n, newNextTime
  115. }