Convert backup logic to use promises.

pull/1/head
Matthew Chen 7 years ago
parent 6d8fa78023
commit af477d3bf3

@ -347,15 +347,14 @@ NS_ASSUME_NONNULL_BEGIN
[self updateProgressWithDescription:nil progress:nil]; [self updateProgressWithDescription:nil progress:nil];
__weak OWSBackupExportJob *weakSelf = self;
[[self.backup checkCloudKitAccess] [[self.backup checkCloudKitAccess]
.thenInBackground(^{ .thenInBackground(^{
[weakSelf start]; [self start];
}) })
.catch(^(NSError *error) { .catch(^(NSError *error) {
[weakSelf failWithErrorDescription: [self failWithErrorDescription:
NSLocalizedString(@"BACKUP_EXPORT_ERROR_COULD_NOT_EXPORT", NSLocalizedString(@"BACKUP_EXPORT_ERROR_COULD_NOT_EXPORT",
@"Error indicating the backup export could not export the user's data.")]; @"Error indicating the backup export could not export the user's data.")];
}) retainUntilComplete]; }) retainUntilComplete];
} }
@ -365,7 +364,6 @@ NS_ASSUME_NONNULL_BEGIN
@"Indicates that the backup export is being configured.") @"Indicates that the backup export is being configured.")
progress:nil]; progress:nil];
__weak OWSBackupExportJob *weakSelf = self;
[[self configureExport] [[self configureExport]
.thenInBackground(^{ .thenInBackground(^{
return [self fetchAllRecords]; return [self fetchAllRecords];
@ -389,9 +387,9 @@ NS_ASSUME_NONNULL_BEGIN
.catch(^(NSError *error) { .catch(^(NSError *error) {
OWSFailDebug(@"Backup export failed with error: %@.", error); OWSFailDebug(@"Backup export failed with error: %@.", error);
[weakSelf failWithErrorDescription: [self failWithErrorDescription:
NSLocalizedString(@"BACKUP_EXPORT_ERROR_COULD_NOT_EXPORT", NSLocalizedString(@"BACKUP_EXPORT_ERROR_COULD_NOT_EXPORT",
@"Error indicating the backup export could not export the user's data.")]; @"Error indicating the backup export could not export the user's data.")];
}) retainUntilComplete]; }) retainUntilComplete];
} }
@ -441,18 +439,13 @@ NS_ASSUME_NONNULL_BEGIN
return [AnyPromise promiseWithValue:OWSBackupErrorWithDescription(@"Backup export no longer active.")]; return [AnyPromise promiseWithValue:OWSBackupErrorWithDescription(@"Backup export no longer active.")];
} }
__weak OWSBackupExportJob *weakSelf = self;
AnyPromise *promise = [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) { AnyPromise *promise = [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[OWSBackupAPI fetchAllRecordNamesWithRecipientId:self.recipientId [OWSBackupAPI fetchAllRecordNamesWithRecipientId:self.recipientId
success:^(NSArray<NSString *> *recordNames) { success:^(NSArray<NSString *> *recordNames) {
OWSBackupExportJob *strongSelf = weakSelf; if (self.isComplete) {
if (!strongSelf) {
return resolve(OWSBackupErrorWithDescription(@"Backup export no longer active."));
}
if (strongSelf.isComplete) {
return resolve(OWSBackupErrorWithDescription(@"Backup export no longer active.")); return resolve(OWSBackupErrorWithDescription(@"Backup export no longer active."));
} }
strongSelf.lastValidRecordNames = [NSSet setWithArray:recordNames]; self.lastValidRecordNames = [NSSet setWithArray:recordNames];
resolve(@(1)); resolve(@(1));
} }
failure:^(NSError *error) { failure:^(NSError *error) {
@ -473,14 +466,8 @@ NS_ASSUME_NONNULL_BEGIN
return [AnyPromise promiseWithValue:OWSBackupErrorWithDescription(@"Backup export no longer active.")]; return [AnyPromise promiseWithValue:OWSBackupErrorWithDescription(@"Backup export no longer active.")];
} }
__weak OWSBackupExportJob *weakSelf = self;
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) { return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
OWSBackupExportJob *strongSelf = weakSelf; if (![self performExportDatabase]) {
if (!strongSelf) {
return resolve(OWSBackupErrorWithDescription(@"Backup export no longer active."));
}
if (![strongSelf performExportDatabase]) {
NSError *error = OWSBackupErrorWithDescription(@"Backup export failed."); NSError *error = OWSBackupErrorWithDescription(@"Backup export failed.");
return resolve(error); return resolve(error);
} }
@ -757,8 +744,6 @@ NS_ASSUME_NONNULL_BEGIN
// This method returns YES IFF "work was done and there might be more work to do". // This method returns YES IFF "work was done and there might be more work to do".
- (AnyPromise *)saveDatabaseFilesToCloud - (AnyPromise *)saveDatabaseFilesToCloud
{ {
__weak OWSBackupExportJob *weakSelf = self;
AnyPromise *promise = [AnyPromise promiseWithValue:@(1)]; AnyPromise *promise = [AnyPromise promiseWithValue:@(1)];
// We need to preserve ordering of database shards. // We need to preserve ordering of database shards.
@ -774,12 +759,8 @@ NS_ASSUME_NONNULL_BEGIN
[OWSBackupAPI saveEphemeralDatabaseFileToCloudWithRecipientId:self.recipientId [OWSBackupAPI saveEphemeralDatabaseFileToCloudWithRecipientId:self.recipientId
fileUrl:[NSURL fileURLWithPath:item.encryptedItem.filePath] fileUrl:[NSURL fileURLWithPath:item.encryptedItem.filePath]
success:^(NSString *recordName) { success:^(NSString *recordName) {
OWSBackupExportJob *strongSelf = weakSelf;
if (!strongSelf) {
return resolve(OWSBackupErrorWithDescription(@"Backup export no longer active."));
}
item.recordName = recordName; item.recordName = recordName;
[strongSelf.savedDatabaseItems addObject:item]; [self.savedDatabaseItems addObject:item];
resolve(@(1)); resolve(@(1));
} }
failure:^(NSError *error) { failure:^(NSError *error) {
@ -815,8 +796,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)saveAttachmentFileToCloud:(OWSAttachmentExport *)attachmentExport resolve:(PMKResolver)resolve - (void)saveAttachmentFileToCloud:(OWSAttachmentExport *)attachmentExport resolve:(PMKResolver)resolve
{ {
__weak OWSBackupExportJob *weakSelf = self;
if (self.lastValidRecordNames) { if (self.lastValidRecordNames) {
// Wherever possible, we do incremental backups and re-use fragments of the last // Wherever possible, we do incremental backups and re-use fragments of the last
// backup and/or restore. // backup and/or restore.
@ -881,11 +860,6 @@ NS_ASSUME_NONNULL_BEGIN
return [NSURL fileURLWithPath:attachmentExport.encryptedItem.filePath]; return [NSURL fileURLWithPath:attachmentExport.encryptedItem.filePath];
} }
success:^(NSString *recordName) { success:^(NSString *recordName) {
OWSBackupExportJob *strongSelf = weakSelf;
if (!strongSelf) {
return resolve(OWSBackupErrorWithDescription(@"Backup export no longer active."));
}
if (![attachmentExport cleanUp]) { if (![attachmentExport cleanUp]) {
OWSLogError(@"couldn't clean up attachment export."); OWSLogError(@"couldn't clean up attachment export.");
// Attachment files are non-critical so any error uploading them is recoverable. // Attachment files are non-critical so any error uploading them is recoverable.
@ -895,7 +869,7 @@ NS_ASSUME_NONNULL_BEGIN
exportItem.encryptedItem = attachmentExport.encryptedItem; exportItem.encryptedItem = attachmentExport.encryptedItem;
exportItem.recordName = recordName; exportItem.recordName = recordName;
exportItem.attachmentExport = attachmentExport; exportItem.attachmentExport = attachmentExport;
[strongSelf.savedAttachmentItems addObject:exportItem]; [self.savedAttachmentItems addObject:exportItem];
// Immediately save the record metadata to facilitate export resume. // Immediately save the record metadata to facilitate export resume.
OWSBackupFragment *backupFragment = [OWSBackupFragment new]; OWSBackupFragment *backupFragment = [OWSBackupFragment new];
@ -935,18 +909,12 @@ NS_ASSUME_NONNULL_BEGIN
OWSBackupExportItem *exportItem = [OWSBackupExportItem new]; OWSBackupExportItem *exportItem = [OWSBackupExportItem new];
exportItem.encryptedItem = encryptedItem; exportItem.encryptedItem = encryptedItem;
__weak OWSBackupExportJob *weakSelf = self;
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) { return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[OWSBackupAPI upsertManifestFileToCloudWithRecipientId:self.recipientId [OWSBackupAPI upsertManifestFileToCloudWithRecipientId:self.recipientId
fileUrl:[NSURL fileURLWithPath:encryptedItem.filePath] fileUrl:[NSURL fileURLWithPath:encryptedItem.filePath]
success:^(NSString *recordName) { success:^(NSString *recordName) {
OWSBackupExportJob *strongSelf = weakSelf;
if (!strongSelf) {
return resolve(OWSBackupErrorWithDescription(@"Backup export no longer active."));
}
exportItem.recordName = recordName; exportItem.recordName = recordName;
strongSelf.manifestItem = exportItem; self.manifestItem = exportItem;
// All files have been saved to the cloud. // All files have been saved to the cloud.
resolve(@(1)); resolve(@(1));
@ -1082,14 +1050,9 @@ NS_ASSUME_NONNULL_BEGIN
return [AnyPromise promiseWithValue:OWSBackupErrorWithDescription(@"Backup export no longer active.")]; return [AnyPromise promiseWithValue:OWSBackupErrorWithDescription(@"Backup export no longer active.")];
} }
__weak OWSBackupExportJob *weakSelf = self;
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) { return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[OWSBackupAPI fetchAllRecordNamesWithRecipientId:self.recipientId [OWSBackupAPI fetchAllRecordNamesWithRecipientId:self.recipientId
success:^(NSArray<NSString *> *recordNames) { success:^(NSArray<NSString *> *recordNames) {
OWSBackupExportJob *strongSelf = weakSelf;
if (!strongSelf) {
return resolve(OWSBackupErrorWithDescription(@"Backup export no longer active."));
}
NSMutableSet<NSString *> *obsoleteRecordNames = [NSMutableSet new]; NSMutableSet<NSString *> *obsoleteRecordNames = [NSMutableSet new];
[obsoleteRecordNames addObjectsFromArray:recordNames]; [obsoleteRecordNames addObjectsFromArray:recordNames];
[obsoleteRecordNames minusSet:activeRecordNames]; [obsoleteRecordNames minusSet:activeRecordNames];
@ -1099,12 +1062,12 @@ NS_ASSUME_NONNULL_BEGIN
activeRecordNames.count, activeRecordNames.count,
obsoleteRecordNames.count); obsoleteRecordNames.count);
[weakSelf deleteRecordsFromCloud:[obsoleteRecordNames.allObjects mutableCopy] [self deleteRecordsFromCloud:[obsoleteRecordNames.allObjects mutableCopy]
deletedCount:0 deletedCount:0
completion:^(NSError *_Nullable error) { completion:^(NSError *_Nullable error) {
// Cloud cleanup is non-critical so any error is recoverable. // Cloud cleanup is non-critical so any error is recoverable.
resolve(@(1)); resolve(@(1));
}]; }];
} }
failure:^(NSError *error) { failure:^(NSError *error) {
// Cloud cleanup is non-critical so any error is recoverable. // Cloud cleanup is non-critical so any error is recoverable.
@ -1145,18 +1108,17 @@ NS_ASSUME_NONNULL_BEGIN
[batchRecordNames addObject:recordName]; [batchRecordNames addObject:recordName];
} }
__weak OWSBackupExportJob *weakSelf = self;
[OWSBackupAPI deleteRecordsFromCloudWithRecordNames:batchRecordNames [OWSBackupAPI deleteRecordsFromCloudWithRecordNames:batchRecordNames
success:^{ success:^{
[weakSelf deleteRecordsFromCloud:obsoleteRecordNames [self deleteRecordsFromCloud:obsoleteRecordNames
deletedCount:deletedCount + batchRecordNames.count deletedCount:deletedCount + batchRecordNames.count
completion:completion]; completion:completion];
} }
failure:^(NSError *error) { failure:^(NSError *error) {
// Cloud cleanup is non-critical so any error is recoverable. // Cloud cleanup is non-critical so any error is recoverable.
[weakSelf deleteRecordsFromCloud:obsoleteRecordNames [self deleteRecordsFromCloud:obsoleteRecordNames
deletedCount:deletedCount + batchRecordNames.count deletedCount:deletedCount + batchRecordNames.count
completion:completion]; completion:completion];
}]; }];
} }

Loading…
Cancel
Save