task.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/interface/main/laser/model"
  5. xsql "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. // QueryTaskState is query satisfied logDate from memcache if key exist,Otherwise query from db.
  9. func (s *Service) QueryUndoneTaskLogdate(c context.Context, mid int64, platform int, sourceType int) (logDate int64, err error) {
  10. // cache flag: cached to memcache only when query db.
  11. cache := true
  12. var v *model.TaskInfo
  13. if v, err = s.dao.TaskInfoCache(c, mid); err != nil {
  14. err = nil
  15. cache = false
  16. } else if v != nil {
  17. // if memcache key exist
  18. if v.Empty {
  19. return
  20. }
  21. if logDateExist(v, sourceType, platform) {
  22. logDate = v.LogDate.Time().Unix()
  23. return
  24. }
  25. return
  26. }
  27. // if memcache key not exist , then query db.
  28. v, err = s.dao.QueryUndoneTaskInfo(c, mid)
  29. if err != nil {
  30. return
  31. }
  32. if v != nil {
  33. if logDateExist(v, sourceType, platform) {
  34. logDate = v.LogDate.Time().Unix()
  35. }
  36. } else {
  37. // if not found from db, new empty Instance and set Flag:Empty true.
  38. v = &model.TaskInfo{
  39. Empty: true,
  40. }
  41. }
  42. if cache {
  43. s.addCache(func() {
  44. s.dao.AddTaskInfoCache(context.Background(), mid, v)
  45. })
  46. }
  47. return
  48. }
  49. func logDateExist(t *model.TaskInfo, sourceType int, platform int) bool {
  50. if sourceType == t.SourceType && (platform == t.Platform || platform == model.ALL_PLATFORM) {
  51. return true
  52. }
  53. return false
  54. }
  55. // UpdateTaskState is update task set state = 1 ,insert taskLog to table task_log and remove memcache.
  56. func (s *Service) UpdateTaskState(c context.Context, mid int64, build string, platform int, taskState int, reason string) (err error) {
  57. // avoid err when remove the specified-mid task on laser-admin application.
  58. taskID, err := s.dao.QueryTaskID(c, mid)
  59. if err != nil {
  60. log.Error("s.UpdateTaskState() error(%v)", err)
  61. return
  62. }
  63. if taskID == 0 {
  64. return
  65. }
  66. tx, err := s.dao.BeginTran(c)
  67. if err != nil {
  68. log.Error("s.dao.BeginTran() error(%v)", err)
  69. return
  70. }
  71. rows, err := s.dao.TxUpdateTaskState(c, tx, 1, taskID)
  72. if err != nil || rows <= 0 {
  73. tx.Rollback()
  74. log.Error("s.UpdateTaskState() error(%v)", err)
  75. return
  76. }
  77. err = s.addTaskLog(c, tx, taskID, mid, build, platform, taskState, reason)
  78. if err != nil {
  79. tx.Rollback()
  80. log.Error("s.UpdateTaskState() error(%v)", err)
  81. return
  82. }
  83. if err = tx.Commit(); err != nil {
  84. log.Error("tx.Commit() error(%v)", err)
  85. return
  86. }
  87. s.dao.RemoveTaskInfoCache(c, mid)
  88. go s.SendEmail(context.Background(), taskID)
  89. return
  90. }
  91. // addTaskLog is insert TaskLog to table task_log.
  92. func (s *Service) addTaskLog(c context.Context, tx *xsql.Tx, taskID int64, mid int64, build string, platform int, taskState int, reason string) (err error) {
  93. insertID, err := s.dao.TxAddTaskLog(c, tx, taskID, mid, build, platform, taskState, reason)
  94. if err != nil || insertID <= 0 {
  95. log.Error("s.UpdateTaskState() error(%v)", err)
  96. return
  97. }
  98. return
  99. }