mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
refactor(cron): migrate cron jobs to foundation scheduler
Replace custom goroutine-based scheduling in cron jobs with centralized foundation scheduler. Each cron job now exposes a RunOnce method called by the scheduler instead of managing its own ticker loop. Changes: - Remove interval/enabled config from cron job structs - Convert Start() methods to RunOnce() for all cron jobs - Add scheduler setup in main.go with configurable intervals - Update foundation dependency to v0.6.0 for scheduler support - Update tests to validate RunOnce nil-safety
This commit is contained in:
@@ -12,37 +12,21 @@ import (
|
||||
)
|
||||
|
||||
type QuotaResetter struct {
|
||||
db *gorm.DB
|
||||
sync *service.SyncService
|
||||
interval time.Duration
|
||||
db *gorm.DB
|
||||
sync *service.SyncService
|
||||
}
|
||||
|
||||
func NewQuotaResetter(db *gorm.DB, sync *service.SyncService, interval time.Duration) *QuotaResetter {
|
||||
if interval <= 0 {
|
||||
interval = 5 * time.Minute
|
||||
}
|
||||
return &QuotaResetter{db: db, sync: sync, interval: interval}
|
||||
func NewQuotaResetter(db *gorm.DB, sync *service.SyncService) *QuotaResetter {
|
||||
return &QuotaResetter{db: db, sync: sync}
|
||||
}
|
||||
|
||||
func (q *QuotaResetter) Start(ctx context.Context) {
|
||||
// RunOnce executes a single quota reset check. Called by scheduler.
|
||||
func (q *QuotaResetter) RunOnce(ctx context.Context) {
|
||||
if q == nil || q.db == nil {
|
||||
return
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
ticker := time.NewTicker(q.interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if err := q.resetOnce(ctx); err != nil {
|
||||
slog.Default().Warn("quota reset failed", "err", err)
|
||||
}
|
||||
}
|
||||
if err := q.resetOnce(ctx); err != nil {
|
||||
slog.Default().Warn("quota reset failed", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user