From 1b03e005d49ecc3c85e6ef7eada1081a7c01c01b Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 22 Jan 2021 18:05:06 +0100 Subject: [PATCH] Inline cleanup log --- .../java/eu/faircode/email/EntityLog.java | 51 ++++++++++++++++++- .../java/eu/faircode/email/FragmentLogs.java | 25 +-------- .../java/eu/faircode/email/WorkerCleanup.java | 18 ------- 3 files changed, 50 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityLog.java b/app/src/main/java/eu/faircode/email/EntityLog.java index e3c8939676..a5ae8ca19f 100644 --- a/app/src/main/java/eu/faircode/email/EntityLog.java +++ b/app/src/main/java/eu/faircode/email/EntityLog.java @@ -39,6 +39,11 @@ import java.util.concurrent.ExecutorService; ) public class EntityLog { static final String TABLE_NAME = "log"; + static Long last_cleanup = null; + + private static final long LOG_CLEANUP_INTERVAL = 3600 * 1000L; // milliseconds + private static final long LOG_KEEP_DURATION = 24 * 3600 * 1000L; // milliseconds + private static final int LOG_DELETE_BATCH_SIZE = 100; @PrimaryKey(autoGenerate = true) public Long id; @@ -50,7 +55,7 @@ public class EntityLog { private static final ExecutorService executor = Helper.getBackgroundExecutor(1, "log"); - static void log(Context context, String data) { + static void log(final Context context, String data) { Log.i(data); final EntityLog entry = new EntityLog(); @@ -62,11 +67,53 @@ public class EntityLog { executor.submit(new Runnable() { @Override public void run() { - db.log().insertLog(entry); + try { + db.beginTransaction(); + db.log().insertLog(entry); + db.setTransactionSuccessful(); + } catch (Throwable ex) { + Log.e(ex); + } finally { + db.endTransaction(); + } + + long now = new Date().getTime(); + if (last_cleanup == null || last_cleanup + LOG_CLEANUP_INTERVAL < now) { + last_cleanup = now; + cleanup(context, now - LOG_KEEP_DURATION); + } } }); } + static void clear(final Context context) { + executor.submit(new Runnable() { + @Override + public void run() { + cleanup(context, new Date().getTime()); + } + }); + } + + private static void cleanup(final Context context, final long before) { + Log.i("Log cleanup interval=" + LOG_CLEANUP_INTERVAL); + DB db = DB.getInstance(context); + while (true) + try { + db.beginTransaction(); + int logs = db.log().deleteLogs(before, LOG_DELETE_BATCH_SIZE); + db.setTransactionSuccessful(); + Log.i("Cleanup logs=" + logs + " before=" + new Date(before)); + if (logs < LOG_DELETE_BATCH_SIZE) + break; + } catch (Throwable ex) { + Log.e(ex); + break; + } finally { + db.endTransaction(); + } + } + @Override public boolean equals(Object obj) { if (obj instanceof EntityLog) { diff --git a/app/src/main/java/eu/faircode/email/FragmentLogs.java b/app/src/main/java/eu/faircode/email/FragmentLogs.java index bd18769b5a..b25239ce26 100644 --- a/app/src/main/java/eu/faircode/email/FragmentLogs.java +++ b/app/src/main/java/eu/faircode/email/FragmentLogs.java @@ -19,7 +19,6 @@ package eu.faircode.email; Copyright 2018-2021 by Marcel Bokhorst (M66B) */ -import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; @@ -129,28 +128,6 @@ public class FragmentLogs extends FragmentBase { } private void onMenuClear() { - Bundle args = new Bundle(); - - new SimpleTask() { - @Override - protected Void onExecute(Context context, Bundle args) { - DB db = DB.getInstance(context); - - long before = new Date().getTime(); - while (true) { - int logs = db.log().deleteLogs(before, WorkerCleanup.LOG_DELETE_BATCH_SIZE); - Log.i("Deleted logs=" + logs); - if (logs < WorkerCleanup.LOG_DELETE_BATCH_SIZE) - break; - } - - return null; - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Log.unexpectedError(getParentFragmentManager(), ex); - } - }.execute(this, args, "log:clear"); + EntityLog.clear(getContext()); } } diff --git a/app/src/main/java/eu/faircode/email/WorkerCleanup.java b/app/src/main/java/eu/faircode/email/WorkerCleanup.java index 10937baf69..58595332ed 100644 --- a/app/src/main/java/eu/faircode/email/WorkerCleanup.java +++ b/app/src/main/java/eu/faircode/email/WorkerCleanup.java @@ -51,9 +51,6 @@ public class WorkerCleanup extends Worker { private static final long KEEP_FILES_DURATION = 3600 * 1000L; // milliseconds private static final long KEEP_IMAGES_DURATION = 3 * 24 * 3600 * 1000L; // milliseconds private static final long KEEP_CONTACTS_DURATION = 180 * 24 * 3600 * 1000L; // milliseconds - private static final long KEEP_LOG_DURATION = 24 * 3600 * 1000L; // milliseconds - - static final int LOG_DELETE_BATCH_SIZE = 100; public WorkerCleanup(@NonNull Context context, @NonNull WorkerParameters workerParams) { super(context, workerParams); @@ -306,21 +303,6 @@ public class WorkerCleanup extends Worker { db.endTransaction(); } - Log.i("Cleanup log"); - long before = now - KEEP_LOG_DURATION; - while (true) { - try { - db.beginTransaction(); - int logs = db.log().deleteLogs(before, LOG_DELETE_BATCH_SIZE); - db.setTransactionSuccessful(); - Log.i("Deleted logs=" + logs + " before=" + new Date(before)); - if (logs < LOG_DELETE_BATCH_SIZE) - break; - } finally { - db.endTransaction(); - } - } - if (BuildConfig.DEBUG) { // https://sqlite.org/lang_analyze.html Log.i("Analyze");