Skip to content

Commit

Permalink
Improvement to rescheduleKilledTask that also reschedules tasks that …
Browse files Browse the repository at this point in the history
…are waiting to retry in teh database, but not in the downloader (Dart side)
  • Loading branch information
781flyingdutchman committed Jan 20, 2025
1 parent 138091d commit 1fc51aa
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/src/file_downloader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,12 @@ interface class FileDownloader {
///
/// This function retrieves all tasks from the database that are in enqueued
/// or running states and compares these tasks with the
/// list of tasks currently present in the native task queue.
/// list of tasks currently present in the native task queue, and all tasks
/// that are in waitingToRetry state that are not actually waiting to retry
/// in the downloader
///
/// For each task found only in the database, the function:
/// For each task found only in the database (or waitingToRetry yet not
/// actually waiting), the function:
/// 1. Deletes the corresponding record from the database.
/// 2. Enqueues the task back into the native task queue.
///
Expand All @@ -592,15 +595,24 @@ interface class FileDownloader {
_downloader.isTrackingTasks,
'rescheduleKilledTasks should only be called if you are tracking tasks. '
'Did you call trackTasks or trackTasksInGroup?');
final databaseTasks = (await database.allRecords())
final missingTasks = <Task>{};
final databaseTasks = await database.allRecords();
// find missing enqueued/running tasks
final enqueuedOrRunningDatabaseTasks = databaseTasks
.where((record) => const [
TaskStatus.enqueued,
TaskStatus.running,
].contains(record.status))
.map((record) => record.task)
.toSet();
final nativeTasks = Set<Task>.from(await FileDownloader().allTasks());
final missingTasks = databaseTasks.difference(nativeTasks);
missingTasks.addAll(enqueuedOrRunningDatabaseTasks.difference(nativeTasks));
// find missing tasks waiting to retry
missingTasks.addAll(databaseTasks
.where((record) =>
record.status == TaskStatus.waitingToRetry &&
!_downloader.tasksWaitingToRetry.contains(record.task))
.map((record) => record.task));
final successfullyEnqueued = <Task>[];
final failedToEnqueue = <Task>[];
for (final task in missingTasks) {
Expand Down

0 comments on commit 1fc51aa

Please sign in to comment.