Fixed synchronization

This commit is contained in:
M66B 2018-10-07 18:43:44 +00:00
parent 8f784c8954
commit b75088833e
1 changed files with 43 additions and 73 deletions

View File

@ -576,10 +576,7 @@ public class ServiceSynchronize extends LifecycleService {
public void notification(StoreEvent e) { public void notification(StoreEvent e) {
Log.i(Helper.TAG, account.name + " event: " + e.getMessage()); Log.i(Helper.TAG, account.name + " event: " + e.getMessage());
db.account().setAccountError(account.id, e.getMessage()); db.account().setAccountError(account.id, e.getMessage());
state.thread.interrupt();
synchronized (state) {
state.notifyAll();
}
} }
}); });
@ -588,9 +585,7 @@ public class ServiceSynchronize extends LifecycleService {
@Override @Override
public void folderCreated(FolderEvent e) { public void folderCreated(FolderEvent e) {
Log.i(Helper.TAG, "Folder created=" + e.getFolder().getFullName()); Log.i(Helper.TAG, "Folder created=" + e.getFolder().getFullName());
synchronized (state) { state.thread.interrupt();
state.notifyAll();
}
} }
@Override @Override
@ -602,17 +597,13 @@ public class ServiceSynchronize extends LifecycleService {
int count = db.folder().renameFolder(account.id, old, name); int count = db.folder().renameFolder(account.id, old, name);
Log.i(Helper.TAG, "Renamed to " + name + " count=" + count); Log.i(Helper.TAG, "Renamed to " + name + " count=" + count);
synchronized (state) { state.thread.interrupt();
state.notifyAll();
}
} }
@Override @Override
public void folderDeleted(FolderEvent e) { public void folderDeleted(FolderEvent e) {
Log.i(Helper.TAG, "Folder deleted=" + e.getFolder().getFullName()); Log.i(Helper.TAG, "Folder deleted=" + e.getFolder().getFullName());
synchronized (state) { state.thread.interrupt();
state.notifyAll();
}
} }
}); });
@ -722,9 +713,7 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
synchronized (state) { state.thread.interrupt();
state.notifyAll();
}
} }
} }
} }
@ -751,9 +740,7 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
synchronized (state) { state.thread.interrupt();
state.notifyAll();
}
} }
} }
} }
@ -801,9 +788,7 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
synchronized (state) { state.thread.interrupt();
state.notifyAll();
}
} }
} }
} }
@ -826,9 +811,7 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
synchronized (state) { state.thread.interrupt();
state.notifyAll();
}
} finally { } finally {
if (!capIdle) if (!capIdle)
Log.i(Helper.TAG, folder.name + " end polling"); Log.i(Helper.TAG, folder.name + " end polling");
@ -845,7 +828,7 @@ public class ServiceSynchronize extends LifecycleService {
public void run() { public void run() {
try { try {
Log.i(Helper.TAG, folder.name + " start idle"); Log.i(Helper.TAG, folder.name + " start idle");
while (state.running && ifolder.isOpen()) { while (state.running) {
Log.i(Helper.TAG, folder.name + " do idle"); Log.i(Helper.TAG, folder.name + " do idle");
ifolder.idle(false); ifolder.idle(false);
//Log.i(Helper.TAG, folder.name + " done idle"); //Log.i(Helper.TAG, folder.name + " done idle");
@ -856,9 +839,7 @@ public class ServiceSynchronize extends LifecycleService {
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex)); db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
synchronized (state) { state.thread.interrupt();
state.notifyAll();
}
} finally { } finally {
Log.i(Helper.TAG, folder.name + " end idle"); Log.i(Helper.TAG, folder.name + " end idle");
} }
@ -956,20 +937,23 @@ public class ServiceSynchronize extends LifecycleService {
while (state.running) { while (state.running) {
EntityLog.log(this, account.name + " wait=" + account.poll_interval); EntityLog.log(this, account.name + " wait=" + account.poll_interval);
synchronized (state) { try {
try { Thread.sleep(account.poll_interval * 60 * 1000L);
state.wait(account.poll_interval * 60 * 1000L); } catch (InterruptedException ex) {
} catch (InterruptedException ex) { Log.w(Helper.TAG, account.name + " wait " + ex.toString());
Log.w(Helper.TAG, account.name + " wait " + ex.toString());
}
} }
if (!istore.isConnected()) if (state.running) {
throw new StoreClosedException(istore); EntityLog.log(this, account.name + " checking store");
if (!istore.isConnected())
throw new StoreClosedException(istore);
for (EntityFolder folder : folders.keySet()) for (EntityFolder folder : folders.keySet()) {
if (!folders.get(folder).isOpen()) EntityLog.log(this, account.name + " checking " + folder.name);
throw new FolderClosedException(folders.get(folder)); if (!folders.get(folder).isOpen())
throw new FolderClosedException(folders.get(folder));
}
}
} }
Log.i(Helper.TAG, account.name + " done running=" + state.running); Log.i(Helper.TAG, account.name + " done running=" + state.running);
} finally { } finally {
@ -1830,7 +1814,6 @@ public class ServiceSynchronize extends LifecycleService {
private ServiceState state; private ServiceState state;
private boolean running = false; private boolean running = false;
private long lastLost = 0; private long lastLost = 0;
private Thread main;
private EntityFolder outbox = null; private EntityFolder outbox = null;
private ExecutorService lifecycle = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory); private ExecutorService lifecycle = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory); private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
@ -1879,9 +1862,8 @@ public class ServiceSynchronize extends LifecycleService {
EntityLog.log(ServiceSynchronize.this, "Main start"); EntityLog.log(ServiceSynchronize.this, "Main start");
state = new ServiceState(); state = new ServiceState();
state.thread = new Thread(new Runnable() {
main = new Thread(new Runnable() { private List<ServiceState> threadState = new ArrayList<>();
private Map<Thread, ServiceState> threadState = new HashMap<>();
@Override @Override
public void run() { public void run() {
@ -1931,7 +1913,7 @@ public class ServiceSynchronize extends LifecycleService {
for (final EntityAccount account : accounts) { for (final EntityAccount account : accounts) {
Log.i(Helper.TAG, account.host + "/" + account.user + " run"); Log.i(Helper.TAG, account.host + "/" + account.user + " run");
final ServiceState astate = new ServiceState(); final ServiceState astate = new ServiceState();
Thread t = new Thread(new Runnable() { astate.thread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -1942,30 +1924,23 @@ public class ServiceSynchronize extends LifecycleService {
} }
} }
}, "sync.account." + account.id); }, "sync.account." + account.id);
t.start(); astate.thread.start();
threadState.put(t, astate); threadState.add(astate);
} }
EntityLog.log(ServiceSynchronize.this, "Main started"); EntityLog.log(ServiceSynchronize.this, "Main started");
synchronized (state) { try {
try { Thread.sleep(Long.MAX_VALUE);
if (state.running) } catch (InterruptedException ex) {
state.wait(); Log.w(Helper.TAG, "main wait " + ex.toString());
} catch (InterruptedException ex) {
Log.w(Helper.TAG, "main wait " + ex.toString());
}
} }
// Stop monitoring accounts // Stop monitoring accounts
for (Thread t : threadState.keySet()) { for (ServiceState astate : threadState) {
ServiceState astate = threadState.get(t); astate.running = false;
synchronized (astate) { astate.thread.interrupt();
astate.running = false; join(astate.thread);
astate.notifyAll();
}
t.interrupt();
join(t);
} }
threadState.clear(); threadState.clear();
@ -1981,25 +1956,19 @@ public class ServiceSynchronize extends LifecycleService {
} }
} }
}, "sync.main"); }, "sync.main");
main.setPriority(THREAD_PRIORITY_BACKGROUND); // will be inherited state.thread.setPriority(THREAD_PRIORITY_BACKGROUND); // will be inherited
main.start(); state.thread.start();
} }
private void stop() { private void stop() {
EntityLog.log(ServiceSynchronize.this, "Main stop"); EntityLog.log(ServiceSynchronize.this, "Main stop");
synchronized (state) { state.running = false;
state.running = false; state.thread.interrupt();
state.notifyAll(); join(state.thread);
}
// stop wait or backoff
main.interrupt();
join(main);
EntityLog.log(ServiceSynchronize.this, "Main stopped"); EntityLog.log(ServiceSynchronize.this, "Main stopped");
main = null;
state = null; state = null;
} }
@ -2066,5 +2035,6 @@ public class ServiceSynchronize extends LifecycleService {
private class ServiceState { private class ServiceState {
boolean running = true; boolean running = true;
Thread thread;
} }
} }