jitteredNextCronRunMs function

int? jitteredNextCronRunMs(
  1. String cron,
  2. int fromMs,
  3. String taskId, [
  4. CronJitterConfig cfg = kDefaultCronJitterConfig,
])

Same as nextCronRunMs, plus a deterministic per-task delay to avoid a thundering herd when many sessions schedule the same cron string.

Only used for recurring tasks.

Implementation

int? jitteredNextCronRunMs(
  String cron,
  int fromMs,
  String taskId, [
  CronJitterConfig cfg = kDefaultCronJitterConfig,
]) {
  final t1 = nextCronRunMs(cron, fromMs);
  if (t1 == null) return null;
  final t2 = nextCronRunMs(cron, t1);
  if (t2 == null) return t1;
  final jitter = min(
    _jitterFrac(taskId) * cfg.recurringFrac * (t2 - t1),
    cfg.recurringCapMs.toDouble(),
  );
  return t1 + jitter.round();
}