Compare commits

...

2 Commits

Author SHA1 Message Date
Hans-Peter Lehmann
9db9dc7732
Run download log cleaner regularly (#7975) 2025-09-07 22:34:40 +02:00
Hans-Peter Lehmann
e8a18d4887
Only load the download log entries we actually need (#7974) 2025-09-07 21:39:26 +02:00
13 changed files with 107 additions and 33 deletions

View File

@ -73,6 +73,7 @@ dependencies {
implementation project(':playback:base')
implementation project(':playback:cast')
implementation project(':storage:database')
implementation project(':storage:database-maintenance-service')
implementation project(':storage:importexport')
implementation project(':storage:preferences')
implementation project(':ui:app-start-intent')

View File

@ -48,6 +48,7 @@ import de.danoeh.antennapod.net.download.serviceinterface.FeedUpdateManager;
import de.danoeh.antennapod.net.sync.serviceinterface.SynchronizationQueue;
import de.danoeh.antennapod.playback.cast.CastEnabledActivity;
import de.danoeh.antennapod.playback.service.PlaybackServiceInterface;
import de.danoeh.antennapod.storage.databasemaintenanceservice.DatabaseMaintenanceWorker;
import de.danoeh.antennapod.storage.importexport.AutomaticDatabaseExportWorker;
import de.danoeh.antennapod.storage.preferences.PlaybackPreferences;
import de.danoeh.antennapod.storage.preferences.UserPreferences;
@ -199,6 +200,7 @@ public class MainActivity extends CastEnabledActivity {
FeedUpdateManager.getInstance().restartUpdateAlarm(this, false);
SynchronizationQueue.getInstance().syncIfNotSyncedRecently();
AutomaticDatabaseExportWorker.enqueueIfNeeded(this, false);
DatabaseMaintenanceWorker.enqueueIfNeeded(this);
WorkManager.getInstance(this)
.getWorkInfosByTagLiveData(FeedUpdateManagerImpl.WORK_TAG_FEED_UPDATE)

View File

@ -589,8 +589,8 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
private void showErrorDetails() {
Maybe.fromCallable(
() -> {
List<DownloadResult> feedDownloadLog = DBReader.getFeedDownloadLog(feedID);
if (feedDownloadLog.size() == 0 || feedDownloadLog.get(0).isSuccessful()) {
List<DownloadResult> feedDownloadLog = DBReader.getFeedDownloadLog(feedID, 1);
if (feedDownloadLog.isEmpty() || feedDownloadLog.get(0).isSuccessful()) {
return null;
}
return feedDownloadLog.get(0);

View File

@ -244,7 +244,7 @@ public class FeedUpdateWorker extends Worker {
return savedFeed; // No download logs for new subscriptions
}
// we create a 'successful' download log if the feed's last refresh failed
List<DownloadResult> log = DBReader.getFeedDownloadLog(request.getFeedfileId());
List<DownloadResult> log = DBReader.getFeedDownloadLog(request.getFeedfileId(), 1);
if (!log.isEmpty() && !log.get(0).isSuccessful()) {
DBWriter.addDownloadStatus(parserTask.getDownloadStatus());
}

View File

@ -18,7 +18,6 @@ import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
@ -68,7 +67,8 @@ public class LocalFeedUpdater {
}
Feed updatedFeed = tryUpdateFeed(feed, context, documentFolder.getUri(), updaterProgressListener);
if (mustReportDownloadSuccessful(feed)) {
List<DownloadResult> downloadResults = DBReader.getFeedDownloadLog(feed.getId(), 1);
if (downloadResults.isEmpty() || !downloadResults.get(0).isSuccessful()) {
reportSuccess(feed);
}
return updatedFeed;
@ -269,27 +269,6 @@ public class LocalFeedUpdater {
DBWriter.setFeedLastUpdateFailed(feed.getId(), false);
}
/**
* Answers if reporting success is needed for the given feed.
*/
private static boolean mustReportDownloadSuccessful(Feed feed) {
List<DownloadResult> downloadResults = DBReader.getFeedDownloadLog(feed.getId());
if (downloadResults.isEmpty()) {
// report success if never reported before
return true;
}
Collections.sort(downloadResults, (downloadStatus1, downloadStatus2) ->
downloadStatus1.getCompletionDate().compareTo(downloadStatus2.getCompletionDate()));
DownloadResult lastDownloadResult = downloadResults.get(downloadResults.size() - 1);
// report success if the last update was not successful
// (avoid logging success again if the last update was ok)
return !lastDownloadResult.isSuccessful();
}
@FunctionalInterface
public interface UpdaterProgressListener {
void onLocalFileScanned(int scanned, int totalFiles);

View File

@ -39,6 +39,7 @@ include ':playback:cast'
include ':playback:service'
include ':storage:database'
include ':storage:database-maintenance-service'
include ':storage:importexport'
include ':storage:preferences'

View File

@ -0,0 +1,5 @@
# :storage:database-maintenance-service
Periodic tasks to clean up the database, such as clearing old download logs.
Should never be directly triggered by users.

View File

@ -0,0 +1,19 @@
plugins {
id("com.android.library")
}
apply from: "../../common.gradle"
apply from: "../../playFlavor.gradle"
android {
namespace "de.danoeh.antennapod.storage.databasemaintenanceservice"
}
dependencies {
implementation project(':storage:database')
implementation project(':ui:notifications')
annotationProcessor "androidx.annotation:annotation:$annotationVersion"
implementation "androidx.core:core:$coreVersion"
implementation "androidx.work:work-runtime:$workManagerVersion"
implementation "com.google.guava:guava:31.0.1-android"
}

View File

@ -0,0 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:supportsRtl="true">
</application>
</manifest>

View File

@ -0,0 +1,53 @@
package de.danoeh.antennapod.storage.databasemaintenanceservice;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.ForegroundInfo;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import de.danoeh.antennapod.storage.database.PodDBAdapter;
import de.danoeh.antennapod.ui.notifications.NotificationUtils;
import java.util.concurrent.TimeUnit;
public class DatabaseMaintenanceWorker extends Worker {
private static final String WORK_ID_DATABASE_MAINTENANCE = "DatabaseMaintenanceWorker";
public static void enqueueIfNeeded(Context context) {
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(
DatabaseMaintenanceWorker.class, 3, TimeUnit.DAYS).build();
WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORK_ID_DATABASE_MAINTENANCE,
ExistingPeriodicWorkPolicy.KEEP, workRequest);
}
public DatabaseMaintenanceWorker(@NonNull Context context, @NonNull WorkerParameters params) {
super(context, params);
}
@Override
@NonNull
public Result doWork() {
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
adapter.clearOldDownloadLog();
adapter.close();
return Result.success();
}
@NonNull
@Override
public ListenableFuture<ForegroundInfo> getForegroundInfoAsync() {
return Futures.immediateFuture(new ForegroundInfo(R.id.notification_db_maintenance,
new NotificationCompat.Builder(getApplicationContext(), NotificationUtils.CHANNEL_ID_REFRESHING)
.setContentTitle(getApplicationContext().getString(R.string.download_notification_title_feeds))
.setSmallIcon(R.drawable.ic_notification_sync)
.setOngoing(true)
.build()));
}
}

View File

@ -0,0 +1,3 @@
<resources>
<item name="notification_db_maintenance" type="id"/>
</resources>

View File

@ -321,13 +321,13 @@ public final class DBReader {
* @return A list with DownloadStatus objects that represent the feed's download log,
* newest events first.
*/
public static List<DownloadResult> getFeedDownloadLog(long feedId) {
public static List<DownloadResult> getFeedDownloadLog(long feedId, long limit) {
Log.d(TAG, "getFeedDownloadLog() called with: " + "feed = [" + feedId + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
try (DownloadResultCursor cursor = new DownloadResultCursor(
adapter.getDownloadLog(Feed.FEEDFILETYPE_FEED, feedId))) {
adapter.getDownloadLog(Feed.FEEDFILETYPE_FEED, feedId, limit))) {
List<DownloadResult> downloadLog = new ArrayList<>(cursor.getCount());
while (cursor.moveToNext()) {
downloadLog.add(cursor.getDownloadResult());

View File

@ -963,6 +963,11 @@ public class PodDBAdapter {
db.delete(TABLE_NAME_DOWNLOAD_LOG, null, null);
}
public void clearOldDownloadLog() {
db.execSQL("DELETE FROM " + PodDBAdapter.TABLE_NAME_DOWNLOAD_LOG + " WHERE "
+ PodDBAdapter.KEY_COMPLETION_DATE + "<" + (System.currentTimeMillis() - 7L * 24L * 3600L * 1000L));
}
/**
* Get all Feeds from the Feed Table.
*
@ -1015,10 +1020,10 @@ public class PodDBAdapter {
);
}
public final Cursor getDownloadLog(final int feedFileType, final long feedFileId) {
public final Cursor getDownloadLog(final int feedFileType, final long feedFileId, final long limit) {
final String query = "SELECT * FROM " + TABLE_NAME_DOWNLOAD_LOG +
" WHERE " + KEY_FEEDFILE + "=" + feedFileId + " AND " + KEY_FEEDFILETYPE + "=" + feedFileType
+ " ORDER BY " + KEY_COMPLETION_DATE + " DESC";
+ " ORDER BY " + KEY_COMPLETION_DATE + " DESC LIMIT " + limit;
return db.rawQuery(query, null);
}
@ -1531,9 +1536,6 @@ public class PodDBAdapter {
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
Log.w("DBAdapter", "Upgrading from version " + oldVersion + " to " + newVersion + ".");
DBUpgrader.upgrade(db, oldVersion, newVersion);
db.execSQL("DELETE FROM " + PodDBAdapter.TABLE_NAME_DOWNLOAD_LOG + " WHERE "
+ PodDBAdapter.KEY_COMPLETION_DATE + "<" + (System.currentTimeMillis() - 7L * 24L * 3600L * 1000L));
}
}
}