onStart method
Callback when this executor is started. Returns true if successfully started, false otherwise.
Implementation
@override
Future<bool> onStart() async {
debug(
'$runtimeType - ${taskControl.taskName} hasBeenScheduledUntil: ${taskControl.hasBeenScheduledUntil}');
final from = taskControl.hasBeenScheduledUntil ?? DateTime.now();
final to = DateTime.now().add(const Duration(days: 15)); // 15 days ahead
// get all the instances where the task should be scheduled in the given range
final schedule = triggerExecutor.getSchedule(from, to);
if (schedule.isEmpty) {
// Stop since the schedule is empty and there is not more to schedule.
stop();
} else {
info(
'$runtimeType Buffering ${schedule.length} app tasks ($schedule) for task ${taskExecutor.task.name}');
Iterator<DateTime> it = schedule.iterator;
DateTime current = DateTime.now();
while (it.moveNext()) {
current = it.current;
AppTaskController().buffer(
taskExecutor,
taskControl,
triggerTime: current,
);
}
// Now stop since the schedule has all been enqueued.
stop();
// .. but start again when the scheduled time has passed.
// This in the case where the app keeps running in the background
var duration = current.millisecondsSinceEpoch -
DateTime.now().millisecondsSinceEpoch;
Timer(Duration(milliseconds: duration), () => start());
}
return true;
}