Drive-In: check its bookings first in each round, not last

Build 83's four-hour log showed the Drive-In's check of its next two days
paced out in every round: it ran after the channels had used the hour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 18:19:34 +00:00
co-authored by Claude Opus 5.5
parent dfeed3babc
commit e55ec5dd21
3 changed files with 33 additions and 4 deletions
@@ -298,10 +298,17 @@ internal class ChannelScheduler(private val store: ScheduleStore,
checks.values.groupingBy { it.reason }.eachCount().entries.joinToString(" ") { "${it.key.replace(' ', '_')}=${it.value}" })
}
fun topUpDriveIn(userId: String, session: EmbySampleSession? = null): ChannelPlan? {
/**
* The Drive-In's check of its next two days, run first in each background round (Paul, 30 November 2026: run last, it
* was paced out in every round of build 83's four-hour log, after the channels had used the hour). Null if it couldn't run.
*/
fun refileDriveInAhead(userId: String, session: EmbySampleSession): ChannelPlan? =
runCatching { refileDriveIn(userId, session, clock()) }.onFailure { Log.w("OrbitSchedule", "channel=$DRIVE_IN_NUMBER refileAhead failed=${it.javaClass.simpleName}") }.getOrNull()
/** [refile] false when the round has already checked the Drive-In's bookings ([refileDriveInAhead]). */
fun topUpDriveIn(userId: String, session: EmbySampleSession? = null, refile: Boolean = true): ChannelPlan? {
val now = clock()
val existing = session?.let { s -> runCatching { refileDriveIn(userId, s, now) }.onFailure { Log.w("OrbitSchedule", "channel=$DRIVE_IN_NUMBER refileAhead failed=${it.javaClass.simpleName}") }.getOrNull() }
?: store.loadPlan(userId, DRIVE_IN_NUMBER)
val existing = session?.takeIf { refile }?.let { refileDriveInAhead(userId, it) } ?: store.loadPlan(userId, DRIVE_IN_NUMBER)
if (driveInBookedAhead(existing, now)) return existing
val failed = store.loadFailed(userId)
fun films(numbers: List<Int>) = numbers.flatMap { number -> withoutFailed(store.loadPool(userId, number)?.series.orEmpty().filter { it.isFilm }, failed) }.distinctBy { seriesKey(it.title) }
@@ -441,6 +441,10 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
BackgroundPacing.budget = BackgroundPacing.share(if (quiet) QUIET_START_BUDGET else BackgroundPacing.HOURLY_BUDGET)
// Which lists have a refresh to do, so each gets a fair share of the hour (26 November 2026: 22 lists starved).
withContext(Dispatchers.IO) { runCatching { scheduler.beginRound(active.accountId, SmartChannels) } }
// The Drive-In's bookings checked first, so the channels' reads can't use up the hour before it (30 November 2026).
try { withContext(Dispatchers.IO) { BackgroundPacing.run { scheduler.refileDriveInAhead(active.accountId, active) } }?.let { driveInPlan = it } }
catch (cancelled: CancellationException) { throw cancelled }
catch (_: Exception) { /* Checked again at tuning, and next round. */ }
for (programming in SmartChannels) {
val loop = smartLoop(programming) ?: run {
// A channel of your own that is switched on but whose first plan never finished (an update arrived mid-read,
@@ -462,7 +466,7 @@ internal fun EmbyConnectionScreen(modifier: Modifier = Modifier, visible: Boolea
} catch (cancelled: CancellationException) { throw cancelled }
catch (_: Exception) { /* Tried again next hour. */ }
}
try { driveInPlan = withContext(Dispatchers.IO) { BackgroundPacing.run { scheduler.topUpDriveIn(active.accountId, active) } } }
try { driveInPlan = withContext(Dispatchers.IO) { BackgroundPacing.run { scheduler.topUpDriveIn(active.accountId, active, refile = false) } } }
catch (cancelled: CancellationException) { throw cancelled }
catch (_: Exception) { /* Tried again next hour. */ }
// Stage C's light daily library refresh, in the background. A failure changes nothing and is tried next hour.
@@ -58,4 +58,22 @@ class DriveInRefileTest {
// One request to check both, one search for the gone film.
assertEquals(2, asked.size)
}
/** Build 83's log (30 November 2026): checked last, the Drive-In was paced out every round; the round now checks it first, once. */
@Test fun theRoundChecksTheDriveInOnceAndFirst() {
val now = 1_795_000_000_000L
val batman = SampleItem("Batman Returns", "Movie", 126, "", "4282684", 126 * 60_000L)
val plan = ChannelPlan(listOf(ScheduleEntry("4282684", EpisodeMark(1, 1), ScheduledMedia(batman, "mediasource_4282684", 126 * 60_000L), now + 3_600_000L)), ScheduleCursor())
val store = ScheduleStore(folder.newFolder())
store.savePlan("fixture-user", DRIVE_IN_NUMBER, plan)
val checks = mutableListOf<String>()
val transport = EmbyTransport { r -> if ("&Ids=" in r.path) { checks += r.path; EmbyReply(200, JSONObject().put("Items", org.json.JSONArray().put(JSONObject().put("Id", "4282684"))).toString()) } else EmbyReply(500, "") }
val session = EmbySampleSession(transport, "fixture-token-private", "fixture-user", EmbySample(listOf(batman), 1))
val scheduler = ChannelScheduler(store, copies = { null }, clock = { now })
assertEquals(plan.entries, scheduler.refileDriveInAhead("fixture-user", session)!!.entries)
assertEquals(1, checks.size)
// The top-up at the end of the round doesn't check again.
assertEquals(plan.entries, scheduler.topUpDriveIn("fixture-user", session, refile = false)!!.entries)
assertEquals(1, checks.size)
}
}