hot_fix_hub.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package hot_fix
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/allanpk716/ChineseSubFinder/internal/dao"
  6. "github.com/allanpk716/ChineseSubFinder/internal/ifaces"
  7. "github.com/allanpk716/ChineseSubFinder/internal/models"
  8. "github.com/allanpk716/ChineseSubFinder/internal/types"
  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. }
  19. // -----------------------------------------------------------------------
  20. // 找现在有多少个 hotfix 执行过了
  21. var hotFixes []models.HotFix
  22. result := dao.GetDb().Find(&hotFixes)
  23. if result == nil || result.Error != nil {
  24. return errors.New(fmt.Sprintf("hotfix query all result failed"))
  25. }
  26. // 数据库中是否有记录,记录了是否有运行都需要判断
  27. var hotFixRecord = make(map[string]models.HotFix)
  28. for _, fix := range hotFixes {
  29. hotFixRecord[fix.Key] = fix
  30. }
  31. // 交叉对比,这个执行的顺序又上面 []ifaces.IHotFix 指定
  32. for _, hotfixCase := range hotfixCases {
  33. _, bFound := hotFixRecord[hotfixCase.GetKey()]
  34. if bFound == true {
  35. // 如果修复过了,那么就跳过
  36. continue
  37. }
  38. // 没有找到那么就需要进行修复
  39. processResult, err := hotfixCase.Process()
  40. // 找到对应的 hotfix 方案进行 interface 数据的转换输出
  41. switch hotfixCase.GetKey() {
  42. case "001":
  43. outStruct := processResult.(OutStruct001)
  44. if err != nil {
  45. for i, file := range outStruct.ErrFiles {
  46. log.Errorln("Hotfix 001, rename failed,", i, file)
  47. }
  48. // 如果任意故障则跳出后续的修复
  49. log.Errorln("Hotfix 001 failed, break")
  50. return err
  51. } else {
  52. for i, file := range outStruct.RenamedFiles {
  53. log.Infoln("Hotfix 001, rename done,", i, file)
  54. }
  55. }
  56. break
  57. case "002":
  58. log.Infoln("Hotfix 002, process == ", processResult.(bool))
  59. break
  60. default:
  61. continue
  62. }
  63. // 执行成功则存入数据库中,标记完成
  64. markHotFixDone := models.HotFix{Key: hotfixCase.GetKey(), Done: true}
  65. result = dao.GetDb().Create(&markHotFixDone)
  66. if result == nil {
  67. nowError := errors.New(fmt.Sprintf("hotfix %s is done, but record failed, dao.GetDb().Create return nil", hotfixCase.GetKey()))
  68. log.Errorln(nowError)
  69. return nowError
  70. }
  71. if result.Error != nil {
  72. nowError := errors.New(fmt.Sprintf("hotfix %s is done, but record failed, %s", hotfixCase.GetKey(), result.Error))
  73. log.Errorln(nowError)
  74. return nowError
  75. }
  76. log.Infoln("Hotfix", hotfixCase.GetKey(), "is Recorded")
  77. // 找到了,目前的逻辑是成功才插入,那么查询到了,就默认是执行成功了
  78. }
  79. return nil
  80. }