cancel method

void cancel(
  1. String taskId
)

Cancels a task

Implementation

void cancel(String taskId) {
  // Find the task in the list
  final taskIndex = _taskList.indexWhere((t) => t.id == taskId);
  ScrapingTask? task;

  if (taskIndex >= 0) {
    // Task is in the list
    task = _taskList[taskIndex];
  } else {
    // Task might be executing
    task = _executingTasks.firstWhere(
      (t) => t.id == taskId,
      orElse: () => throw Exception('Task not found: $taskId'),
    );
  }

  // Cancel the task
  task.cancel();

  // Remove the task from the list if it's still there
  if (taskIndex >= 0) {
    _taskList.removeAt(taskIndex);
  }

  // Remove the task from the executing tasks if it's there
  _executingTasks.remove(task);

  // Remove the task from the domain map
  _tasksByDomain[task.domain]?.remove(task);
  if (_tasksByDomain[task.domain]?.isEmpty ?? false) {
    _tasksByDomain.remove(task.domain);
  }

  logger?.info('Task $taskId cancelled');
}