enqueueBufferedTasks method

Future<void> enqueueBufferedTasks()

Enqueue all tasks buffered with buffer. This method is called by the SmartphoneDeploymentExecutor 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 =
      NotificationManager.pendingNotificationLimit -
      (await SmartPhoneClientManager()
          .notificationManager
          .pendingNotificationRequestsCount);

  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();
}