|
| 1 | +package application |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/mudler/LocalAI/core/config" |
| 9 | + "github.com/mudler/LocalAI/core/gallery" |
| 10 | + "github.com/mudler/LocalAI/core/services/advisorylock" |
| 11 | + "github.com/mudler/LocalAI/pkg/model" |
| 12 | + "github.com/mudler/LocalAI/pkg/system" |
| 13 | + "github.com/mudler/xlog" |
| 14 | + "gorm.io/gorm" |
| 15 | +) |
| 16 | + |
| 17 | +// UpgradeChecker periodically checks for backend upgrades and optionally |
| 18 | +// auto-upgrades them. It caches the last check results for API queries. |
| 19 | +// |
| 20 | +// In standalone mode it runs a simple ticker loop. |
| 21 | +// In distributed mode it uses a PostgreSQL advisory lock so that only one |
| 22 | +// frontend instance performs periodic checks and auto-upgrades at a time. |
| 23 | +type UpgradeChecker struct { |
| 24 | + appConfig *config.ApplicationConfig |
| 25 | + modelLoader *model.ModelLoader |
| 26 | + galleries []config.Gallery |
| 27 | + systemState *system.SystemState |
| 28 | + db *gorm.DB // non-nil in distributed mode |
| 29 | + |
| 30 | + checkInterval time.Duration |
| 31 | + stop chan struct{} |
| 32 | + done chan struct{} |
| 33 | + triggerCh chan struct{} |
| 34 | + |
| 35 | + mu sync.RWMutex |
| 36 | + lastUpgrades map[string]gallery.UpgradeInfo |
| 37 | + lastCheckTime time.Time |
| 38 | +} |
| 39 | + |
| 40 | +// NewUpgradeChecker creates a new UpgradeChecker service. |
| 41 | +// Pass db=nil for standalone mode, or a *gorm.DB for distributed mode |
| 42 | +// (uses advisory locks so only one instance runs periodic checks). |
| 43 | +func NewUpgradeChecker(appConfig *config.ApplicationConfig, ml *model.ModelLoader, db *gorm.DB) *UpgradeChecker { |
| 44 | + return &UpgradeChecker{ |
| 45 | + appConfig: appConfig, |
| 46 | + modelLoader: ml, |
| 47 | + galleries: appConfig.BackendGalleries, |
| 48 | + systemState: appConfig.SystemState, |
| 49 | + db: db, |
| 50 | + checkInterval: 6 * time.Hour, |
| 51 | + stop: make(chan struct{}), |
| 52 | + done: make(chan struct{}), |
| 53 | + triggerCh: make(chan struct{}, 1), |
| 54 | + lastUpgrades: make(map[string]gallery.UpgradeInfo), |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Run starts the upgrade checker loop. It waits 30 seconds after startup, |
| 59 | +// performs an initial check, then re-checks every 6 hours. |
| 60 | +// |
| 61 | +// In distributed mode, periodic checks are guarded by a PostgreSQL advisory |
| 62 | +// lock so only one frontend instance runs them. On-demand triggers (TriggerCheck) |
| 63 | +// and the initial check always run locally for fast API response cache warming. |
| 64 | +func (uc *UpgradeChecker) Run(ctx context.Context) { |
| 65 | + defer close(uc.done) |
| 66 | + |
| 67 | + // Initial delay: don't slow down startup |
| 68 | + select { |
| 69 | + case <-ctx.Done(): |
| 70 | + return |
| 71 | + case <-uc.stop: |
| 72 | + return |
| 73 | + case <-time.After(30 * time.Second): |
| 74 | + } |
| 75 | + |
| 76 | + // First check always runs locally (to warm the cache on this instance) |
| 77 | + uc.runCheck(ctx) |
| 78 | + |
| 79 | + if uc.db != nil { |
| 80 | + // Distributed mode: use advisory lock for periodic checks. |
| 81 | + // RunLeaderLoop ticks every checkInterval; only the lock holder executes. |
| 82 | + go advisorylock.RunLeaderLoop(ctx, uc.db, advisorylock.KeyBackendUpgradeCheck, uc.checkInterval, func() { |
| 83 | + uc.runCheck(ctx) |
| 84 | + }) |
| 85 | + |
| 86 | + // Still listen for on-demand triggers (from API / settings change) |
| 87 | + // and stop signal — these run on every instance. |
| 88 | + for { |
| 89 | + select { |
| 90 | + case <-ctx.Done(): |
| 91 | + return |
| 92 | + case <-uc.stop: |
| 93 | + return |
| 94 | + case <-uc.triggerCh: |
| 95 | + uc.runCheck(ctx) |
| 96 | + } |
| 97 | + } |
| 98 | + } else { |
| 99 | + // Standalone mode: simple ticker loop |
| 100 | + ticker := time.NewTicker(uc.checkInterval) |
| 101 | + defer ticker.Stop() |
| 102 | + |
| 103 | + for { |
| 104 | + select { |
| 105 | + case <-ctx.Done(): |
| 106 | + return |
| 107 | + case <-uc.stop: |
| 108 | + return |
| 109 | + case <-ticker.C: |
| 110 | + uc.runCheck(ctx) |
| 111 | + case <-uc.triggerCh: |
| 112 | + uc.runCheck(ctx) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Shutdown stops the upgrade checker loop. |
| 119 | +func (uc *UpgradeChecker) Shutdown() { |
| 120 | + close(uc.stop) |
| 121 | + <-uc.done |
| 122 | +} |
| 123 | + |
| 124 | +// TriggerCheck forces an immediate upgrade check on this instance. |
| 125 | +func (uc *UpgradeChecker) TriggerCheck() { |
| 126 | + select { |
| 127 | + case uc.triggerCh <- struct{}{}: |
| 128 | + default: |
| 129 | + // Already triggered, skip |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// GetAvailableUpgrades returns the cached upgrade check results. |
| 134 | +func (uc *UpgradeChecker) GetAvailableUpgrades() map[string]gallery.UpgradeInfo { |
| 135 | + uc.mu.RLock() |
| 136 | + defer uc.mu.RUnlock() |
| 137 | + |
| 138 | + // Return a copy to avoid races |
| 139 | + result := make(map[string]gallery.UpgradeInfo, len(uc.lastUpgrades)) |
| 140 | + for k, v := range uc.lastUpgrades { |
| 141 | + result[k] = v |
| 142 | + } |
| 143 | + return result |
| 144 | +} |
| 145 | + |
| 146 | +func (uc *UpgradeChecker) runCheck(ctx context.Context) { |
| 147 | + upgrades, err := gallery.CheckBackendUpgrades(ctx, uc.galleries, uc.systemState) |
| 148 | + |
| 149 | + uc.mu.Lock() |
| 150 | + uc.lastCheckTime = time.Now() |
| 151 | + if err != nil { |
| 152 | + xlog.Debug("Backend upgrade check failed", "error", err) |
| 153 | + uc.mu.Unlock() |
| 154 | + return |
| 155 | + } |
| 156 | + uc.lastUpgrades = upgrades |
| 157 | + uc.mu.Unlock() |
| 158 | + |
| 159 | + if len(upgrades) == 0 { |
| 160 | + xlog.Debug("All backends up to date") |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + // Log available upgrades |
| 165 | + for name, info := range upgrades { |
| 166 | + if info.AvailableVersion != "" { |
| 167 | + xlog.Info("Backend upgrade available", |
| 168 | + "backend", name, |
| 169 | + "installed", info.InstalledVersion, |
| 170 | + "available", info.AvailableVersion) |
| 171 | + } else { |
| 172 | + xlog.Info("Backend upgrade available (new build)", |
| 173 | + "backend", name) |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // Auto-upgrade if enabled |
| 178 | + if uc.appConfig.AutoUpgradeBackends { |
| 179 | + for name, info := range upgrades { |
| 180 | + xlog.Info("Auto-upgrading backend", "backend", name, |
| 181 | + "from", info.InstalledVersion, "to", info.AvailableVersion) |
| 182 | + if err := gallery.UpgradeBackend(ctx, uc.systemState, uc.modelLoader, |
| 183 | + uc.galleries, name, nil); err != nil { |
| 184 | + xlog.Error("Failed to auto-upgrade backend", |
| 185 | + "backend", name, "error", err) |
| 186 | + } else { |
| 187 | + xlog.Info("Backend upgraded successfully", "backend", name, |
| 188 | + "version", info.AvailableVersion) |
| 189 | + } |
| 190 | + } |
| 191 | + // Re-check to update cache after upgrades |
| 192 | + if freshUpgrades, err := gallery.CheckBackendUpgrades(ctx, uc.galleries, uc.systemState); err == nil { |
| 193 | + uc.mu.Lock() |
| 194 | + uc.lastUpgrades = freshUpgrades |
| 195 | + uc.mu.Unlock() |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments