Added task monitoring

pull/199/head
M66B 3 years ago
parent fbfa16eb73
commit 68be0cb204

@ -1542,11 +1542,12 @@ public class Log {
attachNetworkInfo(context, draft.id, 3); attachNetworkInfo(context, draft.id, 3);
attachLog(context, draft.id, 4); attachLog(context, draft.id, 4);
attachOperations(context, draft.id, 5); attachOperations(context, draft.id, 5);
attachLogcat(context, draft.id, 6); attachTasks(context, draft.id, 6);
attachLogcat(context, draft.id, 7);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
attachNotificationInfo(context, draft.id, 7); attachNotificationInfo(context, draft.id, 8);
//if (MessageClassifier.isEnabled(context)) //if (MessageClassifier.isEnabled(context))
// attachClassifierData(context, draft.id, 8); // attachClassifierData(context, draft.id, 9);
EntityOperation.queue(context, draft, EntityOperation.ADD); EntityOperation.queue(context, draft, EntityOperation.ADD);
@ -2062,6 +2063,30 @@ public class Log {
db.attachment().setDownloaded(attachment.id, size); db.attachment().setDownloaded(attachment.id, size);
} }
private static void attachTasks(Context context, long id, int sequence) throws IOException {
DB db = DB.getInstance(context);
EntityAttachment attachment = new EntityAttachment();
attachment.message = id;
attachment.sequence = sequence;
attachment.name = "tasks.txt";
attachment.type = "text/plain";
attachment.disposition = Part.ATTACHMENT;
attachment.size = null;
attachment.progress = 0;
attachment.id = db.attachment().insertAttachment(attachment);
long size = 0;
File file = attachment.getFile(context);
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
for (SimpleTask task : SimpleTask.getList()) {
size += write(os, String.format("%s\r\n", task.toString()));
}
}
db.attachment().setDownloaded(attachment.id, size);
}
private static void attachLogcat(Context context, long id, int sequence) throws IOException { private static void attachLogcat(Context context, long id, int sequence) throws IOException {
DB db = DB.getInstance(context); DB db = DB.getInstance(context);

@ -53,6 +53,10 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private boolean count = true; private boolean count = true;
private String name; private String name;
private long created;
private long started;
private boolean reported;
private boolean interrupted;
private Future<?> future; private Future<?> future;
private ExecutorService localExecutor; private ExecutorService localExecutor;
@ -60,8 +64,16 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private static ExecutorService globalExecutor = null; private static ExecutorService globalExecutor = null;
private static final List<SimpleTask> tasks = new ArrayList<>(); private static final List<SimpleTask> tasks = new ArrayList<>();
private static final int MAX_WAKELOCK = 30 * 60 * 1000; // milliseconds
private static final int REPORT_AFTER = 5 * 60 * 1000; // milliseconds
private static final int CANCEL_AFTER = MAX_WAKELOCK; // milliseconds
static final String ACTION_TASK_COUNT = BuildConfig.APPLICATION_ID + ".ACTION_TASK_COUNT"; static final String ACTION_TASK_COUNT = BuildConfig.APPLICATION_ID + ".ACTION_TASK_COUNT";
public SimpleTask() {
created = new Date().getTime();
}
public SimpleTask<T> setLog(boolean log) { public SimpleTask<T> setLog(boolean log) {
this.log = log; this.log = log;
if (!log) if (!log)
@ -121,6 +133,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
private void run(final Context context, final LifecycleOwner owner, final Bundle args, final String name) { private void run(final Context context, final LifecycleOwner owner, final Bundle args, final String name) {
this.name = name; this.name = name;
this.started = new Date().getTime();
if (owner instanceof TwoStateOwner) if (owner instanceof TwoStateOwner)
Log.e(new Throwable("SimpleTask/TwoStateOwner")); Log.e(new Throwable("SimpleTask/TwoStateOwner"));
@ -152,7 +165,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
public void run() { public void run() {
// Run in background thread // Run in background thread
try { try {
wl.acquire(); wl.acquire(MAX_WAKELOCK);
if (log) if (log)
Log.i("Executing task=" + name); Log.i("Executing task=" + name);
@ -264,6 +277,26 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
private void updateTaskCount(Context context) { private void updateTaskCount(Context context) {
long now = new Date().getTime();
synchronized (tasks) {
for (SimpleTask task : tasks) {
long pending = now - task.created;
long elapsed = now - task.started;
if (elapsed > CANCEL_AFTER && !task.interrupted && task.started > 0) {
task.interrupted = true;
if (task.future != null && !task.future.isDone())
task.future.cancel(true);
} else if (pending > REPORT_AFTER && !task.reported) {
task.reported = true;
Log.e("Long running task " + task.name +
" pending=" + (pending / 1000) +
" elapsed=" + (elapsed / 1000) +
" done=" + (task.future == null ? null : task.future.isDone()) +
" cancelled=" + (task.future == null ? null : task.future.isCancelled()));
}
}
}
int executing = getCount(); int executing = getCount();
Log.i("Remaining tasks=" + executing + "/" + tasks.size()); Log.i("Remaining tasks=" + executing + "/" + tasks.size());
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
@ -283,6 +316,16 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
protected void onPostExecute(Bundle args) { protected void onPostExecute(Bundle args) {
} }
@Override
public String toString() {
long now = new Date().getTime();
long pending = now - created;
long elapsed = now - started;
return name +
" pending=" + (pending / 1000) + "s" +
" elapsed=" + (elapsed / 1000) + "s";
}
static int getCount() { static int getCount() {
int executing = 0; int executing = 0;
synchronized (tasks) { synchronized (tasks) {
@ -292,4 +335,10 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
} }
return executing; return executing;
} }
static List<SimpleTask> getList() {
synchronized (tasks) {
return new ArrayList<>(tasks);
}
}
} }

Loading…
Cancel
Save