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

@@ -36,11 +36,10 @@ type SyncOutboxEntry struct {
// SyncOutboxService retries failed sync operations stored in the database.
type SyncOutboxService struct {
db *gorm.DB
sync *SyncService
cfg SyncOutboxConfig
logger *slog.Logger
started bool
db *gorm.DB
sync *SyncService
cfg SyncOutboxConfig
logger *slog.Logger
}
func NewSyncOutboxService(db *gorm.DB, sync *SyncService, cfg SyncOutboxConfig, logger *slog.Logger) *SyncOutboxService {
@@ -63,6 +62,13 @@ func (s *SyncOutboxService) Enabled() bool {
return s != nil && s.cfg.Enabled
}
func (s *SyncOutboxService) Interval() time.Duration {
if s == nil || s.cfg.Interval <= 0 {
return 5 * time.Second
}
return s.cfg.Interval
}
// Enqueue persists a failed sync operation for retry.
func (s *SyncOutboxService) Enqueue(entry SyncOutboxEntry) error {
if s == nil || !s.cfg.Enabled {
@@ -101,24 +107,12 @@ func (s *SyncOutboxService) Enqueue(entry SyncOutboxEntry) error {
return nil
}
// Start begins the background retry loop.
func (s *SyncOutboxService) Start(ctx context.Context) {
if s == nil || !s.cfg.Enabled || s.started {
// RunOnce processes one batch of pending outbox items. Called by scheduler.
func (s *SyncOutboxService) RunOnce(ctx context.Context) {
if s == nil || !s.cfg.Enabled {
return
}
s.started = true
ticker := time.NewTicker(s.cfg.Interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.processBatch(ctx)
}
}
s.processBatch(ctx)
}
func (s *SyncOutboxService) processBatch(ctx context.Context) {