perf(scheduler): harden jobs for scalable execution

This commit is contained in:
2026-05-20 12:19:27 +08:00
parent 4b09a34748
commit 5fb9d0b0dd
23 changed files with 887 additions and 147 deletions
@@ -117,12 +117,27 @@ func (w *MonitoringReceivedInspectionWorker) runOnce(parent context.Context) {
}
}
func (w *MonitoringReceivedInspectionWorker) RunOnce(parent context.Context) (map[string]any, error) {
func (w *MonitoringReceivedInspectionWorker) RunOnce(parent context.Context, run JobRunContext) (map[string]any, error) {
if w == nil || w.monitoringPool == nil {
return map[string]any{"skipped": true, "reason": "worker_not_ready"}, nil
}
startedAt := time.Now()
ctx, cancel := context.WithTimeout(parent, w.timeout)
timeout := w.timeout
if run.Job != nil && run.Job.TimeoutSeconds > 0 {
timeout = time.Duration(run.Job.TimeoutSeconds) * time.Second
}
limit := w.limit
if run.Job != nil && run.Job.BatchSize != nil && *run.Job.BatchSize > 0 {
limit = *run.Job.BatchSize
}
if value := AsInt(run.Config, "batch_size", 0); value > 0 && (run.Job == nil || run.Job.BatchSize == nil) {
limit = value
}
threshold := w.threshold
if value := AsDuration(run.Config, "threshold", 0); value > 0 {
threshold = value
}
ctx, cancel := context.WithTimeout(parent, timeout)
defer cancel()
tx, err := w.monitoringPool.Begin(ctx)
@@ -132,7 +147,7 @@ func (w *MonitoringReceivedInspectionWorker) RunOnce(parent context.Context) (ma
defer tx.Rollback(ctx)
now := time.Now().UTC()
items, err := tenantapp.MarkMonitoringStaleReceivedTasks(ctx, tx, now, w.threshold, w.limit)
items, err := tenantapp.MarkMonitoringStaleReceivedTasks(ctx, tx, now, threshold, limit)
if err != nil {
return map[string]any{"stage": "inspect"}, err
}
@@ -141,7 +156,8 @@ func (w *MonitoringReceivedInspectionWorker) RunOnce(parent context.Context) (ma
}
return map[string]any{
"overdue_task_count": len(items),
"threshold_seconds": int64(w.threshold.Seconds()),
"threshold_seconds": int64(threshold.Seconds()),
"duration_ms": time.Since(startedAt).Milliseconds(),
"batch_size": limit,
}, nil
}