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.

task.go 2.3 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package task
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/services/reward"
  6. "code.gitea.io/gitea/services/reward/limiter"
  7. "fmt"
  8. )
  9. func Accomplish(action models.Action) {
  10. switch action.OpType {
  11. case models.ActionCreateRepo,
  12. models.ActionCreateImage:
  13. if action.Repo.IsPrivate {
  14. return
  15. }
  16. case models.ActionCreateDebugGPUTask,
  17. models.ActionCreateDebugNPUTask,
  18. models.ActionCreateTrainTask,
  19. models.ActionCreateInferenceTask,
  20. models.ActionCreateBenchMarkTask,
  21. models.ActionCreateGPUTrainTask:
  22. action.OpType = models.ActionCreateCloudbrainTask
  23. }
  24. go accomplish(action)
  25. }
  26. func accomplish(action models.Action) error {
  27. defer func() {
  28. if err := recover(); err != nil {
  29. combinedErr := fmt.Errorf("%s\n%s", err, log.Stack(2))
  30. log.Error("PANIC:%v", combinedErr)
  31. }
  32. }()
  33. userId := action.ActUserID
  34. taskType := fmt.Sprint(action.OpType)
  35. //get task config
  36. config, err := GetTaskConfig(taskType)
  37. if err != nil {
  38. log.Error("GetTaskConfig error,%v", err)
  39. return err
  40. }
  41. if config == nil {
  42. log.Info("task config not exist,userId=%d taskType=%s", userId, taskType)
  43. return nil
  44. }
  45. //is limited?
  46. if isLimited(userId, config, models.JustReject) {
  47. log.Info("task accomplish maximum times are reached,userId=%d taskType=%s", userId, taskType)
  48. return nil
  49. }
  50. //add log
  51. _, err = models.InsertTaskAccomplishLog(&models.TaskAccomplishLog{
  52. ConfigId: config.ID,
  53. TaskCode: config.TaskCode,
  54. UserId: userId,
  55. ActionId: action.ID,
  56. })
  57. if err != nil {
  58. return err
  59. }
  60. //reward
  61. reward.Operate(&models.RewardOperateContext{
  62. SourceType: models.SourceTypeAccomplishTask,
  63. SourceId: fmt.Sprint(action.ID),
  64. Tittle: config.Tittle,
  65. Reward: models.Reward{
  66. Amount: config.AwardAmount,
  67. Type: models.GetRewardTypeInstance(config.AwardType),
  68. },
  69. TargetUserId: userId,
  70. RequestId: fmt.Sprint(action.ID),
  71. OperateType: models.OperateTypeIncrease,
  72. RejectPolicy: models.FillUp,
  73. })
  74. return nil
  75. }
  76. func isLimited(userId int64, config *models.TaskConfig, rejectPolicy models.LimiterRejectPolicy) bool {
  77. if _, err := limiter.CheckLimit(config.TaskCode, models.LimitTypeTask, userId, 1, rejectPolicy); err != nil {
  78. return true
  79. }
  80. return false
  81. }