feat(android): add diagnostic logging for prefetch scheduling

- Add comprehensive logging to scheduleBackgroundFetch method
  - Log scheduledTime and currentTime for comparison
  - Log calculated fetch time and delay in ms, hours, and minutes
  - Log detailed timing information for future vs past fetch times
  - Add fallback path logging for immediate fetch scenarios

- Add logging to scheduleDailyNotification callback
  - Log scheduled notification result with content details
  - Log when scheduleBackgroundFetch is called
  - Add error logging when notification scheduling fails

- Add WorkManager status logging in DailyNotificationFetcher
  - Log work ID when work is enqueued
  - Log detailed timing information (delay_ms, delay_hours)
  - Add past time detection with duration logging
  - Improve immediate fetch fallback logging

- Add prefetch scheduling trace documentation
  - Document complete code flow from notification to prefetch
  - Include debugging checklist and log search patterns
  - Add ADB commands for troubleshooting

These changes enable better debugging of prefetch scheduling issues
by providing detailed timing and execution information at every
decision point in the prefetch scheduling flow.
This commit is contained in:
Matthew Raymer
2025-10-27 12:40:04 +00:00
parent b724eb716f
commit 0e783a8a2d
4 changed files with 285 additions and 7 deletions

View File

@@ -82,6 +82,8 @@ public class DailyNotificationFetcher {
long fetchTime = scheduledTime - TimeUnit.MINUTES.toMillis(5);
if (fetchTime > System.currentTimeMillis()) {
long delayMs = fetchTime - System.currentTimeMillis();
// Create work data
Data inputData = new Data.Builder()
.putLong("scheduled_time", scheduledTime)
@@ -94,16 +96,22 @@ public class DailyNotificationFetcher {
DailyNotificationFetchWorker.class)
.setInputData(inputData)
.addTag(WORK_TAG_FETCH)
.setInitialDelay(fetchTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS)
.setInitialDelay(delayMs, TimeUnit.MILLISECONDS)
.build();
// Enqueue the work
workManager.enqueue(fetchWork);
Log.i(TAG, "DN|WORK_ENQUEUED work_id=" + fetchWork.getId().toString() +
" fetch_at=" + fetchTime +
" delay_ms=" + delayMs +
" delay_hours=" + (delayMs / 3600000.0));
Log.i(TAG, "Background fetch scheduled successfully");
} else {
Log.w(TAG, "Fetch time has already passed, scheduling immediate fetch");
Log.w(TAG, "DN|FETCH_PAST_TIME fetch_time=" + fetchTime +
" current=" + System.currentTimeMillis() +
" past_by_ms=" + (System.currentTimeMillis() - fetchTime));
scheduleImmediateFetch();
}

View File

@@ -652,8 +652,14 @@ public class DailyNotificationPlugin extends Plugin {
// Schedule the notification
boolean scheduled = scheduler.scheduleNotification(content);
Log.d(TAG, "DN|SCHEDULE_RESULT scheduled=" + scheduled +
" content_id=" + content.getId() +
" content_scheduled_time=" + content.getScheduledTime());
if (scheduled) {
Log.i(TAG, "DN|SCHEDULE_CALLBACK scheduled=true, calling scheduleBackgroundFetch");
Log.d(TAG, "DN|SCHEDULE_CALLBACK content.getScheduledTime()=" + content.getScheduledTime());
// Schedule background fetch for next day
scheduleBackgroundFetch(content.getScheduledTime());
@@ -664,6 +670,7 @@ public class DailyNotificationPlugin extends Plugin {
call.resolve();
} else {
Log.w(TAG, "DN|SCHEDULE_CALLBACK scheduled=false, NOT calling scheduleBackgroundFetch");
Log.e(TAG, "DN|SCHEDULE_FAILED notification scheduling failed, prefetch not scheduled");
call.reject("Failed to schedule notification");
}
@@ -960,17 +967,29 @@ public class DailyNotificationPlugin extends Plugin {
*/
private void scheduleBackgroundFetch(long scheduledTime) {
try {
Log.d(TAG, "DN|SCHEDULE_FETCH_START time=" + scheduledTime);
Log.i(TAG, "DN|SCHEDULE_FETCH_START time=" + scheduledTime + " current=" + System.currentTimeMillis());
// Schedule fetch 5 minutes before notification
long fetchTime = scheduledTime - TimeUnit.MINUTES.toMillis(5);
long currentTime = System.currentTimeMillis();
if (fetchTime > System.currentTimeMillis()) {
Log.d(TAG, "DN|SCHEDULE_FETCH_CALC fetch_at=" + fetchTime + " notification_at=" + scheduledTime);
Log.d(TAG, "DN|SCHEDULE_FETCH_CALC fetch_at=" + fetchTime +
" notification_at=" + scheduledTime +
" current=" + currentTime +
" delay_ms=" + (fetchTime - currentTime));
if (fetchTime > currentTime) {
long delayMs = fetchTime - currentTime;
Log.d(TAG, "DN|SCHEDULE_FETCH_FUTURE delay_hours=" + (delayMs / 3600000.0) +
" delay_minutes=" + (delayMs / 60000.0));
fetcher.scheduleFetch(fetchTime);
Log.i(TAG, "DN|SCHEDULE_FETCH_OK Background fetch scheduled for " + fetchTime + " (5 minutes before notification at " + scheduledTime + ")");
} else {
Log.w(TAG, "DN|SCHEDULE_FETCH_PAST fetch_time=" + fetchTime + " current=" + System.currentTimeMillis());
Log.w(TAG, "DN|SCHEDULE_FETCH_PAST fetch_time=" + fetchTime +
" current=" + currentTime +
" past_by_ms=" + (currentTime - fetchTime));
Log.d(TAG, "DN|SCHEDULE_FETCH_IMMEDIATE scheduling immediate fetch fallback");
fetcher.scheduleImmediateFetch();
}
} catch (Exception e) {
Log.e(TAG, "DN|SCHEDULE_FETCH_ERR Error scheduling background fetch", e);