Delay stop self until no commands queued anymore

pull/146/head
M66B 6 years ago
parent 553aecdd24
commit c4a3362012

@ -1995,6 +1995,7 @@ public class ServiceSynchronize extends LifecycleService {
private class ServiceManager extends ConnectivityManager.NetworkCallback { private class ServiceManager extends ConnectivityManager.NetworkCallback {
private ServiceState state; private ServiceState state;
private boolean running = false; private boolean running = false;
private int queued = 0;
private long lastLost = 0; private long lastLost = 0;
private EntityFolder outbox = null; private EntityFolder outbox = null;
private ExecutorService lifecycle = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory); private ExecutorService lifecycle = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
@ -2008,11 +2009,18 @@ public class ServiceSynchronize extends LifecycleService {
if (!running) { if (!running) {
running = true; running = true;
queued++;
lifecycle.submit(new Runnable() { lifecycle.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
Log.i(Helper.TAG, "Starting service"); Log.i(Helper.TAG, "Starting service");
start(); start();
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} finally {
queued--;
}
} }
}); });
} }
@ -2030,10 +2038,17 @@ public class ServiceSynchronize extends LifecycleService {
EntityLog.log(ServiceSynchronize.this, "Network disconnected=" + ani); EntityLog.log(ServiceSynchronize.this, "Network disconnected=" + ani);
running = false; running = false;
lastLost = new Date().getTime(); lastLost = new Date().getTime();
queued++;
lifecycle.submit(new Runnable() { lifecycle.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
stop(); stop();
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} finally {
queued--;
}
} }
}); });
} }
@ -2041,7 +2056,7 @@ public class ServiceSynchronize extends LifecycleService {
} }
private void start() { private void start() {
EntityLog.log(ServiceSynchronize.this, "Main start"); EntityLog.log(ServiceSynchronize.this, "Main start queued=" + queued);
state = new ServiceState(); state = new ServiceState();
state.thread = new Thread(new Runnable() { state.thread = new Thread(new Runnable() {
@ -2061,16 +2076,14 @@ public class ServiceSynchronize extends LifecycleService {
outbox = db.folder().getOutbox(); outbox = db.folder().getOutbox();
if (outbox == null) { if (outbox == null) {
EntityLog.log(ServiceSynchronize.this, "No outbox, halt"); EntityLog.log(ServiceSynchronize.this, "No outbox, halt");
Thread.sleep(3000); serviceManager.queue_stop();
stopSelf();
return; return;
} }
List<EntityAccount> accounts = db.account().getAccounts(true); List<EntityAccount> accounts = db.account().getAccounts(true);
if (accounts.size() == 0) { if (accounts.size() == 0) {
EntityLog.log(ServiceSynchronize.this, "No accounts, halt"); EntityLog.log(ServiceSynchronize.this, "No accounts, halt");
Thread.sleep(3000); serviceManager.queue_stop();
stopSelf();
return; return;
} }
@ -2172,7 +2185,7 @@ public class ServiceSynchronize extends LifecycleService {
state.semaphore.release(); state.semaphore.release();
join(state.thread); join(state.thread);
EntityLog.log(ServiceSynchronize.this, "Main stopped"); EntityLog.log(ServiceSynchronize.this, "Main stopped queued=" + queued);
state = null; state = null;
} finally { } finally {
@ -2182,23 +2195,38 @@ public class ServiceSynchronize extends LifecycleService {
} }
private void queue_reload() { private void queue_reload() {
if (running) if (running) {
queued++;
lifecycle.submit(new Runnable() { lifecycle.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
stop(); stop();
start(); start();
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} finally {
queued--;
}
} }
}); });
} }
}
private void queue_start() { private void queue_start() {
if (!running) { if (!running) {
running = true; running = true;
queued++;
lifecycle.submit(new Runnable() { lifecycle.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
start(); start();
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} finally {
queued--;
}
} }
}); });
} }
@ -2207,12 +2235,25 @@ public class ServiceSynchronize extends LifecycleService {
private void queue_stop() { private void queue_stop() {
if (running) { if (running) {
running = false; running = false;
queued++;
lifecycle.submit(new Runnable() { lifecycle.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
try {
stop(); stop();
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} finally {
if (--queued == 0) {
try {
Thread.sleep(3000);
} catch (InterruptedException ignored) {
}
if (queued == 0)
stopSelf(); stopSelf();
} }
}
}
}); });
} }
} }

Loading…
Cancel
Save