hot_fix_hub.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // 注意下面的 switch case 也要相应的加
  23. }
  24. // -----------------------------------------------------------------------
  25. // 找现在有多少个 hotfix 执行过了
  26. var hotFixes []models.HotFix
  27. result := dao.GetDb().Find(&hotFixes)
  28. if result == nil || result.Error != nil {
  29. return errors.New(fmt.Sprintf("hotfix query all result failed"))
  30. }
  31. // 数据库中是否有记录,记录了是否有运行都需要判断
  32. var hotFixRecord = make(map[string]models.HotFix)
  33. for _, fix := range hotFixes {
  34. hotFixRecord[fix.Key] = fix
  35. }
  36. // 交叉对比,这个执行的顺序又上面 []ifaces.IHotFix 指定
  37. for _, hotfixCase := range hotfixCases {
  38. _, bFound := hotFixRecord[hotfixCase.GetKey()]
  39. if bFound == true && hotFixRecord[hotfixCase.GetKey()].Done == true {
  40. // 如果修复过了,那么就跳过
  41. continue
  42. }
  43. // 没有找到那么就需要进行修复
  44. processResult, err := hotfixCase.Process()
  45. // 找到对应的 hotfix 方案进行 interface 数据的转换输出
  46. switch hotfixCase.GetKey() {
  47. case "001":
  48. outStruct := processResult.(OutStruct001)
  49. if err != nil {
  50. for i, file := range outStruct.ErrFiles {
  51. log.Errorln("Hotfix 001, rename failed,", i, file)
  52. }
  53. // 如果任意故障则跳出后续的修复
  54. log.Errorln("Hotfix 001 failed, break")
  55. return err
  56. } else {
  57. for i, file := range outStruct.RenamedFiles {
  58. log.Infoln("Hotfix 001, rename done,", i, file)
  59. }
  60. }
  61. break
  62. case "002":
  63. log.Infoln("Hotfix 002, process == ", processResult.(bool))
  64. break
  65. case "003":
  66. log.Infoln("Hotfix 003, process == ", processResult.(bool))
  67. break
  68. case "004":
  69. log.Infoln("Hotfix 004, process == ", processResult.(bool))
  70. break
  71. case "005":
  72. log.Infoln("Hotfix 005, process == ", processResult.(bool))
  73. break
  74. case "006":
  75. log.Infoln("Hotfix 006, process == ", processResult.(bool))
  76. break
  77. default:
  78. continue
  79. }
  80. var hotfixs []models.HotFix
  81. dao.GetDb().Where("key = ?", hotfixCase.GetKey()).Find(&hotfixs)
  82. if len(hotfixs) < 1 {
  83. // 不存在则新建
  84. // 执行成功则存入数据库中,标记完成
  85. markHotFixDone := models.HotFix{Key: hotfixCase.GetKey(), Done: true}
  86. result = dao.GetDb().Create(&markHotFixDone)
  87. if result == nil {
  88. nowError := errors.New(fmt.Sprintf("hotfix %s is done, but record failed, dao.GetDb().Create return nil", hotfixCase.GetKey()))
  89. log.Errorln(nowError)
  90. return nowError
  91. }
  92. if result.Error != nil {
  93. nowError := errors.New(fmt.Sprintf("hotfix %s is done, but record failed, %s", hotfixCase.GetKey(), result.Error))
  94. log.Errorln(nowError)
  95. return nowError
  96. }
  97. log.Infoln("Hotfix", hotfixCase.GetKey(), "is Recorded")
  98. // 找到了,目前的逻辑是成功才插入,那么查询到了,就默认是执行成功了
  99. } else {
  100. // 存在则更新
  101. hotfixs[0].Done = true
  102. dao.GetDb().Save(&hotfixs[0])
  103. }
  104. }
  105. return nil
  106. }