Synchronize while running

pull/146/head
M66B 6 years ago
parent a7fefa5bdb
commit 3fe2a6d357

@ -711,7 +711,6 @@ public class ServiceSynchronize extends LifecycleService {
int backoff = CONNECT_BACKOFF_START; int backoff = CONNECT_BACKOFF_START;
while (state.running()) { while (state.running()) {
state.reset();
Log.i(Helper.TAG, account.name + " run"); Log.i(Helper.TAG, account.name + " run");
// Debug // Debug
@ -1045,7 +1044,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.alive()) { 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");
@ -1323,7 +1322,7 @@ public class ServiceSynchronize extends LifecycleService {
DB db = DB.getInstance(this); DB db = DB.getInstance(this);
List<EntityOperation> ops = db.operation().getOperationsByFolder(folder.id); List<EntityOperation> ops = db.operation().getOperationsByFolder(folder.id);
Log.i(Helper.TAG, folder.name + " pending operations=" + ops.size()); Log.i(Helper.TAG, folder.name + " pending operations=" + ops.size());
for (int i = 0; i < ops.size() && state.alive(); i++) { for (int i = 0; i < ops.size() && state.running(); i++) {
EntityOperation op = ops.get(i); EntityOperation op = ops.get(i);
try { try {
Log.i(Helper.TAG, folder.name + Log.i(Helper.TAG, folder.name +
@ -1852,7 +1851,7 @@ public class ServiceSynchronize extends LifecycleService {
long fetch = SystemClock.elapsedRealtime(); long fetch = SystemClock.elapsedRealtime();
Log.i(Helper.TAG, folder.name + " remote fetched=" + (SystemClock.elapsedRealtime() - fetch) + " ms"); Log.i(Helper.TAG, folder.name + " remote fetched=" + (SystemClock.elapsedRealtime() - fetch) + " ms");
for (int i = 0; i < imessages.length && state.alive(); i++) { for (int i = 0; i < imessages.length && state.running(); i++) {
Message imessage = imessages[i]; Message imessage = imessages[i];
try { try {
@ -1886,7 +1885,7 @@ public class ServiceSynchronize extends LifecycleService {
// Add/update local messages // Add/update local messages
Long[] ids = new Long[imessages.length]; Long[] ids = new Long[imessages.length];
Log.i(Helper.TAG, folder.name + " add=" + imessages.length); Log.i(Helper.TAG, folder.name + " add=" + imessages.length);
for (int i = imessages.length - 1; i >= 0 && state.alive(); i -= SYNC_BATCH_SIZE) { for (int i = imessages.length - 1; i >= 0 && state.running(); i -= SYNC_BATCH_SIZE) {
int from = Math.max(0, i - SYNC_BATCH_SIZE + 1); int from = Math.max(0, i - SYNC_BATCH_SIZE + 1);
//Log.i(Helper.TAG, folder.name + " update " + from + " .. " + i); //Log.i(Helper.TAG, folder.name + " update " + from + " .. " + i);
@ -1907,7 +1906,7 @@ public class ServiceSynchronize extends LifecycleService {
" " + (SystemClock.elapsedRealtime() - headers) + " ms"); " " + (SystemClock.elapsedRealtime() - headers) + " ms");
} }
for (int j = isub.length - 1; j >= 0 && state.alive(); j--) for (int j = isub.length - 1; j >= 0 && state.running(); j--)
try { try {
db.beginTransaction(); db.beginTransaction();
ids[from + j] = synchronizeMessage( ids[from + j] = synchronizeMessage(
@ -1930,7 +1929,7 @@ public class ServiceSynchronize extends LifecycleService {
((IMAPMessage) isub[j]).invalidateHeaders(); ((IMAPMessage) isub[j]).invalidateHeaders();
} }
if (state.alive()) if (state.running())
try { try {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
@ -1943,14 +1942,14 @@ public class ServiceSynchronize extends LifecycleService {
// Download messages/attachments // Download messages/attachments
Log.i(Helper.TAG, folder.name + " download=" + imessages.length); Log.i(Helper.TAG, folder.name + " download=" + imessages.length);
for (int i = imessages.length - 1; i >= 0 && state.alive(); i -= DOWNLOAD_BATCH_SIZE) { for (int i = imessages.length - 1; i >= 0 && state.running(); i -= DOWNLOAD_BATCH_SIZE) {
int from = Math.max(0, i - DOWNLOAD_BATCH_SIZE + 1); int from = Math.max(0, i - DOWNLOAD_BATCH_SIZE + 1);
//Log.i(Helper.TAG, folder.name + " download " + from + " .. " + i); //Log.i(Helper.TAG, folder.name + " download " + from + " .. " + i);
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1); Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
// Fetch on demand // Fetch on demand
for (int j = isub.length - 1; j >= 0 && state.alive(); j--) for (int j = isub.length - 1; j >= 0 && state.running(); j--)
try { try {
//Log.i(Helper.TAG, folder.name + " download index=" + (from + j) + " id=" + ids[from + j]); //Log.i(Helper.TAG, folder.name + " download index=" + (from + j) + " id=" + ids[from + j]);
if (ids[from + j] != null) { if (ids[from + j] != null) {
@ -1968,7 +1967,7 @@ public class ServiceSynchronize extends LifecycleService {
((IMAPMessage) isub[j]).invalidateHeaders(); ((IMAPMessage) isub[j]).invalidateHeaders();
} }
if (state.alive()) if (state.running())
try { try {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
@ -2556,7 +2555,6 @@ public class ServiceSynchronize extends LifecycleService {
private Thread thread; private Thread thread;
private Semaphore semaphore = new Semaphore(0); private Semaphore semaphore = new Semaphore(0);
private boolean running = true; private boolean running = true;
private boolean error = false;
void runnable(Runnable runnable, String name) { void runnable(Runnable runnable, String name) {
thread = new Thread(runnable, name); thread = new Thread(runnable, name);
@ -2576,7 +2574,6 @@ public class ServiceSynchronize extends LifecycleService {
} }
void error() { void error() {
error = true;
thread.interrupt(); thread.interrupt();
yield(); yield();
} }
@ -2589,10 +2586,6 @@ public class ServiceSynchronize extends LifecycleService {
} }
} }
void reset() {
error = false;
}
void start() { void start() {
thread.setPriority(THREAD_PRIORITY_BACKGROUND); thread.setPriority(THREAD_PRIORITY_BACKGROUND);
thread.start(); thread.start();
@ -2612,10 +2605,6 @@ public class ServiceSynchronize extends LifecycleService {
return running; return running;
} }
boolean alive() {
return (running && !error);
}
void join(Thread thread) { void join(Thread thread) {
boolean joined = false; boolean joined = false;
while (!joined) while (!joined)
@ -2632,7 +2621,7 @@ public class ServiceSynchronize extends LifecycleService {
@NonNull @NonNull
@Override @Override
public String toString() { public String toString() {
return "[running=" + running + " error=" + error + "]"; return "[running=" + running + "]";
} }
} }
} }

Loading…
Cancel
Save