mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
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:
@@ -81,26 +81,23 @@ func NewModelRegistryService(db *gorm.DB, rdb *redis.Client, cfg ModelRegistryCo
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ModelRegistryService) Start(ctx context.Context) {
|
||||
func (s *ModelRegistryService) Enabled() bool {
|
||||
return s != nil && s.cfg.Enabled
|
||||
}
|
||||
|
||||
func (s *ModelRegistryService) RefreshEvery() time.Duration {
|
||||
if s == nil || s.cfg.RefreshEvery <= 0 {
|
||||
return 30 * time.Minute
|
||||
}
|
||||
return s.cfg.RefreshEvery
|
||||
}
|
||||
|
||||
// RunOnce triggers a single model registry refresh. Called by scheduler.
|
||||
func (s *ModelRegistryService) RunOnce(ctx context.Context) {
|
||||
if !s.cfg.Enabled {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
ticker := time.NewTicker(s.cfg.RefreshEvery)
|
||||
defer ticker.Stop()
|
||||
|
||||
// Best-effort initial refresh.
|
||||
_ = s.Refresh(ctx, "")
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
_ = s.Refresh(ctx, "")
|
||||
}
|
||||
}
|
||||
}()
|
||||
_ = s.Refresh(ctx, "")
|
||||
}
|
||||
|
||||
type ModelRegistryStatus struct {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user