updater.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // 避免"关掉再打开"后因旧循环退出而导致定期更新永久失效。
  58. func (s *Supervisor) startAutoUpdate() {
  59. s.autoMu.Lock()
  60. if s.autoLoop {
  61. s.autoMu.Unlock()
  62. return
  63. }
  64. s.autoLoop = true
  65. s.autoMu.Unlock()
  66. go func() {
  67. var elapsed time.Duration
  68. ticker := time.NewTicker(time.Minute)
  69. defer ticker.Stop()
  70. for range ticker.C {
  71. s.mu.Lock()
  72. enabled := s.cfg.AutoUpdate
  73. interval := time.Duration(s.cfg.UpdateIntervalH) * time.Hour
  74. s.mu.Unlock()
  75. if !enabled {
  76. elapsed = 0 // 关闭期间不计时
  77. continue
  78. }
  79. if interval <= 0 {
  80. interval = 24 * time.Hour
  81. }
  82. elapsed += time.Minute
  83. if elapsed < interval {
  84. continue
  85. }
  86. elapsed = 0
  87. s.runScheduledUpdate()
  88. }
  89. }()
  90. }
  91. // runScheduledUpdate 查询最新版本,有更新才安装并重启。
  92. func (s *Supervisor) runScheduledUpdate() {
  93. latest, err := s.LatestVersion()
  94. if err != nil {
  95. s.log.Printf("定期更新:查询最新版本失败: %v", err)
  96. return
  97. }
  98. current := s.installedVersion()
  99. if latest == "" || current == latest {
  100. s.log.Printf("定期更新:已是最新(%s)", current)
  101. return
  102. }
  103. s.log.Printf("定期更新:发现新版本 %s(当前 %s),开始更新", latest, current)
  104. if _, err := s.Update(); err != nil {
  105. s.log.Printf("定期更新失败: %v", err)
  106. return
  107. }
  108. s.Restart()
  109. }