updater.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "os/exec"
  7. "strings"
  8. "time"
  9. )
  10. const allowScripts = "@deepseek-ai/dsh-subprocess-local,koffi,node-pty,@google/genai,protobufjs"
  11. // Update 执行全局更新:npm install -g @deepseek-ai/dsh@latest --allow-scripts=...
  12. func (s *Supervisor) Update() (string, error) {
  13. s.mu.Lock()
  14. if s.updating {
  15. s.mu.Unlock()
  16. return "", errors.New("更新已在进行中")
  17. }
  18. s.updating = true
  19. s.mu.Unlock()
  20. defer func() {
  21. s.mu.Lock()
  22. s.updating = false
  23. s.mu.Unlock()
  24. }()
  25. exe, args := npmCommandFor("install", "-g", dshPackage+"@latest", "--allow-scripts="+allowScripts)
  26. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
  27. defer cancel()
  28. cmd := exec.CommandContext(ctx, exe, args...)
  29. hideWindow(cmd)
  30. s.log.Printf("执行更新: %s %s", exe, strings.Join(args, " "))
  31. out, err := cmd.CombinedOutput()
  32. text := strings.TrimSpace(string(out))
  33. if text != "" {
  34. s.log.Printf("更新输出: %s", text)
  35. }
  36. if err != nil {
  37. return text, fmt.Errorf("更新失败: %w", err)
  38. }
  39. s.invalidateProbe()
  40. return text, nil
  41. }
  42. // LatestVersion 查询 npm 上最新版本。
  43. func (s *Supervisor) LatestVersion() (string, error) {
  44. exe, args := npmCommandFor("view", dshPackage, "version")
  45. ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
  46. defer cancel()
  47. cmd := exec.CommandContext(ctx, exe, args...)
  48. hideWindow(cmd)
  49. out, err := cmd.Output()
  50. if err != nil {
  51. return "", err
  52. }
  53. return strings.TrimSpace(string(out)), nil
  54. }
  55. // startAutoUpdate 定期后台更新(单例):每分钟检查开关,到达间隔才查询/更新。
  56. // 重复调用安全:已有循环时直接返回,避免多个循环并存。
  57. func (s *Supervisor) startAutoUpdate() {
  58. s.autoMu.Lock()
  59. if s.autoLoop {
  60. s.autoMu.Unlock()
  61. return
  62. }
  63. s.autoLoop = true
  64. s.autoMu.Unlock()
  65. go func() {
  66. defer func() {
  67. s.autoMu.Lock()
  68. s.autoLoop = false
  69. s.autoMu.Unlock()
  70. }()
  71. var elapsed time.Duration
  72. ticker := time.NewTicker(time.Minute)
  73. defer ticker.Stop()
  74. for range ticker.C {
  75. s.mu.Lock()
  76. enabled := s.cfg.AutoUpdate
  77. interval := time.Duration(s.cfg.UpdateIntervalH) * time.Hour
  78. s.mu.Unlock()
  79. if !enabled {
  80. s.log.Printf("定期更新:已关闭")
  81. return
  82. }
  83. if interval <= 0 {
  84. interval = 24 * time.Hour
  85. }
  86. elapsed += time.Minute
  87. if elapsed < interval {
  88. continue
  89. }
  90. elapsed = 0
  91. s.runScheduledUpdate()
  92. }
  93. }()
  94. }
  95. // runScheduledUpdate 查询最新版本,有更新才安装并重启。
  96. func (s *Supervisor) runScheduledUpdate() {
  97. latest, err := s.LatestVersion()
  98. if err != nil {
  99. s.log.Printf("定期更新:查询最新版本失败: %v", err)
  100. return
  101. }
  102. current := s.installedVersion()
  103. if latest == "" || current == latest {
  104. s.log.Printf("定期更新:已是最新(%s)", current)
  105. return
  106. }
  107. s.log.Printf("定期更新:发现新版本 %s(当前 %s),开始更新", latest, current)
  108. if _, err := s.Update(); err != nil {
  109. s.log.Printf("定期更新失败: %v", err)
  110. return
  111. }
  112. s.Restart()
  113. }