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.

reward_periodic_task.go 3.5 kB

3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package models
  2. import (
  3. "code.gitea.io/gitea/modules/timeutil"
  4. "time"
  5. )
  6. type PeriodicTaskStatus int
  7. const (
  8. PeriodicTaskStatusRunning = iota + 1 // 1
  9. PeriodicTaskStatusFinished // 2
  10. )
  11. type PeriodType string
  12. const (
  13. PeriodType30MinutesFree1HourCost PeriodType = "30MF1HC"
  14. )
  15. func (r PeriodType) Name() string {
  16. switch r {
  17. case PeriodType30MinutesFree1HourCost:
  18. return "30MF1HC"
  19. default:
  20. return ""
  21. }
  22. }
  23. type RewardPeriodicTask struct {
  24. ID int64 `xorm:"pk autoincr"`
  25. OperateSerialNo string `xorm:"INDEX NOT NULL"`
  26. DelaySeconds int64
  27. IntervalSeconds int64
  28. Amount int64 `xorm:"NOT NULL"`
  29. NextExecuteTime timeutil.TimeStamp `xorm:"INDEX NOT NULL"`
  30. SuccessCount int `xorm:"NOT NULL default 0"`
  31. Status int `xorm:"NOT NULL"`
  32. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  33. FinishedUnix timeutil.TimeStamp `xorm:"INDEX"`
  34. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  35. }
  36. type StartPeriodicTaskOpts struct {
  37. SourceType SourceType
  38. SourceId string
  39. Remark string
  40. Title string
  41. TargetUserId int64
  42. RequestId string
  43. OperateType RewardOperateType
  44. Delay time.Duration
  45. Interval time.Duration
  46. UnitAmount int
  47. RewardType RewardType
  48. StartTime time.Time
  49. }
  50. func InsertPeriodicTask(tl *RewardPeriodicTask) (int64, error) {
  51. return x.Insert(tl)
  52. }
  53. func GetRunningRewardTask(now time.Time) ([]RewardPeriodicTask, error) {
  54. r := make([]RewardPeriodicTask, 0)
  55. err := x.Where("next_execute_time <= ? and status = ?", now.Unix(), PeriodicTaskStatusRunning).Find(&r)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return r, err
  60. }
  61. func IncrRewardTaskSuccessCount(t RewardPeriodicTask, count int64, nextTime timeutil.TimeStamp) error {
  62. sess := x.NewSession()
  63. defer sess.Close()
  64. _, err := sess.Exec("update reward_periodic_task set success_count = success_count + ? , next_execute_time = ?, updated_unix = ? where id = ?", count, nextTime, timeutil.TimeStampNow(), t.ID)
  65. if err != nil {
  66. sess.Rollback()
  67. return err
  68. }
  69. _, err = sess.Exec("update reward_operate_record set amount = amount + ? ,updated_unix = ? ,last_operate_unix = ? where serial_no = ?", t.Amount, timeutil.TimeStampNow(), timeutil.TimeStampNow(), t.OperateSerialNo)
  70. if err != nil {
  71. sess.Rollback()
  72. return err
  73. }
  74. sess.Commit()
  75. return nil
  76. }
  77. func GetPeriodicTaskBySourceIdAndType(sourceType SourceType, sourceId string, operateType RewardOperateType) (*RewardPeriodicTask, error) {
  78. r := RewardPeriodicTask{}
  79. _, err := x.SQL("select rpt.* from reward_periodic_task rpt "+
  80. "inner join reward_operate_record ror on rpt.operate_serial_no = ror.serial_no"+
  81. " where ror.source_type = ? and ror.source_id = ? and ror.operate_type = ? ", sourceType.Name(), sourceId, operateType.Name()).Get(&r)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return &r, nil
  86. }
  87. func StopPeriodicTask(taskId int64, operateSerialNo string, stopTime time.Time) error {
  88. sess := x.NewSession()
  89. defer sess.Close()
  90. _, err := sess.Where("id = ? and status = ?", taskId, PeriodicTaskStatusRunning).Update(&RewardPeriodicTask{Status: PeriodicTaskStatusFinished, FinishedUnix: timeutil.TimeStamp(stopTime.Unix())})
  91. if err != nil {
  92. sess.Rollback()
  93. return err
  94. }
  95. _, err = sess.Where("serial_no = ? and status = ?", operateSerialNo, OperateStatusOperating).Update(&RewardOperateRecord{Status: OperateStatusSucceeded})
  96. if err != nil {
  97. sess.Rollback()
  98. return err
  99. }
  100. sess.Commit()
  101. return nil
  102. }