enqueue<T> method

Future<T> enqueue<T>(
  1. ScrapingTask<T> task
)

Enqueues a task

Implementation

Future<T> enqueue<T>(ScrapingTask<T> task) {
  // Add the task to the list
  _taskList.add(task);

  // Sort the list by priority and creation time
  _taskList.sort((a, b) {
    // First compare by priority (higher priority first)
    final priorityComparison = a.priority.index.compareTo(b.priority.index);
    if (priorityComparison != 0) {
      return priorityComparison;
    }

    // Then compare by creation time (older first)
    return a.createdAt.compareTo(b.createdAt);
  });

  // Add the task to the domain map
  _tasksByDomain.putIfAbsent(task.domain, () => {}).add(task);

  // Update the task status
  task.status = TaskStatus.queued;
  logger?.info('Task ${task.id} enqueued (priority: ${task.priority})');

  // Process the next task if the scheduler is running
  if (_isRunning) {
    _processNextTask();
  }

  return task.future;
}