Added SimpleTask.isAlive

pull/207/head
M66B 3 years ago
parent 491b3cec18
commit 842bf19dd2

@ -71,6 +71,12 @@ public class CoalMine {
reporter.getLabels().add("started=" + label); reporter.getLabels().add("started=" + label);
} }
} }
HeapField hfDestroyed = instance.get(className, "destroyed");
if (hfDestroyed != null) {
Boolean destroyed = hfDestroyed.getValue().getAsBoolean();
if (destroyed != null)
reporter.getLabels().add("destroyed=" + destroyed);
}
} else if (className.equals(TwoStateOwner.class.getName())) { } else if (className.equals(TwoStateOwner.class.getName())) {
HeapField hfState = instance.get(className, "state"); HeapField hfState = instance.get(className, "state");
if (hfState != null) { if (hfState != null) {

@ -49,10 +49,10 @@ import java.util.concurrent.Future;
public abstract class SimpleTask<T> implements LifecycleObserver { public abstract class SimpleTask<T> implements LifecycleObserver {
private boolean log = true; private boolean log = true;
private boolean count = true; private boolean count = true;
private boolean optional = false;
private String name; private String name;
private long started; private long started;
private boolean destroyed;
private boolean reported; private boolean reported;
private Lifecycle.State state; private Lifecycle.State state;
private Future<?> future; private Future<?> future;
@ -79,11 +79,6 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
return this; return this;
} }
public SimpleTask<T> setOptional(boolean optional) {
this.optional = optional;
return this;
}
public SimpleTask<T> setExecutor(ExecutorService executor) { public SimpleTask<T> setExecutor(ExecutorService executor) {
this.localExecutor = executor; this.localExecutor = executor;
return this; return this;
@ -153,8 +148,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
LifecycleObserver watcher = new LifecycleObserver() { LifecycleObserver watcher = new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() { public void onDestroy() {
EntityLog.log(context, EntityLog.Type.Debug, "Cancelling task=" + name); destroyed = true;
cancel(context);
owner.getLifecycle().removeObserver(this); owner.getLifecycle().removeObserver(this);
} }
}; };
@ -197,8 +191,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
// No delivery // No delivery
cleanup(context); cleanup(context);
} else { } else {
if (optional) owner.getLifecycle().removeObserver(watcher);
owner.getLifecycle().removeObserver(watcher);
if (state.isAtLeast(Lifecycle.State.RESUMED)) { if (state.isAtLeast(Lifecycle.State.RESUMED)) {
// Inline delivery // Inline delivery
@ -269,12 +262,15 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
}); });
if (optional) owner.getLifecycle().addObserver(watcher);
owner.getLifecycle().addObserver(watcher);
updateTaskCount(context); updateTaskCount(context);
} }
public boolean isAlive() {
return !this.destroyed;
}
void cancel(Context context) { void cancel(Context context) {
if (future != null && future.cancel(false)) { if (future != null && future.cancel(false)) {
Log.i("Cancelled task=" + name); Log.i("Cancelled task=" + name);

@ -273,14 +273,6 @@ public class ViewModelMessages extends ViewModel {
return; return;
} }
ObjectHolder<Boolean> alive = new ObjectHolder<>(true);
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
public void onAny() {
alive.value = owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED);
}
});
Log.i("Observe previous/next id=" + id); Log.i("Observe previous/next id=" + id);
//model.list.getValue().loadAround(lpos); //model.list.getValue().loadAround(lpos);
model.list.observe(owner, new Observer<PagedList<TupleMessageEx>>() { model.list.observe(owner, new Observer<PagedList<TupleMessageEx>>() {
@ -354,6 +346,9 @@ public class ViewModelMessages extends ViewModel {
long id = args.getLong("id"); long id = args.getLong("id");
int lpos = args.getInt("lpos"); int lpos = args.getInt("lpos");
if (!isAlive())
return null;
PagedList<TupleMessageEx> plist = model.list.getValue(); PagedList<TupleMessageEx> plist = model.list.getValue();
if (plist == null) if (plist == null)
return null; return null;
@ -372,7 +367,7 @@ public class ViewModelMessages extends ViewModel {
return getPair(plist, ds, count, from + j); return getPair(plist, ds, count, from + j);
} }
for (int i = 0; i < count && alive.value; i += CHUNK_SIZE) { for (int i = 0; i < count && isAlive(); i += CHUNK_SIZE) {
Log.i("Observe previous/next load" + Log.i("Observe previous/next load" +
" range=" + i + "/#" + count); " range=" + i + "/#" + count);
List<TupleMessageEx> messages = ds.loadRange(i, Math.min(CHUNK_SIZE, count - i)); List<TupleMessageEx> messages = ds.loadRange(i, Math.min(CHUNK_SIZE, count - i));
@ -431,7 +426,7 @@ public class ViewModelMessages extends ViewModel {
Log.i("Observe previous/next fallback=" + result); Log.i("Observe previous/next fallback=" + result);
return result; return result;
} }
}.setOptional(true).execute(context, owner, args, "model:fallback"); }.execute(context, owner, args, "model:fallback");
} }
}); });
} }
@ -449,13 +444,16 @@ public class ViewModelMessages extends ViewModel {
protected List<Long> onExecute(Context context, Bundle args) { protected List<Long> onExecute(Context context, Bundle args) {
List<Long> ids = new ArrayList<>(); List<Long> ids = new ArrayList<>();
if (!isAlive())
return ids;
PagedList<TupleMessageEx> plist = model.list.getValue(); PagedList<TupleMessageEx> plist = model.list.getValue();
if (plist == null) if (plist == null)
return ids; return ids;
LimitOffsetDataSource<TupleMessageEx> ds = (LimitOffsetDataSource<TupleMessageEx>) plist.getDataSource(); LimitOffsetDataSource<TupleMessageEx> ds = (LimitOffsetDataSource<TupleMessageEx>) plist.getDataSource();
int count = ds.countItems(); int count = ds.countItems();
for (int i = 0; i < count; i += 100) for (int i = 0; i < count && isAlive(); i += 100)
for (TupleMessageEx message : ds.loadRange(i, Math.min(100, count - i))) for (TupleMessageEx message : ds.loadRange(i, Math.min(100, count - i)))
if ((message.uid != null && !message.folderReadOnly) || if ((message.uid != null && !message.folderReadOnly) ||
message.accountProtocol != EntityAccount.TYPE_IMAP) message.accountProtocol != EntityAccount.TYPE_IMAP)

Loading…
Cancel
Save