refactor(scheduler): migrate outbox and model registry to scheduler-based execution

Replace internal goroutine-based timing loops with scheduler integration
for SyncOutboxService and ModelRegistryService. Both services now expose
RunOnce() methods called by the central scheduler instead of managing
their own background loops.

- Add Interval() and RunOnce() methods to SyncOutboxService
- Add RefreshEvery() and RunOnce() methods to ModelRegistryService
- Remove started flag from SyncOutboxService struct
- Move scheduler.Start() after all services are initialized
- Ensure initial model registry refresh before scheduler starts
This commit is contained in:
zenfun
2026-01-01 00:55:51 +08:00
parent 05caed37c2
commit 31914b9ab5
3 changed files with 41 additions and 45 deletions

View File

@@ -164,6 +164,7 @@ func main() {
// 4. Setup Services and Handlers
syncService := service.NewSyncService(rdb)
var outboxService *service.SyncOutboxService
if cfg.SyncOutbox.Enabled {
outboxCfg := service.SyncOutboxConfig{
Enabled: cfg.SyncOutbox.Enabled,
@@ -171,11 +172,8 @@ func main() {
BatchSize: cfg.SyncOutbox.BatchSize,
MaxRetries: cfg.SyncOutbox.MaxRetries,
}
outboxService := service.NewSyncOutboxService(db, syncService, outboxCfg, logger)
outboxService = service.NewSyncOutboxService(db, syncService, outboxCfg, logger)
syncService.SetOutbox(outboxService)
outboxCtx, cancelOutbox := context.WithCancel(context.Background())
defer cancelOutbox()
go outboxService.Start(outboxCtx)
}
logPartitioner := service.NewLogPartitioner(logDB, cfg.Log.Partitioning)
if logPartitioner.Enabled() {
@@ -202,13 +200,15 @@ func main() {
alertDetectorConfig := cron.DefaultAlertDetectorConfig()
alertDetector := cron.NewAlertDetector(db, logDB, rdb, service.NewStatsService(rdb), alertDetectorConfig, logger)
// Setup scheduler
// Setup scheduler (jobs are added incrementally, Start() called after all services initialized)
sched := scheduler.New(scheduler.WithLogger(logger), scheduler.WithSkipIfRunning())
sched.Every("quota-reset", time.Duration(cfg.Quota.ResetIntervalSeconds)*time.Second, quotaResetter.RunOnce)
sched.Every("log-cleanup", time.Hour, logCleaner.RunOnce)
sched.Every("token-refresh", time.Duration(cfg.TokenRefresh.IntervalSeconds)*time.Second, tokenRefresher.RunOnce)
sched.Every("alert-detection", time.Minute, alertDetector.RunOnce)
sched.Start()
if outboxService != nil && outboxService.Enabled() {
sched.Every("sync-outbox", outboxService.Interval(), outboxService.RunOnce)
}
defer sched.Stop()
adminService, err := service.NewAdminService()
@@ -243,7 +243,12 @@ func main() {
if err := syncService.SyncAll(db); err != nil {
logger.Warn("initial sync warning", "err", err)
}
modelRegistryService.Start(context.Background())
// Initial model registry refresh before scheduler starts
if modelRegistryService.Enabled() {
modelRegistryService.RunOnce(context.Background())
sched.Every("model-registry-refresh", modelRegistryService.RefreshEvery(), modelRegistryService.RunOnce)
}
sched.Start()
// 5. Setup Gin Router
r := gin.Default()