Initialize on demand

This commit is contained in:
M66B 2020-12-31 14:12:21 +01:00
parent a71ca7b35a
commit 4a973e38d8
2 changed files with 43 additions and 24 deletions

View File

@ -133,8 +133,11 @@ public interface DaoOperation {
@Query("UPDATE operation SET state = :state WHERE id = :id AND NOT (state IS :state)")
int setOperationState(long id, String state);
@Query("UPDATE operation SET state = NULL WHERE state IS NOT NULL")
int resetOperationStates();
@Query("UPDATE operation SET state = NULL" +
" WHERE account = :account" +
" AND state IS NOT NULL" +
" AND name <> '" + EntityOperation.SEND + "'")
int resetOperationStates(long account);
@Query("UPDATE operation SET error = :error WHERE id = :id AND NOT (error IS :error)")
int setOperationError(long id, String error);

View File

@ -212,6 +212,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private boolean fts = false;
private int lastEventId = 0;
private int lastQuitId = -1;
private List<Long> initialized = new ArrayList<>();
private List<TupleAccountNetworkState> accountStates = new ArrayList<>();
private ExecutorService queue = Helper.getBackgroundExecutor(1, "service");
@ -234,6 +235,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
boolean runService = false;
for (TupleAccountNetworkState current : accountNetworkStates) {
Log.d("### evaluating " + current);
if (!initialized.contains(current.accountState.id)) {
initialized.add(current.accountState.id);
init(current);
}
if (current.accountState.shouldRun(current.enabled))
runService = true;
if (!current.accountState.isTransient(ServiceSynchronize.this) &&
@ -373,6 +378,37 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
}
}
private void init(final TupleAccountNetworkState accountNetworkState) {
queue.submit(new Runnable() {
@Override
public void run() {
EntityLog.log(ServiceSynchronize.this, "### init " + accountNetworkState);
DB db = DB.getInstance(ServiceSynchronize.this);
try {
db.beginTransaction();
db.account().setAccountState(accountNetworkState.accountState.id, null);
db.account().setAccountBackoff(accountNetworkState.accountState.id, null);
for (EntityFolder folder : db.folder().getFolders(accountNetworkState.accountState.id, false, false)) {
db.folder().setFolderState(folder.id, null);
db.folder().setFolderSyncState(folder.id, null);
db.folder().setFolderPollCount(folder.id, 0);
}
db.operation().resetOperationStates(accountNetworkState.accountState.id);
db.setTransactionSuccessful();
} catch (Throwable ex) {
Log.e(ex);
} finally {
db.endTransaction();
}
}
});
}
private void start(final TupleAccountNetworkState accountNetworkState, boolean sync, boolean force) {
EntityLog.log(ServiceSynchronize.this,
"Service start=" + accountNetworkState + " sync=" + sync + " force=" + force);
@ -2073,7 +2109,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
};
private class MediatorState extends MediatorLiveData<List<TupleAccountNetworkState>> {
boolean running = true;
private boolean running = true;
private ConnectionHelper.NetworkState lastNetworkState = null;
private List<TupleAccountState> lastAccountStates = null;
@ -2150,22 +2186,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
try {
db.beginTransaction();
// Reset accounts
for (EntityAccount account : db.account().getAccounts()) {
db.account().setAccountState(account.id, null);
db.account().setAccountBackoff(account.id, null);
}
// reset folders
for (EntityFolder folder : db.folder().getFolders()) {
db.folder().setFolderState(folder.id, null);
db.folder().setFolderSyncState(folder.id, null);
db.folder().setFolderPollCount(folder.id, 0);
}
// Reset operations
db.operation().resetOperationStates();
// Restore notifications
db.message().clearNotifyingMessages();
@ -2182,11 +2202,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
schedule(context, false);
// Init service
int accounts = db.account().getSynchronizingAccounts().size();
if (accounts > 0) {
// Reload: watchdog or user might have started service already
reload(context, null, false, "boot");
}
eval(context, "boot");
} catch (Throwable ex) {
Log.e(ex);
}