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.

point_account.go 2.5 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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package account
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/redis/redis_client"
  6. "code.gitea.io/gitea/modules/redis/redis_key"
  7. "code.gitea.io/gitea/modules/redis/redis_lock"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/util"
  10. "encoding/json"
  11. "time"
  12. )
  13. func GetAccount(userId int64) (*models.PointAccount, error) {
  14. redisKey := redis_key.PointAccountInfo(userId)
  15. val, _ := redis_client.Get(redisKey)
  16. if val != "" {
  17. account := &models.PointAccount{}
  18. json.Unmarshal([]byte(val), account)
  19. return account, nil
  20. }
  21. account, err := models.GetAccountByUserId(userId)
  22. if err != nil {
  23. if models.IsErrRecordNotExist(err) {
  24. a, err := InitAccount(userId)
  25. if err != nil {
  26. log.Error("InitAccount error,err=%v", err)
  27. return nil, err
  28. }
  29. return a, nil
  30. }
  31. log.Error("GetAccountByUserId error,err=%v", err)
  32. return nil, err
  33. }
  34. jsonStr, _ := json.Marshal(account)
  35. redis_client.Setex(redisKey, string(jsonStr), 24*time.Hour)
  36. return account, nil
  37. }
  38. func InitAccount(userId int64) (*models.PointAccount, error) {
  39. lock := redis_lock.NewDistributeLock(redis_key.PointAccountInitLock(userId))
  40. isOk, err := lock.LockWithWait(3*time.Second, 3*time.Second)
  41. if err != nil {
  42. log.Error("PointAccountInitLock error,err=%v", err)
  43. return nil, err
  44. }
  45. if isOk {
  46. defer lock.UnLock()
  47. account, _ := models.GetAccountByUserId(userId)
  48. if account == nil {
  49. models.InsertAccount(&models.PointAccount{
  50. Balance: 0,
  51. TotalEarned: 0,
  52. TotalConsumed: 0,
  53. UserId: userId,
  54. Status: models.PointAccountNormal,
  55. Version: 0,
  56. AccountCode: util.UUID(),
  57. })
  58. return models.GetAccountByUserId(userId)
  59. }
  60. return account, nil
  61. }
  62. return nil, nil
  63. }
  64. //IsPointBalanceEnough check whether the user's point balance is bigger than task unit price
  65. func IsPointBalanceEnough(targetUserId int64, jobType string, clusterType int, resourceSpecId int, flavorCode string) bool {
  66. if !setting.CloudBrainPaySwitch {
  67. return true
  68. }
  69. t := models.Cloudbrain{
  70. Type: clusterType,
  71. JobType: jobType,
  72. ResourceSpecId: resourceSpecId,
  73. FlavorCode: flavorCode,
  74. }
  75. uniPrice := models.GetCloudbrainTaskUnitPrice(t)
  76. if uniPrice == 0 {
  77. return true
  78. }
  79. a, err := GetAccount(targetUserId)
  80. if err != nil {
  81. log.Error("IsPointBalanceEnough GetAccount error,err=%v", err)
  82. return false
  83. }
  84. return a.Balance >= uniPrice
  85. }