enqueueBufferedTasks method

Future<void> enqueueBufferedTasks()

Enqueue all tasks buffered with buffer. This method is called by the DeploymentExecutor when a deployment is finished. It will sort the tasks based on their trigger time and then schedule the first N tasks where N is the number of available notification slots.

Implementation

Future<void> enqueueBufferedTasks() async {
  _userTaskBuffer.sort((a, b) => a.triggerTime.compareTo(b.triggerTime));
  var remainingNotifications =
      NotificationController.pendingNotificationLimit -
          (await SmartPhoneClientManager()
                  .notificationController
                  ?.pendingNotificationRequestsCount ??
              0);

  var numberOfTasksToEnqueue =
      min(remainingNotifications, _userTaskBuffer.length);

  // Being mindful of the OS limitations, only schedule however many
  // tasks as remaining notification slots
  List<UserTaskBufferItem> toEnqueue =
      _userTaskBuffer.sublist(0, numberOfTasksToEnqueue);

  debug('$runtimeType - Enqueuing ${toEnqueue.length} tasks.');

  for (var item in toEnqueue) {
    item.taskControl.hasBeenScheduledUntil = item.triggerTime;
    await enqueue(
      item.taskExecutor,
      triggerTime: item.triggerTime,
      sendNotification: item.sendNotification,
    );
  }

  // Discard the tasks that we couldn't queue, they will be re-queued later.
  _userTaskBuffer.clear();

  // Persist the tasks that were just enqueued
  SmartPhoneClientManager().save();
}