Ver Fonte

Time out uploads

vishnukvmd há 3 anos atrás
pai
commit
afeab0fcd6
2 ficheiros alterados com 22 adições e 23 exclusões
  1. 17 13
      lib/services/remote_sync_service.dart
  2. 5 10
      lib/utils/file_uploader.dart

+ 17 - 13
lib/services/remote_sync_service.dart

@@ -45,6 +45,8 @@ class RemoteSyncService {
 
   static const kMaximumPermissibleUploadsInThrottledMode = 4;
 
+  static const kFileUploadTimeout = Duration(minutes: 50);
+
   static final RemoteSyncService instance =
       RemoteSyncService._privateConstructor();
 
@@ -88,7 +90,7 @@ class RemoteSyncService {
     _existingSync.complete();
     _existingSync = null;
     if (hasUploadedFiles && !_shouldThrottleSync()) {
-      // Skipping a resync to ensure that files that were ignored in this 
+      // Skipping a resync to ensure that files that were ignored in this
       // session are not processed now
       sync(silently: true);
     }
@@ -201,10 +203,7 @@ class RemoteSyncService {
         break;
       }
       final file = await _db.getUploadedFileInAnyCollection(uploadedFileID);
-      final future = _uploader
-          .upload(file, file.collectionID)
-          .then((uploadedFile) => _onFileUploaded(uploadedFile));
-      futures.add(future);
+      _uploadFile(file, file.collectionID, futures);
     }
 
     for (final file in filesToBeUploaded) {
@@ -216,10 +215,7 @@ class RemoteSyncService {
       final collectionID = (await CollectionsService.instance
               .getOrCreateForPath(file.deviceFolder))
           .id;
-      final future = _uploader
-          .upload(file, collectionID)
-          .then((uploadedFile) => _onFileUploaded(uploadedFile));
-      futures.add(future);
+      _uploadFile(file, collectionID, futures);
     }
 
     for (final file in editedFiles) {
@@ -228,10 +224,7 @@ class RemoteSyncService {
         _logger.info("Skipping some edited files as we are throttling uploads");
         break;
       }
-      final future = _uploader
-          .upload(file, file.collectionID)
-          .then((uploadedFile) => _onFileUploaded(uploadedFile));
-      futures.add(future);
+      _uploadFile(file, file.collectionID, futures);
     }
 
     try {
@@ -254,6 +247,17 @@ class RemoteSyncService {
     return _completedUploads > 0;
   }
 
+  void _uploadFile(File file, int collectionID, List<Future> futures) {
+    final future = _uploader
+        .upload(file, collectionID)
+        .timeout(kFileUploadTimeout, onTimeout: () async {
+      final message = "Upload timed out for file " + file.toString();
+      _logger.severe(message);
+      throw TimeoutException(message);
+    }).then((uploadedFile) => _onFileUploaded(uploadedFile));
+    futures.add(future);
+  }
+
   Future<void> _onFileUploaded(File file) async {
     Bus.instance.fire(CollectionUpdatedEvent(file.collectionID, [file]));
     _completedUploads++;

+ 5 - 10
lib/utils/file_uploader.dart

@@ -35,7 +35,6 @@ class FileUploader {
   static const kMaximumThumbnailCompressionAttempts = 2;
   static const kMaximumUploadAttempts = 4;
   static const kBlockedUploadsPollFrequency = Duration(seconds: 2);
-  static const kFileUploadTimeout = Duration(minutes: 50);
 
   final _logger = Logger("FileUploader");
   final _dio = Network.instance.getDio();
@@ -327,15 +326,7 @@ class FileUploader {
           await _putFile(thumbnailUploadURL, encryptedThumbnailFile);
 
       final fileUploadURL = await _getUploadURL();
-      String fileObjectKey = await _putFile(fileUploadURL, encryptedFile)
-          .timeout(kFileUploadTimeout, onTimeout: () async {
-        final message = "Upload timed out for file " +
-            file.toString() +
-            " of size " +
-            (await encryptedFile.length()).toString();
-        _logger.severe(message);
-        throw TimeoutException(message);
-      });
+      String fileObjectKey = await _putFile(fileUploadURL, encryptedFile);
 
       final metadata =
           await file.getMetadataForUpload(mediaUploadData.sourceFile);
@@ -637,6 +628,10 @@ class FileUploader {
     int attempt = 1,
   }) async {
     final fileSize = contentLength ?? await file.length();
+    _logger.info("Putting object for " +
+        file.toString() +
+        " of size: " +
+        fileSize.toString());
     final startTime = DateTime.now().millisecondsSinceEpoch;
     try {
       await _dio.put(