From 4bb0122c07cc8c9d85ea949ca151d286e830a0c2 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 6 Aug 2018 08:52:32 -0400 Subject: [PATCH] Respond to CR. --- Signal/src/util/OWSOrphanDataCleaner.m | 34 ++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/Signal/src/util/OWSOrphanDataCleaner.m b/Signal/src/util/OWSOrphanDataCleaner.m index 9fb838973..41c582d3d 100644 --- a/Signal/src/util/OWSOrphanDataCleaner.m +++ b/Signal/src/util/OWSOrphanDataCleaner.m @@ -3,6 +3,7 @@ // #import "OWSOrphanDataCleaner.h" +#import "DateUtil.h" #import #import #import @@ -23,6 +24,7 @@ NS_ASSUME_NONNULL_BEGIN NSString *const OWSOrphanDataCleaner_Collection = @"OWSOrphanDataCleaner_Collection"; NSString *const OWSOrphanDataCleaner_LastCleaningVersionKey = @"OWSOrphanDataCleaner_LastCleaningVersionKey"; +NSString *const OWSOrphanDataCleaner_LastCleaningDateKey = @"OWSOrphanDataCleaner_LastCleaningDateKey"; @interface OWSOrphanData : NSObject @@ -414,24 +416,39 @@ typedef void (^OrphanDataBlock)(OWSOrphanData *); { OWSAssertIsOnMainThread(); + // In production, do not audit or clean up. +#ifndef DEBUG + return; +#endif + OWSPrimaryStorage *primaryStorage = [OWSPrimaryStorage sharedManager]; YapDatabaseConnection *databaseConnection = primaryStorage.dbReadWriteConnection; - // In production, only clean up once per app version. -#ifndef DEBUG - NSString *_Nullable lastCleaningVersion = - [databaseConnection stringForKey:OWSOrphanDataCleaner_LastCleaningVersionKey - inCollection:OWSOrphanDataCleaner_Collection]; + __block NSString *_Nullable lastCleaningVersion; + __block NSDate *_Nullable lastCleaningDate; + [databaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + lastCleaningVersion = [databaseConnection stringForKey:OWSOrphanDataCleaner_LastCleaningVersionKey + inCollection:OWSOrphanDataCleaner_Collection]; + lastCleaningDate = [databaseConnection dateForKey:OWSOrphanDataCleaner_LastCleaningDateKey + inCollection:OWSOrphanDataCleaner_Collection]; + }]; + + // Only clean up once per app version. NSString *currentAppVersion = AppVersion.sharedInstance.currentAppVersion; if (lastCleaningVersion && [lastCleaningVersion isEqualToString:currentAppVersion]) { DDLogVerbose(@"%@ skipping orphan data cleanup; already done on %@.", self.logTag, currentAppVersion); return; } -#endif + + // Only clean up once per day. + if (lastCleaningDate && [DateUtil dateIsToday:lastCleaningDate]) { + DDLogVerbose(@"%@ skipping orphan data cleanup; already done today.", self.logTag); + return; + } // If we want to be cautious, we can disable orphan deletion using // flag - the cleanup will just be a dry run with logging. - BOOL shouldRemoveOrphans = YES; + BOOL shouldRemoveOrphans = NO; [self auditAndCleanup:shouldRemoveOrphans databaseConnection:databaseConnection]; } @@ -501,6 +518,9 @@ typedef void (^OrphanDataBlock)(OWSOrphanData *); [databaseConnection setObject:AppVersion.sharedInstance.currentAppVersion forKey:OWSOrphanDataCleaner_LastCleaningVersionKey inCollection:OWSOrphanDataCleaner_Collection]; + [databaseConnection setDate:[NSDate new] + forKey:OWSOrphanDataCleaner_LastCleaningDateKey + inCollection:OWSOrphanDataCleaner_Collection]; } failure:^{ DDLogInfo(@"%@ Aborting orphan data cleanup.", self.logTag);