781flyingdutchman / background_downloader

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

Duplicate Task when uploading an image #345

Closed Hassan6197 closed 3 months ago

Hassan6197 commented 4 months ago

Describe the bug I am experiencing an issue with the background downloader plugin where tasks with the same id appear to be logged multiple times, files are being uploaded twice. Are there any mistake by me in the code?

Expected behavior Should upload the file once only

Logs

I/flutter (5326): Processing filePath: /data/user/0/com.istudywise/cache/English_Exam_Proficiency.jpg
I/flutter (5326): Enqueuing task for file: English_Exam_Proficiency.jpg
I/BackgroundDownloader(5326): Enqueuing task with id 2196030177
I/BackgroundDownloader(5326): Enqueuing task with id 2196030177
I/TaskWorker(5326): Starting task with taskId 2196030177
I/TaskWorker(5326): Starting task with taskId 2196030177
I/flutter (5326): Status: TaskStatus.enqueued
I/flutter (5326): Status: TaskStatus.enqueued
I/flutter (5326): Status: TaskStatus.running
I/flutter (5326): Progress: 0.0%
I/flutter (5326): Status: TaskStatus.running
I/flutter (5326): Progress: 0.0%
I/flutter (5326): Progress: 15.955747730902575%
I/flutter (5326): Progress: 15.955747730902575%
I/TaskWorker(5326): Successfully uploaded taskId 2196030177 from /data/user/0/com.istudywise/app_flutter/English_Exam_Proficiency.jpg
I/flutter (5326): Progress: 100.0%
I/flutter (5326): Status: TaskStatus.complete

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

Future<void> startAddingImageProcess() async {
    setState(() {
      showLoader = true;
    });

    // Clear the uploader task queues to start fresh
    uploader.taskQueues.clear();

    // Return early if there are no documents to upload
    if (readyToUploadDocumentList.isEmpty) return;

    _totalProgress = 0;

    int totalPendingImages = readyToUploadDocumentList.length;

    // Initialize upload image response
    UploadImageResponse? uploadImageResponse;

    // Endpoint for uploading documents
    String endpoint = "users/uploadDocument";

    // Check network connectivity
    bool isNetworkAvailable = await AppCommon.checkNetworkConnectivity();
    if (isNetworkAvailable) {
      // Iterate over the list of documents to be uploaded
      print("readyToUploadDocumentList.length: ${readyToUploadDocumentList.length}");
      for (int i = 0; i < readyToUploadDocumentList.length; i++) {
        String taskId = "";
        var filePath = readyToUploadDocumentList[i]["document_url"]!;

        // Log the file path being processed
        print("Processing filePath: $filePath");

        // Create a file object from the file path
        File file = File(filePath);

        // Get the application's document directory
        Directory appDocumentDir = await getApplicationDocumentsDirectory();
        final String path = appDocumentDir.path;

        // Extract the file name from the file path
        var fileName = basename(file.path);

        // Copy the file to the application's document directory
        final File newImage = await file.copy('$path/$fileName');

        // Create an upload task
        final task = UploadTask(
          url: "$kAPP_BASE_URL$endpoint",
          filename: basename(newImage.path),
          headers: {"Authorization": "Bearer $kAPI_TOKEN"},
          fields: {
            "document_title": readyToUploadDocumentList[i]["document_title"]!,
            "user_id": widget.user.id.toString(),
            "application_id": createApplicationResponse!.data!.id.toString(),
          },
          fileField: "document_url",
          httpRequestMethod: 'POST',
          updates: Updates.statusAndProgress,
          displayName: 'Uploading ${i + 1}/$totalPendingImages',
        );

        // Log the task details
        print("Enqueuing task for file: ${task.filename}");
          uploader.enqueue(task);

        // try {
        //   // Attempt to upload the file and log the progress and status
        //   final result = await FileDownloader().upload(task,
        //       onProgress: (progress) => print('Progress: ${progress * 100}%'),
        //       onStatus: (status) => print('Status: $status')
        //   );
        //
        // } catch (e) {
        //   // Log any upload errors
        //   print("Upload error for file ${task.filename}: $e");
        // }
      }

      // Set total progress to 100% after all uploads
      _totalProgress = 100;

      // Hide the loader
      setState(() {
        showLoader = false;
      });
    }
  }
Hassan6197 commented 4 months ago

The issue is still there.

781flyingdutchman commented 4 months ago

Sorry about that. Does this happen very time, or only some times? Does it happen in Android and iOS? Are you setting the tasId specifically anywhere or is it always randomly generated by the package? Can you share Android LogCat (not Flutter) logs (or Console if it happens on iOS)? What version of the package and the OS are you running?

Hassan6197 commented 4 months ago
  1. This happening every time.
  2. Happening on both iOS and android.
  3. Task id sets by package it self.
  4. Log cat attached
  5. background_downloader: ^8.5.2 and MacOs Sonoma 14.5

Android Logcat

2024-07-18 11:12:59.124  4885-4989  flutter                 com.istudywise                       I  parameters: {user_id: 95052, institute_id: 143, program_id: 2063, application_status: 1, name: Hassan, email: 1@test.com, phone: 123456789, step_no: complete}
2024-07-18 11:12:59.124  4885-4989  flutter                 com.istudywise                       I  Api Post, url http://10.0.2.2:8000/api/v1/applications/create/95052
2024-07-18 11:12:59.302  4885-4989  flutter                 com.istudywise                       I  response.statusCode: 200
2024-07-18 11:12:59.303  4885-4989  flutter                 com.istudywise                       I  {"data":{"id":8542,"user_id":95052,"agent_id":null,"institute_id":143,"program_id":2063,"application_status":1,"notes":null,"created_at":"2024-07-18T06:13:00.000000Z","updated_at":"2024-07-18T06:13:00.000000Z","deleted_at":null},"message":"Record stored successfully!"}
2024-07-18 11:12:59.303  4885-4989  flutter                 com.istudywise                       I  api get received!
2024-07-18 11:12:59.303  4885-4989  flutter                 com.istudywise                       I  response: {"data":{"id":8542,"user_id":95052,"agent_id":null,"institute_id":143,"program_id":2063,"application_status":1,"notes":null,"created_at":"2024-07-18T06:13:00.000000Z","updated_at":"2024-07-18T06:13:00.000000Z","deleted_at":null},"message":"Record stored successfully!"}
2024-07-18 11:12:59.318  4885-4989  flutter                 com.istudywise                       I  readyToUploadDocumentList.length: 2
2024-07-18 11:12:59.318  4885-4989  flutter                 com.istudywise                       I  Processing filePath: /data/user/0/com.istudywise/cache/English_Exam_Proficiency.jpg
2024-07-18 11:12:59.327  4885-4989  flutter                 com.istudywise                       I  Enqueuing task for file: English_Exam_Proficiency.jpg
2024-07-18 11:12:59.353  4885-4885  BackgroundDownloader    com.istudywise                       I  Enqueuing task with id 255970107
2024-07-18 11:12:59.377  4885-4885  BackgroundDownloader    com.istudywise                       I  Enqueuing task with id 255970107
2024-07-18 11:12:59.391   551-564   ConnectivityService     system_server                        D  requestNetwork for uid/pid:10190/4885 activeRequest: null callbackRequest: 176 [NetworkRequest [ REQUEST id=177, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VCN_MANAGED Uid: 10190 RequestorUid: 10190 RequestorPkg: com.istudywise UnderlyingNetworks: Null] ]] callback flags: 0 order: 2147483647
2024-07-18 11:12:59.394   551-682   WifiNetworkFactory      system_server                        D  got request NetworkRequest [ REQUEST id=177, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VCN_MANAGED Uid: 10190 RequestorUid: 10190 RequestorPkg: com.istudywise UnderlyingNetworks: Null] ]
2024-07-18 11:12:59.394   551-682   UntrustedW...orkFactory system_server                        D  got request NetworkRequest [ REQUEST id=177, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VCN_MANAGED Uid: 10190 RequestorUid: 10190 RequestorPkg: com.istudywise UnderlyingNetworks: Null] ]
2024-07-18 11:12:59.394   551-682   OemPaidWif...orkFactory system_server                        D  got request NetworkRequest [ REQUEST id=177, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VCN_MANAGED Uid: 10190 RequestorUid: 10190 RequestorPkg: com.istudywise UnderlyingNetworks: Null] ]
2024-07-18 11:12:59.394   551-682   MultiInter...orkFactory system_server                        D  got request NetworkRequest [ REQUEST id=177, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VCN_MANAGED Uid: 10190 RequestorUid: 10190 RequestorPkg: com.istudywise UnderlyingNetworks: Null] ]
2024-07-18 11:12:59.401  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.enqueued
2024-07-18 11:12:59.402  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.enqueued
2024-07-18 11:12:59.408  4885-5547  TaskWorker              com.istudywise                       I  Starting task with taskId 255970107
2024-07-18 11:12:59.414  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.running
2024-07-18 11:12:59.415  4885-5545  TaskWorker              com.istudywise                       I  Starting task with taskId 255970107
2024-07-18 11:12:59.417  4885-5547  TrafficStats            com.istudywise                       D  tagSocket(102) with statsTag=0xffffffff, statsUid=-1
2024-07-18 11:12:59.417  4885-4989  flutter                 com.istudywise                       I  Progress: 0%
2024-07-18 11:12:59.419  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.running
2024-07-18 11:12:59.420  4885-4989  flutter                 com.istudywise                       I  Progress: 0%
2024-07-18 11:12:59.421  4885-5545  TrafficStats            com.istudywise                       D  tagSocket(157) with statsTag=0xffffffff, statsUid=-1
2024-07-18 11:12:59.431  4885-4989  flutter                 com.istudywise                       I  Progress: 46%
2024-07-18 11:12:59.431  4885-4989  flutter                 com.istudywise                       I  Progress: 46%
2024-07-18 11:12:59.923  4885-4990  EGL_emulation           com.istudywise                       D  app_time_stats: avg=17.22ms min=1.26ms max=49.85ms count=58
2024-07-18 11:13:00.227  4885-5552  TaskWorker              com.istudywise                       I  Successfully uploaded taskId 255970107 from /data/user/0/com.istudywise/app_flutter/English_Exam_Proficiency.jpg
2024-07-18 11:13:00.230  4885-4999  WM-WorkerWrapper        com.istudywise                       I  Worker result SUCCESS for Work [ id=0908dce5-456d-4fb2-9dfe-2dfa14517b68, tags={ com.bbflight.background_downloader.UploadTaskWorker, BackgroundDownloader, taskId=255970107, group=default } ]
2024-07-18 11:13:00.234  4885-4989  flutter                 com.istudywise                       I  Progress: 50%
2024-07-18 11:13:00.238  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.complete
2024-07-18 11:13:00.238  4885-4989  flutter                 com.istudywise                       I  Processing filePath: /data/user/0/com.istudywise/cache/Grade_12.jpg
2024-07-18 11:13:00.248  4885-4989  flutter                 com.istudywise                       I  Enqueuing task for file: Grade_12.jpg
2024-07-18 11:13:00.251  4885-4885  BackgroundDownloader    com.istudywise                       I  Enqueuing task with id 1780276150
2024-07-18 11:13:00.268  4885-4885  BackgroundDownloader    com.istudywise                       I  Enqueuing task with id 1780276150
2024-07-18 11:13:00.286  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.enqueued
2024-07-18 11:13:00.287  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.enqueued
2024-07-18 11:13:00.290  4885-5821  TaskWorker              com.istudywise                       I  Starting task with taskId 1780276150
2024-07-18 11:13:00.293  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.running
2024-07-18 11:13:00.293  4885-4989  flutter                 com.istudywise                       I  Progress: 50%
2024-07-18 11:13:00.295  4885-5821  TrafficStats            com.istudywise                       D  tagSocket(95) with statsTag=0xffffffff, statsUid=-1
2024-07-18 11:13:00.301  4885-5545  TaskWorker              com.istudywise                       I  Starting task with taskId 1780276150
2024-07-18 11:13:00.303  4885-4989  flutter                 com.istudywise                       I  Progress: 96%
2024-07-18 11:13:00.307  4885-5545  TrafficStats            com.istudywise                       D  tagSocket(154) with statsTag=0xffffffff, statsUid=-1
2024-07-18 11:13:00.315  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.running
2024-07-18 11:13:00.316  4885-4989  flutter                 com.istudywise                       I  Progress: 50%
2024-07-18 11:13:00.322  4885-4989  flutter                 com.istudywise                       I  Progress: 96%
2024-07-18 11:13:00.365   551-604   AppsFilter              system_server                        I  interaction: PackageSetting{2051b30 app.linkhome.linkhomecorporation/10191} -> PackageSetting{2e373e7 com.istudywise/10190} BLOCKED
2024-07-18 11:13:00.368   937-937   CarrierSvcBindHelper    com.android.phone                    D  onPackageModified: com.istudywise
2024-07-18 11:13:00.397   937-1187  ImsResolver             com.android.phone                    D  maybeAddedImsService, packageName: com.istudywise
2024-07-18 11:13:00.567  1469-1469  AiAiEcho                com.google.android.as                I  AppFetcherImpl onPackageChanged com.istudywise.
2024-07-18 11:13:00.569  1469-5826  AiAiEcho                com.google.android.as                I  AppIndexer Package:[com.istudywise] UserProfile:[0] Enabled:[true].
2024-07-18 11:13:00.569  1469-5826  AiAiEcho                com.google.android.as                I  AppFetcherImplV2 updateApps package:[com.istudywise], userId:[0], reason:[package is updated.].
2024-07-18 11:13:00.923  4885-4990  EGL_emulation           com.istudywise                       D  app_time_stats: avg=7.63ms min=0.92ms max=34.24ms count=60
2024-07-18 11:13:00.943  4885-5818  TaskWorker              com.istudywise                       I  Successfully uploaded taskId 255970107 from /data/user/0/com.istudywise/app_flutter/English_Exam_Proficiency.jpg
2024-07-18 11:13:00.947  4885-4944  WM-WorkerWrapper        com.istudywise                       I  Worker result SUCCESS for Work [ id=09f60735-107d-45f7-9ff4-7cbd3700a95d, tags={ com.bbflight.background_downloader.UploadTaskWorker, BackgroundDownloader, taskId=255970107, group=default } ]
2024-07-18 11:13:01.873  4885-5817  TaskWorker              com.istudywise                       I  Successfully uploaded taskId 1780276150 from /data/user/0/com.istudywise/app_flutter/Grade_12.jpg
2024-07-18 11:13:01.876  4885-4999  WM-WorkerWrapper        com.istudywise                       I  Worker result SUCCESS for Work [ id=e41e0b3f-79fc-4d5d-b065-25ed529c4d01, tags={ com.bbflight.background_downloader.UploadTaskWorker, BackgroundDownloader, taskId=1780276150, group=default } ]
2024-07-18 11:13:01.879  4885-4989  flutter                 com.istudywise                       I  Progress: 100%
2024-07-18 11:13:01.880  4885-4989  flutter                 com.istudywise                       I  Status: TaskStatus.complete
github-actions[bot] commented 3 months ago

This issue is stale because it has been open for 14 days with no activity.

Hassan6197 commented 3 months ago

@781flyingdutchman Can you help me in this?

781flyingdutchman commented 3 months ago

I'm not sure. It's clearly working for me and in my tests, so I can think of to possible issues:

  1. Somehow your enqueue call runs twice, based on how you have coded it
  2. Somehow you have two instances of the plugin running, in which case both get the enqueue 'command' and start running the upload.

You'd have to check number 1 by trying different, simpler upload code. If it still happens, then it may be number 2 and could be related to the use of other plugins - some don't "play nice" with other plugins. In that case, you may want to put the background_downloader reference in your pubspec.yaml all the way at the start of your list of dependencies, or all the way at the end, and see if that makes a difference: the order determines the initialization order of the plugins. If that doesn't work, try to debug which other plug is causing the issue.

If none of this works then I am at a loss...