781flyingdutchman / background_downloader

Flutter plugin for file downloads and uploads
Other
162 stars 76 forks source link

Task is canceled without any logs or feedback #367

Closed hedi-ghodhbane closed 2 months ago

hedi-ghodhbane commented 2 months ago

Describe the bug I'm using Background_downloader to upload pictures. I'm using uploadBatch for that. However, i started getting constant feedback from clients on iOS and Android that sometimes they try to upload one or multiple picture but nothing happens. I don't get any logs that the task is Completed/Failed and no TaskStatus update nor task progress.

To Reproduce Steps to reproduce the behavior: It's not consistent.

Expected behavior Should have a way to upload all the time or at least have a log when something unexpected happens. Maybe the os killed the task with no notification? Logs No logs

Code If possible, paste in the code snippet where you interact with the package:

Future<void> addImagesOrFilesToQueue(
      {required List<FastScanUploadRequest> requests,
      required FastScanUploadGroup group,
      required bool doOcr}) async {

    final tasks = <UploadTask>[];
    for (var request in requests) {
      tasks.add(await buildQueryParamsFromImageNetworkRequest(request, group));
    }
    final batch = await fileDownloader.uploadBatch(
      tasks,
      taskProgressCallback: (update) => updateStream.add(update),
      taskStatusCallback: (update) => updateStream.add(update),
    );
    _trackUploadEvent(batch);
  }

Additional context Sometimes clients say it appears of 15 minutes and sometimes after 6 hours.

781flyingdutchman commented 2 months ago

The batch upload feature lives on the Dart side, which means that it is meant for use when the app is in the foreground - when the app is suspended your callbacks will not be called. If the functionality you need requires true background processing, then you need to enqueue your tasks individually, monitor progress using a registered central listener, and implement resumeFromBackground. See here for more detail.

The batch functionality is useful if you have smaller files, or if you are less concerned about progress/completion tracking. For more robust implementation you need to implement this more complex approach.

hedi-ghodhbane commented 2 months ago

I don't want to upload each file individually as we have a use-case where users can upload up to 20 files at once. Enqueuing one by one would take too much time. The upload batch works fine for this case, it uploads in parallel. Can this be done otherwise?

781flyingdutchman commented 2 months ago

Understand, but all the batch upload function does is schedule each upload as a background task, and provide some monitoring. It is meant for use in the foreground. What I recommend you do is schedule each task in your batch yourself, using enqueue, which accomplishes your goal of allowing users to upload 20 files at once, in parallel. The only difference is that now

To the user, this is the same experience. For you, it's a bit more code, but avoids the issues that you raised.

hedi-ghodhbane commented 2 months ago

Thank you @781flyingdutchman for your quick & precise answers!