register method

void register(
  1. String name,
  2. String expression,
  3. Future<void> handler(), {
  4. bool catchUp = false,
  5. int maxCatchUp = 100,
})

Adds a task.

catchUp runs each occurrence that was missed while the process was down, bounded by maxCatchUp. Off by default: a phone that was closed for four days should not send four nightly digests the moment it opens. On is right for work that writes a row per period, where a missing period is a hole in the data.

Implementation

void register(
  String name,
  String expression,
  Future<void> Function() handler, {
  bool catchUp = false,
  int maxCatchUp = 100,
}) {
  if (_tasks.containsKey(name)) {
    // Two tasks under one name means one of them silently never runs.
    throw ArgumentError.value(
      name,
      'name',
      'a task with this name is already registered',
    );
  }
  // Parsed here rather than at the first tick, so a bad expression is a
  // startup failure instead of a silence hours later in a log nobody reads.
  final DVCronSchedule schedule = DVCronSchedule.parse(expression);
  _tasks[name] = _DVTask(
    name: name,
    schedule: schedule,
    handler: handler,
    catchUp: catchUp,
    maxCatchUp: maxCatchUp,
    lastRun: _startedAt,
  );
}