Improved task logging

This commit is contained in:
M66B 2021-06-18 13:23:57 +02:00
parent 0a8d831bdc
commit 13d52b806e
1 changed files with 31 additions and 21 deletions

View File

@ -56,6 +56,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private long started; private long started;
private boolean reported; private boolean reported;
private boolean interrupted; private boolean interrupted;
private Lifecycle.State state;
private Future<?> future; private Future<?> future;
private ExecutorService localExecutor; private ExecutorService localExecutor;
@ -181,7 +182,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
ApplicationEx.getMainHandler().post(new Runnable() { ApplicationEx.getMainHandler().post(new Runnable() {
@Override @Override
public void run() { public void run() {
Lifecycle.State state = owner.getLifecycle().getCurrentState(); state = owner.getLifecycle().getCurrentState();
if (state.equals(Lifecycle.State.DESTROYED)) { if (state.equals(Lifecycle.State.DESTROYED)) {
// No delivery // No delivery
cleanup(context); cleanup(context);
@ -195,7 +196,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
owner.getLifecycle().addObserver(new LifecycleObserver() { owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY) @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() { public void onAny() {
Lifecycle.State state = owner.getLifecycle().getCurrentState(); state = owner.getLifecycle().getCurrentState();
if (state.equals(Lifecycle.State.DESTROYED)) { if (state.equals(Lifecycle.State.DESTROYED)) {
Log.i("Destroyed task " + name); Log.i("Destroyed task " + name);
owner.getLifecycle().removeObserver(this); owner.getLifecycle().removeObserver(this);
@ -275,22 +276,26 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
private void updateTaskCount(Context context) { private void updateTaskCount(Context context) {
// Check tasks
long now = new Date().getTime(); long now = new Date().getTime();
synchronized (tasks) { synchronized (tasks) {
for (SimpleTask task : tasks) { for (SimpleTask task : tasks)
long elapsed = now - task.started; if (!BuildConfig.PLAY_STORE_RELEASE ||
if (elapsed > CANCEL_AFTER && !task.interrupted && task.started > 0) { (task.future != null && !task.future.isDone())) {
task.interrupted = true; long elapsed = now - task.started;
if (task.future != null && !task.future.isDone()) if (elapsed > CANCEL_AFTER && !task.interrupted) {
task.future.cancel(true); task.interrupted = true;
} else if (elapsed > REPORT_AFTER && !task.reported) { if (task.future != null && !task.future.isDone()) {
task.reported = true; Log.e("Interrupting task " + task +
Log.e("Long running task " + task.name + " tasks=" + getCountLocked() + "/" + tasks.size());
" elapsed=" + (task.started == 0 ? null : elapsed / 1000) + task.future.cancel(true);
" done=" + (task.future == null ? null : task.future.isDone()) + }
" cancelled=" + (task.future == null ? null : task.future.isCancelled())); } else if (elapsed > REPORT_AFTER && !task.reported) {
task.reported = true;
Log.e("Long running task " + task +
" tasks=" + getCountLocked() + "/" + tasks.size());
}
} }
}
} }
int executing = getCount(); int executing = getCount();
@ -319,17 +324,22 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
return name + return name +
" elapsed=" + (started == 0 ? null : elapsed / 1000) + " elapsed=" + (started == 0 ? null : elapsed / 1000) +
" done=" + (future == null ? null : future.isDone()) + " done=" + (future == null ? null : future.isDone()) +
" cancelled=" + (future == null ? null : future.isCancelled()); " cancelled=" + (future == null ? null : future.isCancelled() +
" state=" + state);
} }
static int getCount() { static int getCount() {
int executing = 0;
synchronized (tasks) { synchronized (tasks) {
for (SimpleTask task : tasks) return getCountLocked();
if (task.count &&
task.future != null && !task.future.isDone())
executing++;
} }
}
private static int getCountLocked() {
int executing = 0;
for (SimpleTask task : tasks)
if (task.count &&
task.future != null && !task.future.isDone())
executing++;
return executing; return executing;
} }