hot_fix_hub.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/pkg/log_helper"
  9. "github.com/allanpk716/ChineseSubFinder/internal/types"
  10. )
  11. // HotFixProcess 去 DB 中查询 Hotfix 的标记,看有那些需要修复,那些已经修复完毕
  12. func HotFixProcess(param types.HotFixParam) error {
  13. // -----------------------------------------------------------------------
  14. // 一共有多少个 HotFix 要修复,需要固定下来
  15. hotfixCases := []ifaces.IHotFix{
  16. NewHotFix001(param.MovieRootDir, param.SeriesRootDir),
  17. }
  18. // -----------------------------------------------------------------------
  19. // 找现在有多少个 hotfix 执行过了
  20. var hotFixes []models.HotFix
  21. result := dao.GetDb().Find(&hotFixes)
  22. if result == nil || result.Error != nil {
  23. return errors.New(fmt.Sprintf("hotfix query all result failed"))
  24. }
  25. // 数据库中是否有记录,记录了是否有运行都需要判断
  26. var hotFixRecord = make(map[string]models.HotFix)
  27. for _, fix := range hotFixes {
  28. hotFixRecord[fix.Key] = fix
  29. }
  30. // 交叉对比,这个执行的顺序又上面 []ifaces.IHotFix 指定
  31. for _, hotfixCase := range hotfixCases {
  32. _, bFound := hotFixRecord[hotfixCase.GetKey()]
  33. if bFound == false {
  34. // 没有找到那么就需要进行修复
  35. log_helper.GetLogger().Infoln("hotfix", hotfixCase.GetKey(), "start...")
  36. processResult, err := hotfixCase.Process()
  37. // 找到对应的 hotfix 方案进行 interface 数据的转换输出
  38. switch hotfixCase.GetKey() {
  39. case "001":
  40. outStruct := processResult.(OutStruct001)
  41. if err != nil {
  42. for i, file := range outStruct.ErrFiles {
  43. log_helper.GetLogger().Errorln("hotfix 001, rename failed,", i, file)
  44. }
  45. // 如果任意故障则跳出后续的修复
  46. log_helper.GetLogger().Errorln("hotfix 001 failed, break")
  47. return err
  48. }
  49. break
  50. default:
  51. continue
  52. }
  53. // 执行成功则存入数据库中,标记完成
  54. markHotFixDone := models.HotFix{Key: hotfixCase.GetKey(), Done: true}
  55. result = dao.GetDb().Create(&markHotFixDone)
  56. if result == nil {
  57. return errors.New(fmt.Sprintf("hotfix %s is done, but record failed, dao.GetDb().Create return nil", hotfixCase.GetKey()))
  58. }
  59. if result.Error != nil {
  60. return errors.New(fmt.Sprintf("hotfix %s is done, but record failed, %s", hotfixCase.GetKey(), result.Error))
  61. }
  62. log_helper.GetLogger().Infoln("hotfix", hotfixCase.GetKey(), "is done")
  63. }
  64. // 找到了,目前的逻辑是成功才插入,那么查询到了,就默认是执行成功了
  65. }
  66. return nil
  67. }