From b7acf7a2887a5d1f5a3311bfd9e0ecbd6300756e Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 8 Feb 2020 12:49:12 +0100 Subject: [PATCH] Cleanup after operation errors --- app/src/main/java/eu/faircode/email/Core.java | 61 +++++++++---------- .../eu/faircode/email/EntityOperation.java | 3 + 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 651f66d055..ce3a0c96d0 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -152,6 +152,7 @@ class Core { " folder=" + op.folder + " msg=" + op.message + " args=" + op.args + + " group=" + group + " retry=" + retry); // Fetch most recent copy of message @@ -242,14 +243,9 @@ class Core { db.beginTransaction(); db.operation().setOperationError(op.id, null); - for (TupleOperationEx s : similar.keySet()) - db.operation().setOperationError(s.id, null); - if (message != null) { + if (message != null) db.message().setMessageError(message.id, null); - for (EntityMessage m : similar.values()) - db.message().setMessageError(m.id, null); - } if (!EntityOperation.SYNC.equals(op.name)) { db.operation().setOperationState(op.id, "executing"); @@ -391,33 +387,27 @@ class Core { state.error(ex); + if (similar.size() > 0) { + // Retry individually + group = false; + // Finally will reset state + continue; + } + try { db.beginTransaction(); - db.operation().setOperationError(op.id, Log.formatThrowable(ex)); - for (TupleOperationEx s : similar.keySet()) - db.operation().setOperationError(s.id, Log.formatThrowable(ex)); + op.error = Log.formatThrowable(ex); + db.operation().setOperationError(op.id, op.error); - if (message != null && !(ex instanceof IllegalArgumentException)) { + if (message != null && !(ex instanceof IllegalArgumentException)) db.message().setMessageError(message.id, Log.formatThrowable(ex)); - for (EntityMessage m : similar.values()) - db.message().setMessageError(m.id, Log.formatThrowable(ex)); - } - - if (similar.size() > 0 && !(ex instanceof IllegalArgumentException)) - db.folder().setFolderError(folder.id, Log.formatThrowable(ex)); db.setTransactionSuccessful(); } finally { db.endTransaction(); } - if (similar.size() > 0) { - // Retry individually - group = false; - continue; - } - if (ex instanceof OutOfMemoryError || ex instanceof MessageRemovedException || ex instanceof MessageRemovedIOException || @@ -443,13 +433,12 @@ class Core { try { db.beginTransaction(); + // Cleanup operation + op.cleanup(context); + // There is no use in repeating db.operation().deleteOperation(op.id); - // Cleanup folder - if (EntityOperation.SYNC.equals(op.name)) - db.folder().setFolderSyncState(folder.id, null); - // Cleanup messages if (message != null && (ex instanceof MessageRemovedException || @@ -458,9 +447,6 @@ class Core { ex.getCause() instanceof MessageRemovedIOException)) db.message().deleteMessage(message.id); - // Cleanup operations - op.cleanup(context); - db.setTransactionSuccessful(); } finally { db.endTransaction(); @@ -477,6 +463,7 @@ class Core { } } } finally { + // Reset operation state try { db.beginTransaction(); @@ -494,11 +481,19 @@ class Core { } } - for (TupleOperationEx op : ops) - op.cleanup(context); + if (state.isRunning() && state.isRecoverable()) + try { + db.beginTransaction(); + for (TupleOperationEx op : ops) { + Log.e("Operation=" + op.name + " error=" + op.error); + op.cleanup(context); + db.operation().deleteOperation(op.id); + } - if (ops.size() > 0 && state.isRunning() && state.isRecoverable()) - Log.e("Operations failed=" + ops.size()); + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } } finally { Log.i(folder.name + " end process state=" + state); diff --git a/app/src/main/java/eu/faircode/email/EntityOperation.java b/app/src/main/java/eu/faircode/email/EntityOperation.java index 5375f0481e..f23a6fca60 100644 --- a/app/src/main/java/eu/faircode/email/EntityOperation.java +++ b/app/src/main/java/eu/faircode/email/EntityOperation.java @@ -440,6 +440,9 @@ public class EntityOperation { Log.e(ex); } + if (EntityOperation.SYNC.equals(name)) + db.folder().setFolderSyncState(folder, null); + if (message != null) { EntityMessage m = db.message().getMessage(message); if (m == null || m.uid == null)