hot_fix_hub.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package hot_fix
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/ifaces"
  6. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/types"
  7. "github.com/ChineseSubFinder/ChineseSubFinder/internal/dao"
  8. "github.com/ChineseSubFinder/ChineseSubFinder/internal/models"
  9. "github.com/sirupsen/logrus"
  10. )
  11. // HotFixProcess 去 DB 中查询 Hotfix 的标记,看有那些需要修复,那些已经修复完毕
  12. func HotFixProcess(log *logrus.Logger, param types.HotFixParam) error {
  13. // -----------------------------------------------------------------------
  14. // 一共有多少个 HotFix 要修复,需要固定下来
  15. hotfixCases := []ifaces.IHotFix{
  16. NewHotFix001(log, param.MovieRootDirs, param.SeriesRootDirs),
  17. NewHotFix002(log),
  18. NewHotFix003(log),
  19. NewHotFix004(log),
  20. NewHotFix005(log),
  21. NewHotFix006(log),
  22. NewHotFix007(log),
  23. // 注意下面的 switch case 也要相应的加
  24. }
  25. // -----------------------------------------------------------------------
  26. // 找现在有多少个 hotfix 执行过了
  27. var hotFixes []models.HotFix
  28. result := dao.GetDb().Find(&hotFixes)
  29. if result == nil || result.Error != nil {
  30. return errors.New(fmt.Sprintf("hotfix query all result failed"))
  31. }
  32. // 数据库中是否有记录,记录了是否有运行都需要判断
  33. var hotFixRecord = make(map[string]models.HotFix)
  34. for _, fix := range hotFixes {
  35. hotFixRecord[fix.Key] = fix
  36. }
  37. // 交叉对比,这个执行的顺序又上面 []ifaces.IHotFix 指定
  38. for _, hotfixCase := range hotfixCases {
  39. _, bFound := hotFixRecord[hotfixCase.GetKey()]
  40. if bFound == true && hotFixRecord[hotfixCase.GetKey()].Done == true {
  41. // 如果修复过了,那么就跳过
  42. continue
  43. }
  44. // 没有找到那么就需要进行修复
  45. processResult, err := hotfixCase.Process()
  46. // 找到对应的 hotfix 方案进行 interface 数据的转换输出
  47. switch hotfixCase.GetKey() {
  48. case "001":
  49. outStruct := processResult.(OutStruct001)
  50. if err != nil {
  51. for i, file := range outStruct.ErrFiles {
  52. log.Errorln("Hotfix 001, rename failed,", i, file)
  53. }
  54. // 如果任意故障则跳出后续的修复
  55. log.Errorln("Hotfix 001 failed, break")
  56. return err
  57. } else {
  58. for i, file := range outStruct.RenamedFiles {
  59. log.Infoln("Hotfix 001, rename done,", i, file)
  60. }
  61. }
  62. break
  63. case "002":
  64. log.Infoln("Hotfix 002, process == ", processResult.(bool))
  65. break
  66. case "003":
  67. log.Infoln("Hotfix 003, process == ", processResult.(bool))
  68. break
  69. case "004":
  70. log.Infoln("Hotfix 004, process == ", processResult.(bool))
  71. break
  72. case "005":
  73. log.Infoln("Hotfix 005, process == ", processResult.(bool))
  74. break
  75. case "006":
  76. log.Infoln("Hotfix 006, process == ", processResult.(bool))
  77. break
  78. case "007":
  79. log.Infoln("Hotfix 007, process == ", processResult.(bool))
  80. break
  81. default:
  82. continue
  83. }
  84. var hotfixs []models.HotFix
  85. dao.GetDb().Where("key = ?", hotfixCase.GetKey()).Find(&hotfixs)
  86. if len(hotfixs) < 1 {
  87. // 不存在则新建
  88. // 执行成功则存入数据库中,标记完成
  89. markHotFixDone := models.HotFix{Key: hotfixCase.GetKey(), Done: true}
  90. result = dao.GetDb().Create(&markHotFixDone)
  91. if result == nil {
  92. nowError := errors.New(fmt.Sprintf("hotfix %s is done, but record failed, dao.GetDb().Create return nil", hotfixCase.GetKey()))
  93. log.Errorln(nowError)
  94. return nowError
  95. }
  96. if result.Error != nil {
  97. nowError := errors.New(fmt.Sprintf("hotfix %s is done, but record failed, %s", hotfixCase.GetKey(), result.Error))
  98. log.Errorln(nowError)
  99. return nowError
  100. }
  101. log.Infoln("Hotfix", hotfixCase.GetKey(), "is Recorded")
  102. // 找到了,目前的逻辑是成功才插入,那么查询到了,就默认是执行成功了
  103. } else {
  104. // 存在则更新
  105. hotfixs[0].Done = true
  106. dao.GetDb().Save(&hotfixs[0])
  107. }
  108. }
  109. return nil
  110. }