Shutdown service executor

This commit is contained in:
M66B 2019-03-25 14:05:52 +00:00
parent 795b50ba5d
commit 7473697ac1
1 changed files with 52 additions and 50 deletions

View File

@ -51,6 +51,7 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import javax.mail.FetchProfile; import javax.mail.FetchProfile;
import javax.mail.Folder; import javax.mail.Folder;
@ -155,16 +156,11 @@ public class ServiceSynchronize extends LifecycleService {
@Override @Override
public void onDestroy() { public void onDestroy() {
Log.i("Service destroy"); Log.i("Service destroy");
EntityLog.log(this, "Service destroy");
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
cm.unregisterNetworkCallback(networkCallback); cm.unregisterNetworkCallback(networkCallback);
synchronized (this) {
EntityLog.log(this, "Service destroy");
if (started)
queue_reload(false, "service destroy");
}
Widget.update(this, -1); Widget.update(this, -1);
WorkerCleanup.cancel(); WorkerCleanup.cancel();
@ -305,58 +301,64 @@ public class ServiceSynchronize extends LifecycleService {
started = doStart; started = doStart;
queued++; try {
queue.submit(new Runnable() { queued++;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); queue.submit(new Runnable() {
PowerManager.WakeLock wl = pm.newWakeLock( PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":manage"); PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":manage");
@Override @Override
public void run() { public void run() {
DB db = DB.getInstance(ServiceSynchronize.this); DB db = DB.getInstance(ServiceSynchronize.this);
try { try {
wl.acquire(); wl.acquire();
EntityLog.log(ServiceSynchronize.this, "Reload" + EntityLog.log(ServiceSynchronize.this, "Reload" +
" stop=" + doStop + " start=" + doStart + " queued=" + queued + " " + reason); " stop=" + doStop + " start=" + doStart + " queued=" + queued + " " + reason);
if (doStop) if (doStop)
stop(); stop();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
for (EntityAccount account : db.account().getAccountsTbd()) for (EntityAccount account : db.account().getAccountsTbd())
nm.deleteNotificationChannel(EntityAccount.getNotificationChannelId(account.id)); nm.deleteNotificationChannel(EntityAccount.getNotificationChannelId(account.id));
}
int accounts = db.account().deleteAccountsTbd();
int identities = db.identity().deleteIdentitiesTbd();
if (accounts > 0 || identities > 0)
Log.i("Deleted accounts=" + accounts + " identities=" + identities);
if (doStart)
start();
} catch (Throwable ex) {
Log.e(ex);
} finally {
queued--;
EntityLog.log(ServiceSynchronize.this, "Reload done queued=" + queued);
if (queued == 0 && !isEnabled()) {
try {
Thread.sleep(STOP_DELAY);
} catch (InterruptedException ignored) {
} }
if (queued == 0 && !isEnabled())
stopService();
}
wl.release(); int accounts = db.account().deleteAccountsTbd();
int identities = db.identity().deleteIdentitiesTbd();
if (accounts > 0 || identities > 0)
Log.i("Deleted accounts=" + accounts + " identities=" + identities);
if (doStart)
start();
} catch (Throwable ex) {
Log.e(ex);
} finally {
queued--;
EntityLog.log(ServiceSynchronize.this, "Reload done queued=" + queued);
if (!doStart && queued == 0 && !isEnabled()) {
try {
Thread.sleep(STOP_DELAY);
} catch (InterruptedException ignored) {
}
if (!doStart && queued == 0 && !isEnabled()) {
queue.shutdownNow();
stopService();
}
}
wl.release();
}
} }
} });
}); } catch (RejectedExecutionException ex) {
Log.w(ex);
}
} }
private boolean isEnabled() { private boolean isEnabled() {