Inline cleanup log

This commit is contained in:
M66B 2021-01-22 18:05:06 +01:00
parent 05f11818bf
commit 1b03e005d4
3 changed files with 50 additions and 44 deletions

View File

@ -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) {

View File

@ -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<Void>() {
@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());
}
}

View File

@ -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");