expire.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package service
  2. import (
  3. "context"
  4. "strconv"
  5. "time"
  6. "go-common/app/job/main/workflow/model"
  7. "go-common/library/log"
  8. )
  9. // taskExpireProc task expire.
  10. func (s *Service) taskExpireproc(c context.Context, dealType int) {
  11. var businessExpireMap = make(map[int64]int)
  12. for _, v := range s.businessAttr {
  13. if v.AssignType == 1 {
  14. continue
  15. }
  16. businessExpireMap[v.ID] = v.ExpireTime
  17. }
  18. sParams := searchParams(c, dealType, model.ListAfter, s.businessAttr)
  19. for {
  20. var expireCids []int64
  21. cLists, err := s.challByIDs(c, sParams)
  22. if err != nil {
  23. log.Error("s.challByIDs error(%v)", err)
  24. time.Sleep(time.Second * 3)
  25. continue
  26. }
  27. if len(cLists) <= 0 {
  28. time.Sleep(time.Second * 30)
  29. continue
  30. }
  31. now := time.Now()
  32. for k, cl := range cLists {
  33. if cl.DispatchState != model.QueueState {
  34. continue
  35. }
  36. dispatchTime := cl.DispatchTime.Format("2006-01-02 15:04:05")
  37. expireTime := businessExpireMap[cl.Business]
  38. m, _ := time.ParseDuration("-" + strconv.Itoa(expireTime) + "m")
  39. if now.Add(m).Format("2006-01-02 15:04:05") > dispatchTime {
  40. expireCids = append(expireCids, k)
  41. }
  42. }
  43. if len(expireCids) > 0 {
  44. log.Info("expire cids is %v", expireCids)
  45. assignAdminid := int64(0)
  46. newDispatchState := s.dispatchState(c, dealType, model.ListAfter, cLists[expireCids[0]].DispatchState)
  47. err := s.dao.UpDispatchStateAdminIDByIds(c, expireCids, newDispatchState, assignAdminid)
  48. if err != nil {
  49. log.Error("s.dao.UpDispatchStateAdminIDByIds error(%v)", err)
  50. time.Sleep(time.Second * 3)
  51. continue
  52. }
  53. }
  54. time.Sleep(time.Second * 30)
  55. }
  56. }