mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-09-18 21:50:56 +00:00
Cleanup less often, at a lower priority, in the BG.
This commit is contained in:
parent
cd1126af3e
commit
a7ca334386
6 changed files with 69 additions and 17 deletions
|
@ -108,11 +108,13 @@ public class BlurDatabaseHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanupStories(boolean keepOldStories) {
|
public void cleanupStories(boolean keepOldStories) {
|
||||||
|
// story cleanup happens at a low priority in the background. the goal here is to lock
|
||||||
|
// the DB for as short a time as possible, not absolute efficiency.
|
||||||
for (String feedId : getAllFeeds()) {
|
for (String feedId : getAllFeeds()) {
|
||||||
String q = "DELETE FROM " + DatabaseConstants.STORY_TABLE +
|
String q = "DELETE FROM " + DatabaseConstants.STORY_TABLE +
|
||||||
" WHERE " + DatabaseConstants.STORY_ID + " IN " +
|
" WHERE " + DatabaseConstants.STORY_ID + " IN " +
|
||||||
"( SELECT " + DatabaseConstants.STORY_ID + " FROM " + DatabaseConstants.STORY_TABLE +
|
"( SELECT " + DatabaseConstants.STORY_ID + " FROM " + DatabaseConstants.STORY_TABLE +
|
||||||
" WHERE " + DatabaseConstants.STORY_READ + " = 1" +
|
" WHERE " + DatabaseConstants.STORY_READ + " = 1" + // don't cleanup unreads
|
||||||
" AND " + DatabaseConstants.STORY_FEED_ID + " = " + feedId +
|
" AND " + DatabaseConstants.STORY_FEED_ID + " = " + feedId +
|
||||||
" ORDER BY " + DatabaseConstants.STORY_TIMESTAMP + " DESC" +
|
" ORDER BY " + DatabaseConstants.STORY_TIMESTAMP + " DESC" +
|
||||||
" LIMIT -1 OFFSET " + (keepOldStories ? AppConstants.MAX_READ_STORIES_STORED : 0) +
|
" LIMIT -1 OFFSET " + (keepOldStories ? AppConstants.MAX_READ_STORIES_STORED : 0) +
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.newsblur.service;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.newsblur.util.ImageCache;
|
||||||
|
import com.newsblur.util.PrefsUtils;
|
||||||
|
|
||||||
|
public class CleanupService extends SubService {
|
||||||
|
|
||||||
|
private static volatile boolean Running = false;
|
||||||
|
|
||||||
|
public CleanupService(NBSyncService parent) {
|
||||||
|
super(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void exec() {
|
||||||
|
if (!PrefsUtils.isTimeToCleanup(parent)) return;
|
||||||
|
|
||||||
|
gotWork();
|
||||||
|
|
||||||
|
// do cleanup
|
||||||
|
parent.dbHelper.cleanupStories(PrefsUtils.isKeepOldStories(parent));
|
||||||
|
parent.dbHelper.cleanupStoryText();
|
||||||
|
ImageCache imageCache = new ImageCache(parent);
|
||||||
|
imageCache.cleanup(parent.dbHelper.getAllStoryImages());
|
||||||
|
|
||||||
|
PrefsUtils.updateLastCleanupTime(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean running() {
|
||||||
|
return Running;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void setRunning(boolean running) {
|
||||||
|
Running = running;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected boolean isRunning() {
|
||||||
|
return Running;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -77,7 +77,6 @@ public class NBSyncService extends Service {
|
||||||
private static final Object PENDING_FEED_MUTEX = new Object();
|
private static final Object PENDING_FEED_MUTEX = new Object();
|
||||||
|
|
||||||
private volatile static boolean ActionsRunning = false;
|
private volatile static boolean ActionsRunning = false;
|
||||||
private volatile static boolean CleanupRunning = false;
|
|
||||||
private volatile static boolean FFSyncRunning = false;
|
private volatile static boolean FFSyncRunning = false;
|
||||||
private volatile static boolean StorySyncRunning = false;
|
private volatile static boolean StorySyncRunning = false;
|
||||||
private volatile static boolean HousekeepingRunning = false;
|
private volatile static boolean HousekeepingRunning = false;
|
||||||
|
@ -126,6 +125,7 @@ public class NBSyncService extends Service {
|
||||||
Set<String> orphanFeedIds;
|
Set<String> orphanFeedIds;
|
||||||
|
|
||||||
private ExecutorService primaryExecutor;
|
private ExecutorService primaryExecutor;
|
||||||
|
CleanupService cleanupService;
|
||||||
OriginalTextService originalTextService;
|
OriginalTextService originalTextService;
|
||||||
UnreadsService unreadsService;
|
UnreadsService unreadsService;
|
||||||
ImagePrefetchService imagePrefetchService;
|
ImagePrefetchService imagePrefetchService;
|
||||||
|
@ -155,6 +155,7 @@ public class NBSyncService extends Service {
|
||||||
if (apiManager == null) {
|
if (apiManager == null) {
|
||||||
apiManager = new APIManager(this);
|
apiManager = new APIManager(this);
|
||||||
dbHelper = new BlurDatabaseHelper(this);
|
dbHelper = new BlurDatabaseHelper(this);
|
||||||
|
cleanupService = new CleanupService(this);
|
||||||
originalTextService = new OriginalTextService(this);
|
originalTextService = new OriginalTextService(this);
|
||||||
unreadsService = new UnreadsService(this);
|
unreadsService = new UnreadsService(this);
|
||||||
imagePrefetchService = new ImagePrefetchService(this);
|
imagePrefetchService = new ImagePrefetchService(this);
|
||||||
|
@ -362,9 +363,6 @@ public class NBSyncService extends Service {
|
||||||
* unread hashes. Doing this resets pagination on the server!
|
* unread hashes. Doing this resets pagination on the server!
|
||||||
*/
|
*/
|
||||||
private void syncMetadata(int startId) {
|
private void syncMetadata(int startId) {
|
||||||
if (stopSync()) return;
|
|
||||||
if (ActMode != ActivationMode.ALL) return;
|
|
||||||
|
|
||||||
if (DoFeedsFolders || PrefsUtils.isTimeToAutoSync(this)) {
|
if (DoFeedsFolders || PrefsUtils.isTimeToAutoSync(this)) {
|
||||||
PrefsUtils.updateLastSyncTime(this);
|
PrefsUtils.updateLastSyncTime(this);
|
||||||
DoFeedsFolders = false;
|
DoFeedsFolders = false;
|
||||||
|
@ -372,16 +370,6 @@ public class NBSyncService extends Service {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanup is expensive, so do it as part of the metadata sync
|
|
||||||
CleanupRunning = true;
|
|
||||||
NbActivity.updateAllActivities(false);
|
|
||||||
dbHelper.cleanupStories(PrefsUtils.isKeepOldStories(this));
|
|
||||||
dbHelper.cleanupStoryText();
|
|
||||||
imagePrefetchService.imageCache.cleanup(dbHelper.getAllStoryImages());
|
|
||||||
CleanupRunning = false;
|
|
||||||
NbActivity.updateAllActivities(false);
|
|
||||||
|
|
||||||
// cleanup may have taken a while, so re-check our running status
|
|
||||||
if (stopSync()) return;
|
if (stopSync()) return;
|
||||||
if (ActMode != ActivationMode.ALL) return;
|
if (ActMode != ActivationMode.ALL) return;
|
||||||
|
|
||||||
|
@ -461,6 +449,7 @@ public class NBSyncService extends Service {
|
||||||
lastFFWriteMillis = System.currentTimeMillis() - startTime;
|
lastFFWriteMillis = System.currentTimeMillis() - startTime;
|
||||||
lastFeedCount = feedValues.size();
|
lastFeedCount = feedValues.size();
|
||||||
|
|
||||||
|
cleanupService.start(startId);
|
||||||
unreadsService.start(startId);
|
unreadsService.start(startId);
|
||||||
UnreadsService.doMetadata();
|
UnreadsService.doMetadata();
|
||||||
|
|
||||||
|
@ -690,7 +679,7 @@ public class NBSyncService extends Service {
|
||||||
* Is the main feed/folder list sync running?
|
* Is the main feed/folder list sync running?
|
||||||
*/
|
*/
|
||||||
public static boolean isFeedFolderSyncRunning() {
|
public static boolean isFeedFolderSyncRunning() {
|
||||||
return (HousekeepingRunning || ActionsRunning || RecountsRunning || FFSyncRunning || CleanupRunning || UnreadsService.running() || StorySyncRunning || OriginalTextService.running() || ImagePrefetchService.running());
|
return (HousekeepingRunning || ActionsRunning || RecountsRunning || FFSyncRunning || CleanupService.running() || UnreadsService.running() || StorySyncRunning || OriginalTextService.running() || ImagePrefetchService.running());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -706,7 +695,7 @@ public class NBSyncService extends Service {
|
||||||
if (HousekeepingRunning) return context.getResources().getString(R.string.sync_status_housekeeping);
|
if (HousekeepingRunning) return context.getResources().getString(R.string.sync_status_housekeeping);
|
||||||
if (ActionsRunning||RecountsRunning) return context.getResources().getString(R.string.sync_status_actions);
|
if (ActionsRunning||RecountsRunning) return context.getResources().getString(R.string.sync_status_actions);
|
||||||
if (FFSyncRunning) return context.getResources().getString(R.string.sync_status_ffsync);
|
if (FFSyncRunning) return context.getResources().getString(R.string.sync_status_ffsync);
|
||||||
if (CleanupRunning) return context.getResources().getString(R.string.sync_status_cleanup);
|
if (CleanupService.running()) return context.getResources().getString(R.string.sync_status_cleanup);
|
||||||
if (StorySyncRunning) return context.getResources().getString(R.string.sync_status_stories);
|
if (StorySyncRunning) return context.getResources().getString(R.string.sync_status_stories);
|
||||||
if (UnreadsService.running()) return String.format(context.getResources().getString(R.string.sync_status_unreads), UnreadsService.getPendingCount());
|
if (UnreadsService.running()) return String.format(context.getResources().getString(R.string.sync_status_unreads), UnreadsService.getPendingCount());
|
||||||
if (OriginalTextService.running()) return String.format(context.getResources().getString(R.string.sync_status_text), OriginalTextService.getPendingCount());
|
if (OriginalTextService.running()) return String.format(context.getResources().getString(R.string.sync_status_text), OriginalTextService.getPendingCount());
|
||||||
|
@ -819,6 +808,7 @@ public class NBSyncService extends Service {
|
||||||
try {
|
try {
|
||||||
if (AppConstants.VERBOSE_LOG) Log.d(this.getClass().getName(), "onDestroy - stopping execution");
|
if (AppConstants.VERBOSE_LOG) Log.d(this.getClass().getName(), "onDestroy - stopping execution");
|
||||||
HaltNow = true;
|
HaltNow = true;
|
||||||
|
if (cleanupService != null) cleanupService.shutdown();
|
||||||
if (unreadsService != null) unreadsService.shutdown();
|
if (unreadsService != null) unreadsService.shutdown();
|
||||||
if (originalTextService != null) originalTextService.shutdown();
|
if (originalTextService != null) originalTextService.shutdown();
|
||||||
if (imagePrefetchService != null) imagePrefetchService.shutdown();
|
if (imagePrefetchService != null) imagePrefetchService.shutdown();
|
||||||
|
|
|
@ -29,6 +29,9 @@ public class AppConstants {
|
||||||
// how often to rebuild the DB
|
// how often to rebuild the DB
|
||||||
public static final long VACUUM_TIME_MILLIS = 12L * 60L * 60L * 1000L;
|
public static final long VACUUM_TIME_MILLIS = 12L * 60L * 60L * 1000L;
|
||||||
|
|
||||||
|
// how often to clean up the DB
|
||||||
|
public static final long CLEANUP_TIME_MILLIS = 2L * 60L * 60L * 1000L;
|
||||||
|
|
||||||
// how often to trigger the BG service. slightly longer than how often we will find new stories,
|
// how often to trigger the BG service. slightly longer than how often we will find new stories,
|
||||||
// to account for the fact that it is approximate, and missing a cycle is bad.
|
// to account for the fact that it is approximate, and missing a cycle is bad.
|
||||||
public static final long BG_SERVICE_CYCLE_MILLIS = AUTO_SYNC_TIME_MILLIS + 30L * 1000L;
|
public static final long BG_SERVICE_CYCLE_MILLIS = AUTO_SYNC_TIME_MILLIS + 30L * 1000L;
|
||||||
|
|
|
@ -62,5 +62,7 @@ public class PrefConstants {
|
||||||
|
|
||||||
public static final String LAST_VACUUM_TIME = "last_vacuum_time";
|
public static final String LAST_VACUUM_TIME = "last_vacuum_time";
|
||||||
|
|
||||||
|
public static final String LAST_CLEANUP_TIME = "last_cleanup_time";
|
||||||
|
|
||||||
public static final String VOLUME_KEY_NAVIGATION = "volume_key_navigation";
|
public static final String VOLUME_KEY_NAVIGATION = "volume_key_navigation";
|
||||||
}
|
}
|
||||||
|
|
|
@ -242,6 +242,17 @@ public class PrefsUtils {
|
||||||
prefs.edit().putLong(PrefConstants.LAST_VACUUM_TIME, (new Date()).getTime()).commit();
|
prefs.edit().putLong(PrefConstants.LAST_VACUUM_TIME, (new Date()).getTime()).commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isTimeToCleanup(Context context) {
|
||||||
|
SharedPreferences prefs = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
|
||||||
|
long lastTime = prefs.getLong(PrefConstants.LAST_CLEANUP_TIME, 1L);
|
||||||
|
return ( (lastTime + AppConstants.CLEANUP_TIME_MILLIS) < (new Date()).getTime() );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void updateLastCleanupTime(Context context) {
|
||||||
|
SharedPreferences prefs = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
|
||||||
|
prefs.edit().putLong(PrefConstants.LAST_CLEANUP_TIME, (new Date()).getTime()).commit();
|
||||||
|
}
|
||||||
|
|
||||||
public static StoryOrder getStoryOrderForFeed(Context context, String feedId) {
|
public static StoryOrder getStoryOrderForFeed(Context context, String feedId) {
|
||||||
SharedPreferences prefs = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
|
SharedPreferences prefs = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
|
||||||
return StoryOrder.valueOf(prefs.getString(PrefConstants.FEED_STORY_ORDER_PREFIX + feedId, getDefaultStoryOrder(prefs).toString()));
|
return StoryOrder.valueOf(prefs.getString(PrefConstants.FEED_STORY_ORDER_PREFIX + feedId, getDefaultStoryOrder(prefs).toString()));
|
||||||
|
|
Loading…
Add table
Reference in a new issue