Transactions for operation/message state changes

This commit is contained in:
M66B 2019-09-28 15:47:34 +02:00
parent 4436e62e59
commit c09290b569
1 changed files with 87 additions and 39 deletions

View File

@ -157,22 +157,16 @@ class Core {
if (op.message != null) if (op.message != null)
message = db.message().getMessage(op.message); message = db.message().getMessage(op.message);
if (message == null &&
!EntityOperation.FETCH.equals(op.name) &&
!EntityOperation.SYNC.equals(op.name) &&
!EntityOperation.SUBSCRIBE.equals(op.name))
throw new MessageRemovedException();
JSONArray jargs = new JSONArray(op.args); JSONArray jargs = new JSONArray(op.args);
Map<EntityOperation, EntityMessage> similar = new HashMap<>(); Map<EntityOperation, EntityMessage> similar = new HashMap<>();
try { try {
db.operation().setOperationError(op.id, null);
if (!EntityOperation.SYNC.equals(op.name))
db.operation().setOperationState(op.id, "executing");
if (message == null) {
if (!EntityOperation.FETCH.equals(op.name) &&
!EntityOperation.SYNC.equals(op.name) &&
!EntityOperation.SUBSCRIBE.equals(op.name))
throw new MessageRemovedException();
} else
db.message().setMessageError(message.id, null);
// Operations should use database transaction when needed // Operations should use database transaction when needed
// Process similar operations // Process similar operations
@ -207,8 +201,6 @@ class Core {
if (m != null) { if (m != null) {
processed.add(next.id); processed.add(next.id);
similar.put(next, m); similar.put(next, m);
db.operation().setOperationState(next.id, "executing");
db.message().setMessageError(m.id, null);
} }
} }
} }
@ -243,6 +235,30 @@ class Core {
crumb.put("free", Integer.toString(Log.getFreeMemMb())); crumb.put("free", Integer.toString(Log.getFreeMemMb()));
Log.breadcrumb("start operation", crumb); Log.breadcrumb("start operation", crumb);
try {
db.beginTransaction();
db.operation().setOperationError(op.id, null);
for (EntityOperation s : similar.keySet())
db.operation().setOperationError(s.id, 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");
for (EntityOperation s : similar.keySet())
db.operation().setOperationState(s.id, "executing");
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
if (istore instanceof POP3Store) if (istore instanceof POP3Store)
switch (op.name) { switch (op.name) {
case EntityOperation.SEEN: case EntityOperation.SEEN:
@ -351,21 +367,37 @@ class Core {
Log.breadcrumb("end operation", crumb); Log.breadcrumb("end operation", crumb);
// Operation succeeded // Operation succeeded
db.operation().deleteOperation(op.id); try {
for (EntityOperation s : similar.keySet()) db.beginTransaction();
db.operation().deleteOperation(s.id);
db.operation().deleteOperation(op.id);
for (EntityOperation s : similar.keySet())
db.operation().deleteOperation(s.id);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(folder.name, ex); Log.e(folder.name, ex);
EntityLog.log(context, folder.name + " " + Helper.formatThrowable(ex, false)); EntityLog.log(context, folder.name + " " + Helper.formatThrowable(ex, false));
db.operation().setOperationError(op.id, Helper.formatThrowable(ex)); try {
for (EntityOperation s : similar.keySet()) db.beginTransaction();
db.operation().setOperationError(s.id, Helper.formatThrowable(ex));
if (message != null && !(ex instanceof IllegalArgumentException)) { db.operation().setOperationError(op.id, Helper.formatThrowable(ex));
db.message().setMessageError(message.id, Helper.formatThrowable(ex)); for (EntityOperation s : similar.keySet())
for (EntityMessage m : similar.values()) db.operation().setOperationError(s.id, Helper.formatThrowable(ex));
db.message().setMessageError(m.id, Helper.formatThrowable(ex));
if (message != null && !(ex instanceof IllegalArgumentException)) {
db.message().setMessageError(message.id, Helper.formatThrowable(ex));
for (EntityMessage m : similar.values())
db.message().setMessageError(m.id, Helper.formatThrowable(ex));
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
} }
if (ex instanceof OutOfMemoryError || if (ex instanceof OutOfMemoryError ||
@ -381,20 +413,28 @@ class Core {
// com.sun.mail.iap.CommandFailedException: B16 NO [ALERT] Cannot MOVE messages out of the Drafts folder // com.sun.mail.iap.CommandFailedException: B16 NO [ALERT] Cannot MOVE messages out of the Drafts folder
Log.w("Unrecoverable"); Log.w("Unrecoverable");
// There is no use in repeating try {
db.operation().deleteOperation(op.id); db.beginTransaction();
for (EntityOperation s : similar.keySet())
db.operation().deleteOperation(s.id);
// Cleanup // There is no use in repeating
if (EntityOperation.SYNC.equals(op.name)) db.operation().deleteOperation(op.id);
db.folder().setFolderSyncState(folder.id, null); for (EntityOperation s : similar.keySet())
db.operation().deleteOperation(s.id);
// Cleanup // Cleanup
if (message != null && ex instanceof MessageRemovedException) { if (EntityOperation.SYNC.equals(op.name))
db.message().deleteMessage(message.id); db.folder().setFolderSyncState(folder.id, null);
for (EntityMessage m : similar.values())
db.message().deleteMessage(m.id); // Cleanup
if (message != null && ex instanceof MessageRemovedException) {
db.message().deleteMessage(message.id);
for (EntityMessage m : similar.values())
db.message().deleteMessage(m.id);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
} }
continue; continue;
@ -402,9 +442,17 @@ class Core {
throw ex; throw ex;
} finally { } finally {
db.operation().setOperationState(op.id, null); try {
for (EntityOperation s : similar.keySet()) db.beginTransaction();
db.operation().setOperationState(s.id, null);
db.operation().setOperationState(op.id, null);
for (EntityOperation s : similar.keySet())
db.operation().setOperationState(s.id, null);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} }
} finally { } finally {
Log.i(folder.name + " end op=" + op.id + "/" + op.name); Log.i(folder.name + " end op=" + op.id + "/" + op.name);