Added SimpleTask.isAlive

This commit is contained in:
M66B 2022-04-24 10:37:47 +02:00
parent 491b3cec18
commit 842bf19dd2
3 changed files with 23 additions and 23 deletions

View File

@ -71,6 +71,12 @@ public class CoalMine {
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())) {
HeapField hfState = instance.get(className, "state");
if (hfState != null) {

View File

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

View File

@ -273,14 +273,6 @@ public class ViewModelMessages extends ViewModel {
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);
//model.list.getValue().loadAround(lpos);
model.list.observe(owner, new Observer<PagedList<TupleMessageEx>>() {
@ -354,6 +346,9 @@ public class ViewModelMessages extends ViewModel {
long id = args.getLong("id");
int lpos = args.getInt("lpos");
if (!isAlive())
return null;
PagedList<TupleMessageEx> plist = model.list.getValue();
if (plist == null)
return null;
@ -372,7 +367,7 @@ public class ViewModelMessages extends ViewModel {
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" +
" range=" + i + "/#" + count);
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);
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) {
List<Long> ids = new ArrayList<>();
if (!isAlive())
return ids;
PagedList<TupleMessageEx> plist = model.list.getValue();
if (plist == null)
return ids;
LimitOffsetDataSource<TupleMessageEx> ds = (LimitOffsetDataSource<TupleMessageEx>) plist.getDataSource();
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)))
if ((message.uid != null && !message.folderReadOnly) ||
message.accountProtocol != EntityAccount.TYPE_IMAP)