perf(scheduler): harden jobs for scalable execution
This commit is contained in:
@@ -20,6 +20,12 @@ type GenerationTaskStateCheckWorker struct {
|
||||
cache sharedcache.Cache
|
||||
}
|
||||
|
||||
type GenerationTaskStateCheckRunOptions struct {
|
||||
Timeout time.Duration
|
||||
BatchSize int
|
||||
Lookback time.Duration
|
||||
}
|
||||
|
||||
func NewGenerationTaskStateCheckWorker(pool *pgxpool.Pool, logger *zap.Logger, cfg config.GenerationConfig) *GenerationTaskStateCheckWorker {
|
||||
return &GenerationTaskStateCheckWorker{
|
||||
pool: pool,
|
||||
@@ -92,13 +98,30 @@ func (w *GenerationTaskStateCheckWorker) runOnce(parent context.Context) {
|
||||
}, zap.Int("anomaly_count", count))
|
||||
}
|
||||
|
||||
func (w *GenerationTaskStateCheckWorker) RunOnce(parent context.Context) (map[string]any, error) {
|
||||
func (w *GenerationTaskStateCheckWorker) RunOnce(parent context.Context, options ...GenerationTaskStateCheckRunOptions) (map[string]any, error) {
|
||||
if w == nil || w.pool == nil {
|
||||
return map[string]any{"skipped": true, "reason": "worker_not_ready"}, nil
|
||||
}
|
||||
cfg := w.runtimeConfig()
|
||||
if len(options) > 0 {
|
||||
if options[0].BatchSize > 0 {
|
||||
cfg.BatchSize = options[0].BatchSize
|
||||
}
|
||||
if options[0].Timeout > 0 {
|
||||
cfg.Timeout = options[0].Timeout
|
||||
}
|
||||
if options[0].Lookback > 0 {
|
||||
cfg.Lookback = options[0].Lookback
|
||||
}
|
||||
}
|
||||
startedAt := time.Now()
|
||||
count, err := CheckGenerationTaskStateConsistency(parent, w.pool, w.logger, w.cache, cfg)
|
||||
checkCtx := parent
|
||||
var cancel context.CancelFunc = func() {}
|
||||
if cfg.Timeout > 0 {
|
||||
checkCtx, cancel = context.WithTimeout(parent, cfg.Timeout)
|
||||
}
|
||||
defer cancel()
|
||||
count, err := CheckGenerationTaskStateConsistency(checkCtx, w.pool, w.logger, w.cache, cfg)
|
||||
duration := time.Since(startedAt)
|
||||
stats := map[string]any{
|
||||
"anomaly_count": count,
|
||||
|
||||
Reference in New Issue
Block a user