gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3ed3a0ef3ed3a0efmission 042: gates old/new equal (browser #41 route-table regex accepts unescaped hl:web output), real-data TTFB, E2E blocked by lisbeth lockmre3ed3a0ef/lib/jobs.hl

47.0 KB

  1. // lib/jobs.hl — THE BACKGROUND JOBS (mission 028: out of project.hl, unchanged): the daily TMDB sync run, the adult and kind
  2. // backfills, the details repair, the credits job, the duplicate merge, the short-id backfill and the collection seed — each with
  3. // its queue, its pause, its log lines, and the clocks that drive them (which job runs when, which one waits for which) — and, last,
  4. // the curated franchise seed (tracker#40). The work of ONE step is the topic's own (sync.hl, backfill.hl, details.hl, credits.hl,
  5. // merge.hl, shortids.hl, collections.hl, franchiseseed.hl).
  6. // A CLASS, not statics: its state (queues, positions, totals) is rewritten on every step, and a static cannot be reassigned —
  7. // project.hl makes the one instance (`jobs = new Jobs()`) after the server, as these lines ran there before; constructing it
  8. // starts the clocks and logs which jobs are off.
  9. import { env } from 'hl:proc'
  10. import { readFile, exists } from 'hl:fs'
  11. import { now, timestamp, every } from 'hl:time'
  12. import { syncEnabled, pauseMsAfter, msPerRequest } from './tmdb.hl'
  13. import { persistSync } from './sync.hl'
  14. import { emptyTotals, addTotals, totalsLine } from './sync-helpers.hl'
  15. import { planStart, planStep, planIds, planLine, markSynced, syncRunStep } from './dailysync.hl'
  16. import { adultBackfillQueue, backfillTitle, kindBackfillQueue, kindTitle, tvmazeStatusQueue, tvmazeStatusTitle, artQueue, artTitle } from './backfill.hl'
  17. import { photoQueue, photoStep, persistPhotos } from './people.hl'
  18. import { detailsRepairQueue, completeStep, persistDetails, applySynced } from './details.hl'
  19. import { persistCredits, creditsQueue, creditsStep } from './credits.hl'
  20. import { allFollowedShowIds } from './follows.hl'
  21. import { refreshCatalogShow } from './catalog.hl'
  22. import { searchTitleChanged, searchTitleRenamed } from './search.hl'
  23. import { mergePrepare, mergeStep, mergeStats, mergeLine, persistMerge } from './merge.hl'
  24. import { shortIdStep, shortIdStats, persistShortIds, shortIdsLeft } from './shortids.hl'
  25. import { seedPrepare, seedStep, seedLine, seedPersist } from './collections.hl'
  26. import { franchiseSeedPrepare, franchiseSeedStep, franchiseSeedLine, franchiseSeedStats } from './franchiseseed.hl'
  27. // ---- THE DAILY TMDB SYNC (tracker.worldapi.org#9, sync.hl; tracker#18, dailysync.hl) -------------------------
  28. // Once a day at TRACKER_SYNC_HOUR (UTC, default 4 — hl:time has no time zones; the container runs UTC). hl:web serves ONE
  29. // request at a time, so the run never takes the server for itself: a 1-second clock does ONE step per tick and returns — the
  30. // requests that queued meanwhile are served between two ticks, and a tick never starts inside a page render.
  31. // tracker#18: the run first asks TMDB's and TVmaze's CHANGE LISTS since the last run (dailysync.hl — one list page per step)
  32. // and then syncs only the titles of ours that changed, followed ones first — every title stays current, not only the followed
  33. // ones. FULL WALK (every followed show, as before #18) on the first run, after a gap > 14 days, or when a change list fails.
  34. // A title's step: sync.hl syncShow (1–3 TMDB requests + maybe its poster, TVmaze) + details.hl applySynced (the record's
  35. // title, overview, tagline … and cast/crew from the same details answer). After a step that made n requests the next waits
  36. // n × 260 ms (TMDB: ≤ 40 requests / 10 s) + 550 ms per TVmaze request (≤ 20 / 10 s, tracker.worldapi.org#12).
  37. // The finished run stores its day (dailysync.hl markSynced). Log: "tmdb sync: start, …", the change lists ("tmdb sync:
  38. // changes since …"), errors, slow steps, "tmdb sync done: mode=… changeRequests=… <totals>".
  39. // TRACKER_SYNC=0 switches it off; without TMDB_READ_TOKEN it is off too (logged once).
  40. syncHour = env('TRACKER_SYNC_HOUR') != null ? toNumber(env('TRACKER_SYNC_HOUR')) : 4
  41. syncOn = env('TRACKER_SYNC') != '0' && syncEnabled
  42. syncQueue = []
  43. syncPlanning = false
  44. syncMode = ''
  45. syncMark = false
  46. syncChangeRequests = 0
  47. syncAt = 0
  48. syncDay = ''
  49. syncNextAt = 0
  50. syncTotals = emptyTotals()
  51. syncFinish = () => {
  52. persistDetails()
  53. if (syncMark) { markSynced(syncDay) }
  54. console.log('tmdb sync done: mode=' + syncMode + ' changeRequests=' + syncChangeRequests + ' ' + totalsLine(syncTotals) + ' seconds=' + ((now() - syncAt) / 1000))
  55. return null
  56. }
  57. syncTick = () => {
  58. t = now()
  59. if (syncQueue.length == 0 && !syncPlanning) {
  60. stamp = timestamp(t)
  61. day = stamp.slice(0, 10)
  62. if (toNumber(stamp.slice(11, 13)) != syncHour || day == syncDay) { return null }
  63. syncDay = day
  64. syncAt = t
  65. syncNextAt = 0
  66. syncTotals = emptyTotals()
  67. syncChangeRequests = 0
  68. p = planStart(day, null)
  69. syncMode = p.mode
  70. syncMark = true
  71. if (p.mode == 'delta') {
  72. syncPlanning = true
  73. console.log('tmdb sync: start, changes ' + p.why)
  74. return null
  75. }
  76. syncQueue = allFollowedShowIds()
  77. console.log('tmdb sync: start, ' + syncQueue.length + ' followed shows, full walk (' + p.why + ')')
  78. if (syncQueue.length == 0) { syncFinish() }
  79. return null
  80. }
  81. if (t < syncNextAt) { return null }
  82. if (syncPlanning) {
  83. ps = planStep()
  84. syncChangeRequests = syncChangeRequests + ps.requests + ps.tvmazeRequests
  85. syncTotals.requests = syncTotals.requests + ps.requests
  86. syncTotals.tvmazeRequests = syncTotals.tvmazeRequests + ps.tvmazeRequests
  87. syncNextAt = now() + pauseMsAfter(ps)
  88. if (!ps.done) { return null }
  89. syncPlanning = false
  90. console.log('tmdb sync: ' + planLine())
  91. if (ps.failed != '') {
  92. // a change list could not be read: the old full walk, and the day is not advanced (the next run asks again)
  93. syncMode = 'full'
  94. syncMark = false
  95. syncQueue = allFollowedShowIds()
  96. console.log('tmdb sync: change list failed, full walk of ' + syncQueue.length + ' followed shows instead')
  97. } else {
  98. syncQueue = planIds()
  99. }
  100. if (syncQueue.length == 0) { syncFinish() }
  101. return null
  102. }
  103. id = syncQueue[0]
  104. rest = []
  105. let i = 1
  106. while (i < syncQueue.length) { rest.push(syncQueue[i]) i = i + 1 }
  107. syncQueue = rest
  108. // tracker#18: in a delta run a title nobody follows gets the light step (changed seasons, no TVmaze); tracker#35: one only on
  109. // TVmaze's change list its TVmaze status (dailysync.hl syncRunStep)
  110. r = syncRunStep(id, syncMode == 'delta')
  111. // tracker#18 + #28: the record's own fields (title, overview, tagline …) and the cast + crew from the same details answer
  112. if (r.details != null) {
  113. c = applySynced(id, r.details)
  114. if (c.error != '') { console.log('tmdb sync: ' + c.error) }
  115. if (c.renamed) { searchTitleRenamed(id) }
  116. }
  117. // the public lists (catalog.hl, tracker.worldapi.org#13): this show's place by its newest released episode; the sync
  118. // also brings TMDB's adult flag (mission 054) → the search's view of it too
  119. refreshCatalogShow(id)
  120. searchTitleChanged(id)
  121. took = now() - t
  122. syncTotals = addTotals(syncTotals, r)
  123. if (r.error != '') { console.log('tmdb sync: ' + r.error) }
  124. // a step blocks every request meanwhile (Saturday Night Live, 53 seasons, 4 requests: ~1.7 s) — say so in the log
  125. if (took > 1500) { console.log('tmdb sync: slow step, show ' + id + ', ' + r.requests + ' requests, ' + took + ' ms') }
  126. syncNextAt = now() + pauseMsAfter(r)
  127. if (syncQueue.length > 0 && syncTotals.shows % 10 == 0) { persistDetails() }
  128. if (syncQueue.length == 0) { syncFinish() }
  129. return null
  130. }
  131. // ---- THE ADULT-FLAG BACKFILL (mission 054, backfill.hl backfillTitle) -----------------------------------
  132. // The public lists and the search show only titles TMDB says are not adult (shows.hl isPublicTitle; unknown = hidden). At
  133. // start the app collects every title whose flag is still unknown and asks TMDB for ONE title per step — like the daily
  134. // sync, on the same clock, so requests queued meanwhile are served between two steps: one details request (adult,
  135. // poster_path, external ids) + the poster file when missing, then a pause of 260 ms per TMDB API request (≤ 40 / 10 s;
  136. // the poster comes from TMDB's image host, not the API, and is not counted). Each answered title goes into the lists
  137. // (catalog.hl refreshCatalogShow) and the search at once. RESUMES after a restart: the queue is built from what is
  138. // still unknown. Stops when the queue is empty; titles TMDB answers 404 for are marked (`adultCheck`) and stay hidden; a
  139. // passing failure (no answer, 429, 5xx) goes to the back of the queue, at most 2 more tries per start.
  140. // While the daily sync runs, the backfill waits. Log: start, every TRACKER_BACKFILL_LOG_EVERY (500) titles, done.
  141. // TRACKER_BACKFILL=0 switches it off; without TMDB_READ_TOKEN it is off too.
  142. backfillOn = env('TRACKER_BACKFILL') != '0' && syncEnabled
  143. backfillLogEvery = env('TRACKER_BACKFILL_LOG_EVERY') != null ? toNumber(env('TRACKER_BACKFILL_LOG_EVERY')) : 500
  144. backfillQueue = null
  145. backfillPos = 0
  146. backfillDone = false
  147. backfillAt = 0
  148. backfillNextAt = 0
  149. backfillTries = {}
  150. backfillTotals = { titles = 0 adult = 0 notAdult = 0 unknown = 0 posters = 0 ids = 0 requests = 0 errors = 0 retries = 0 }
  151. backfillLine = () => {
  152. b = backfillTotals
  153. return b.titles + '/' + backfillQueue.length + ' titles, adult=' + b.adult + ' notAdult=' + b.notAdult + ' unknown=' + b.unknown + ' posters=' + b.posters
  154. + ' ids=' + b.ids + ' requests=' + b.requests + ' errors=' + b.errors + ' retries=' + b.retries + ' seconds=' + ((now() - backfillAt) / 1000)
  155. }
  156. backfillTick = () => {
  157. if (backfillDone) { return null }
  158. t = now()
  159. if (backfillQueue == null) {
  160. backfillQueue = adultBackfillQueue()
  161. backfillAt = t
  162. console.log('adult backfill: start, ' + backfillQueue.length + ' titles without an adult flag (' + (now() - t) + ' ms to find them)')
  163. if (backfillQueue.length == 0) { backfillDone = true console.log('adult backfill: nothing to do') }
  164. return null
  165. }
  166. if (t < backfillNextAt) { return null }
  167. id = backfillQueue[backfillPos]
  168. backfillPos = backfillPos + 1
  169. r = backfillTitle(id)
  170. if (r.retry && (backfillTries[id] == null || backfillTries[id] < 2)) {
  171. backfillTries[id] = backfillTries[id] == null ? 1 : backfillTries[id] + 1
  172. q = backfillQueue
  173. q.push(id)
  174. backfillQueue = q
  175. backfillTotals.retries = backfillTotals.retries + 1
  176. } else {
  177. backfillTotals.titles = backfillTotals.titles + 1
  178. if (r.adult == true) { backfillTotals.adult = backfillTotals.adult + 1 } else if (r.adult == false) { backfillTotals.notAdult = backfillTotals.notAdult + 1 } else { backfillTotals.unknown = backfillTotals.unknown + 1 }
  179. }
  180. backfillTotals.posters = backfillTotals.posters + r.posters
  181. backfillTotals.ids = backfillTotals.ids + r.ids
  182. backfillTotals.requests = backfillTotals.requests + r.requests
  183. backfillTotals.errors = backfillTotals.errors + r.errors
  184. if (r.error != '') { console.log('adult backfill: ' + r.error) }
  185. if (r.adult != null) {
  186. refreshCatalogShow(id)
  187. searchTitleChanged(id)
  188. }
  189. took = now() - t
  190. if (took > 1500) { console.log('adult backfill: slow step, show ' + id + ', ' + took + ' ms') }
  191. backfillNextAt = now() + r.requests * msPerRequest
  192. finished = backfillPos >= backfillQueue.length
  193. if (finished || backfillPos % 25 == 0) { persistSync() }
  194. if (!finished && backfillTotals.titles > 0 && backfillTotals.titles % backfillLogEvery == 0 && !r.retry) { console.log('adult backfill: ' + backfillLine()) }
  195. if (finished) {
  196. backfillDone = true
  197. console.log('adult backfill done: ' + backfillLine())
  198. }
  199. return null
  200. }
  201. // ---- THE DETAILS REPAIR (tracker.worldapi.org#26, details.hl completeStep) ----------------------------------------
  202. // Titles the person pages added from a filmography (#16) are MINIMAL (no poster, seasons, ids, cast …), and (tracker#33, mission
  203. // 029) the old tracker's migrated rows were never synced (5,697 on the live copy, 691 series without seasons) — shows.hl
  204. // isIncomplete. Opening one completes it (components/show.hl); this job completes the rest in the background: at start it
  205. // collects every incomplete, non-adult title (the visibly reduced ones first, details.hl detailsRepairQueue) and does ONE per
  206. // step on the app's clock, like the adult backfill — a step is the daily sync's work for one title
  207. // (1–3 TMDB requests + poster + TVmaze), requests queued meanwhile are served between two steps; then a pause of 260 ms per
  208. // TMDB request + 550 ms per TVmaze request (pauseMsAfter: ≤ 40 / 10 s, ≤ 20 / 10 s). RESUMES after a restart (the queue is
  209. // what is still incomplete); TMDB 404 marks the title (`detailsCheck`), a passing failure goes to the back of the queue (at
  210. // most 2 more tries per start). Waits while the daily sync or the adult backfill runs. Persisted every 10 titles + at the end.
  211. // Log: start, every TRACKER_REPAIR_LOG_EVERY (100) titles, each failure, done. TRACKER_REPAIR=0 switches it off (off too
  212. // without TMDB_READ_TOKEN). TRACKER_REPAIR_FAST=1 (the gate, fake TMDB only): 20 ms pause instead.
  213. repairOn = env('TRACKER_REPAIR') != '0' && syncEnabled
  214. repairFast = env('TRACKER_REPAIR_FAST') == '1'
  215. repairLogEvery = env('TRACKER_REPAIR_LOG_EVERY') != null ? toNumber(env('TRACKER_REPAIR_LOG_EVERY')) : 100
  216. repairQueue = null
  217. repairPos = 0
  218. repairDone = false
  219. repairAt = 0
  220. repairNextAt = 0
  221. repairTries = {}
  222. repairTotals = { titles = 0 complete = 0 failed = 0 adult = 0 newSeasons = 0 newEpisodes = 0 posters = 0 cast = 0 requests = 0 tvmazeRequests = 0 retries = 0 }
  223. repairLine = () => {
  224. b = repairTotals
  225. return b.titles + '/' + repairQueue.length + ' titles, complete=' + b.complete + ' failed=' + b.failed + ' adult=' + b.adult + ' newSeasons=' + b.newSeasons + ' newEpisodes=' + b.newEpisodes
  226. + ' posters=' + b.posters + ' cast=' + b.cast + ' requests=' + b.requests + ' tvmazeRequests=' + b.tvmazeRequests + ' retries=' + b.retries + ' seconds=' + ((now() - repairAt) / 1000)
  227. }
  228. repairTick = () => {
  229. if (repairDone) { return null }
  230. t = now()
  231. if (repairQueue == null) {
  232. repairQueue = detailsRepairQueue()
  233. repairAt = t
  234. console.log('details repair: start, ' + repairQueue.length + ' incomplete titles (' + (now() - t) + ' ms to find them)')
  235. if (repairQueue.length == 0) { repairDone = true console.log('details repair: nothing to do') }
  236. return null
  237. }
  238. if (t < repairNextAt) { return null }
  239. id = repairQueue[repairPos]
  240. r = completeStep(id)
  241. took = now() - t
  242. if (took > 1500) { console.log('details repair: slow step, show ' + id + ', ' + took + ' ms') }
  243. if (r.error != '') { console.log('details repair: ' + r.error) }
  244. repairNextAt = now() + (repairFast ? 20 : pauseMsAfter(r))
  245. // a series takes several steps: the same title again next time
  246. if (!r.done && !r.retry) { return null }
  247. repairPos = repairPos + 1
  248. if (r.retry && (repairTries[id] == null || repairTries[id] < 2)) {
  249. repairTries[id] = repairTries[id] == null ? 1 : repairTries[id] + 1
  250. q = repairQueue
  251. q.push(id)
  252. repairQueue = q
  253. repairTotals.retries = repairTotals.retries + 1
  254. } else if (!r.skipped) {
  255. // done: its counts (a failed first step: the request alone)
  256. sum = r.sum
  257. repairTotals.titles = repairTotals.titles + 1
  258. if (r.adult == true) { repairTotals.adult = repairTotals.adult + 1 } else if (sum.steps == 0) { repairTotals.failed = repairTotals.failed + 1 } else { repairTotals.complete = repairTotals.complete + 1 }
  259. repairTotals.newSeasons = repairTotals.newSeasons + sum.newSeasons
  260. repairTotals.newEpisodes = repairTotals.newEpisodes + sum.newEpisodes
  261. repairTotals.posters = repairTotals.posters + sum.posters
  262. repairTotals.cast = repairTotals.cast + sum.cast
  263. repairTotals.requests = repairTotals.requests + (sum.steps == 0 ? r.requests : sum.requests)
  264. repairTotals.tvmazeRequests = repairTotals.tvmazeRequests + sum.tvmazeRequests
  265. }
  266. finished = repairPos >= repairQueue.length
  267. if (finished || repairPos % 10 == 0) { persistDetails() }
  268. if (!finished && !r.skipped && !r.retry && repairTotals.titles > 0 && repairTotals.titles % repairLogEvery == 0) { console.log('details repair: ' + repairLine()) }
  269. if (finished) {
  270. repairDone = true
  271. console.log('details repair done: ' + repairLine())
  272. }
  273. return null
  274. }
  275. // ---- THE TVMAZE STATUS BACKFILL (tracker#35, mission 030, backfill.hl tvmazeStatusTitle) — the details repair's second part ----
  276. // The page's status pill shows TVmaze's status when a series has one (fresher than TMDB's). The sync stores it whenever it merges
  277. // TVmaze's episodes (followed series, every completion); the series it never reached get it here: once the details repair is
  278. // done (same switch, TRACKER_REPAIR) every series with a TVmaze id and no tvmazeStatus, ONE TVmaze request per step, then 550 ms
  279. // (TVmaze: ≤ 20 / 10 s). RESUMES after a restart (the queue = what still has none); a 404 marks the series
  280. // (`tvmazeStatusCheck`), a passing failure goes to the back of the queue (at most 2 more tries per start). Waits while the daily
  281. // sync runs. Persisted every 25 series + at the end. Log: start, every TRACKER_REPAIR_LOG_EVERY series, done.
  282. statusQueue = null
  283. statusPos = 0
  284. statusDone = false
  285. statusAt = 0
  286. statusNextAt = 0
  287. statusTries = {}
  288. statusTotals = { series = 0 stored = 0 types = 0 moved = 0 notFound = 0 requests = 0 errors = 0 retries = 0 }
  289. statusLine = () => {
  290. b = statusTotals
  291. return b.series + '/' + statusQueue.length + ' series, stored=' + b.stored + ' types=' + b.types + ' moved=' + b.moved + ' notFound=' + b.notFound + ' tvmazeRequests=' + b.requests + ' errors=' + b.errors + ' retries=' + b.retries + ' seconds=' + ((now() - statusAt) / 1000)
  292. }
  293. statusTick = () => {
  294. if (statusDone) { return null }
  295. t = now()
  296. if (statusQueue == null) {
  297. statusQueue = tvmazeStatusQueue()
  298. statusAt = t
  299. console.log('tvmaze status: start, ' + statusQueue.length + ' series without a TVmaze status or type (' + (now() - t) + ' ms to find them)')
  300. if (statusQueue.length == 0) { statusDone = true console.log('tvmaze status: nothing to do') }
  301. return null
  302. }
  303. if (t < statusNextAt) { return null }
  304. id = statusQueue[statusPos]
  305. statusPos = statusPos + 1
  306. r = tvmazeStatusTitle(id)
  307. if (r.retry && (statusTries[id] == null || statusTries[id] < 2)) {
  308. statusTries[id] = statusTries[id] == null ? 1 : statusTries[id] + 1
  309. q = statusQueue
  310. q.push(id)
  311. statusQueue = q
  312. statusTotals.retries = statusTotals.retries + 1
  313. } else {
  314. statusTotals.series = statusTotals.series + 1
  315. if (r.status != null) { statusTotals.stored = statusTotals.stored + 1 } else if (r.errors > 0) { statusTotals.notFound = statusTotals.notFound + 1 }
  316. if (r.type != null) { statusTotals.types = statusTotals.types + 1 }
  317. // #43 (mission 037): TVmaze's type made it a show (or no longer one) — it moves between /series and /shows at once
  318. if (r.moved) { refreshCatalogShow(id) statusTotals.moved = statusTotals.moved + 1 console.log('tvmaze status: moved ' + id + ' (TVmaze type ' + r.type + ')') }
  319. }
  320. statusTotals.requests = statusTotals.requests + r.tvmazeRequests
  321. statusTotals.errors = statusTotals.errors + r.errors
  322. if (r.error != '') { console.log('tvmaze status: ' + r.error) }
  323. statusNextAt = now() + (repairFast ? 20 : pauseMsAfter(r))
  324. finished = statusPos >= statusQueue.length
  325. if (finished || statusPos % 25 == 0) { persistSync() }
  326. if (!finished && !r.retry && statusTotals.series > 0 && statusTotals.series % repairLogEvery == 0) { console.log('tvmaze status: ' + statusLine()) }
  327. if (finished) {
  328. statusDone = true
  329. console.log('tvmaze status done: ' + statusLine())
  330. }
  331. return null
  332. }
  333. // ---- THE KIND BACKFILL (tracker.worldapi.org#21, mission 068, backfill.hl kindTitle) ----------------------------------
  334. // Every TV title without a stored kind gets one: a title with a show genre (Reality / Talk / News) from the stored data (no
  335. // request), any other from TMDB's TV type (ONE request: GET /tv/<id>, then 260 ms pause). Starts once the adult backfill is
  336. // done (or off), waits while the daily sync runs, ONE title per step on the same clock. A title that becomes a SHOW moves from
  337. // /series to /shows at once (catalog.hl refreshCatalogShow). RESUMES after a restart (the queue = every TV title still without
  338. // a kind); a 404 stores the genres' answer + `kindCheck`; a passing failure goes to the back of the queue, at most 2 more tries
  339. // per start. Log: start, every TRACKER_KINDS_LOG_EVERY (500) titles, done. TRACKER_KINDS=0 switches it off; without
  340. // TMDB_READ_TOKEN it is off too (the lists still split by genre: shows.hl titleKind).
  341. kindsOn = env('TRACKER_KINDS') != '0' && syncEnabled
  342. kindsLogEvery = env('TRACKER_KINDS_LOG_EVERY') != null ? toNumber(env('TRACKER_KINDS_LOG_EVERY')) : 500
  343. kindQueue = null
  344. kindPos = 0
  345. kindDone = false
  346. kindAt = 0
  347. kindNextAt = 0
  348. kindTries = {}
  349. kindTotals = { titles = 0 series = 0 shows = 0 fromGenre = 0 moved = 0 requests = 0 errors = 0 retries = 0 }
  350. kindLine = () => {
  351. k = kindTotals
  352. return k.titles + '/' + kindQueue.length + ' titles, series=' + k.series + ' shows=' + k.shows + ' fromGenre=' + k.fromGenre + ' moved=' + k.moved
  353. + ' requests=' + k.requests + ' errors=' + k.errors + ' retries=' + k.retries + ' seconds=' + ((now() - kindAt) / 1000)
  354. }
  355. kindTick = () => {
  356. if (kindDone) { return null }
  357. t = now()
  358. if (kindQueue == null) {
  359. kindQueue = kindBackfillQueue()
  360. kindAt = t
  361. console.log('kind backfill: start, ' + kindQueue.length + ' TV titles without a kind (' + (now() - t) + ' ms to find them)')
  362. if (kindQueue.length == 0) { kindDone = true console.log('kind backfill: nothing to do') }
  363. return null
  364. }
  365. if (t < kindNextAt) { return null }
  366. id = kindQueue[kindPos]
  367. kindPos = kindPos + 1
  368. r = kindTitle(id)
  369. if (r.retry && (kindTries[id] == null || kindTries[id] < 2)) {
  370. kindTries[id] = kindTries[id] == null ? 1 : kindTries[id] + 1
  371. q = kindQueue
  372. q.push(id)
  373. kindQueue = q
  374. kindTotals.retries = kindTotals.retries + 1
  375. } else {
  376. kindTotals.titles = kindTotals.titles + 1
  377. if (r.kind == 'show') { kindTotals.shows = kindTotals.shows + 1 } else if (r.kind == 'series') { kindTotals.series = kindTotals.series + 1 }
  378. if (r.fromGenre) { kindTotals.fromGenre = kindTotals.fromGenre + 1 }
  379. }
  380. kindTotals.requests = kindTotals.requests + r.requests
  381. kindTotals.errors = kindTotals.errors + r.errors
  382. if (r.error != '') { console.log('kind backfill: ' + r.error) }
  383. // until now the genres decided (no show genre = series): only a title TMDB's type makes a SHOW changes its list
  384. if (r.kind == 'show' && !r.fromGenre) {
  385. refreshCatalogShow(id)
  386. kindTotals.moved = kindTotals.moved + 1
  387. }
  388. took = now() - t
  389. if (took > 1500) { console.log('kind backfill: slow step, show ' + id + ', ' + took + ' ms') }
  390. kindNextAt = now() + r.requests * msPerRequest
  391. finished = kindPos >= kindQueue.length
  392. if (finished || kindPos % 25 == 0) { persistSync() }
  393. if (!finished && kindTotals.titles > 0 && kindTotals.titles % kindsLogEvery == 0 && !r.retry) { console.log('kind backfill: ' + kindLine()) }
  394. if (finished) {
  395. kindDone = true
  396. console.log('kind backfill done: ' + kindLine())
  397. }
  398. return null
  399. }
  400. // ---- THE CREDITS JOB (tracker.worldapi.org#28, credits.hl creditsStep) ------------------------------------------------------
  401. // Every PUBLIC title (adult == false; adult/unknown are skipped) with a TMDB id gets its whole cast + crew once: at start (after
  402. // the adult backfill and the details repair are done) the job collects every such title without `creditsAt` and asks ONE per
  403. // step on the app's clock — one request (details + credits / aggregate_credits), then 260 ms (≤ 40 / 10 s); requests queued
  404. // meanwhile are served between two steps. Titles the details repair, the daily sync or an import already did carry `creditsAt`
  405. // and are not asked. RESUMES after a restart (the queue = what still has no credits); TMDB 404 / adult → `creditsCheck`, never
  406. // asked again; a passing failure goes to the back of the queue (at most 2 more tries per start). Waits while the daily sync
  407. // runs. Persisted (shows + people only) every 25 titles + at the end. Log: start, every TRACKER_CREDITS_LOG_EVERY (500) titles, failures, slow steps
  408. // (> 1.5 s), done. TRACKER_CREDITS=0 switches it off (off too without TMDB_READ_TOKEN). TRACKER_CREDITS_FAST=1 (the gate, fake
  409. // TMDB only): 20 ms pause instead.
  410. // MEMORY LIMIT: the runtime never frees what a fetch + JSON parse took (mission 061 measured ~6 MB more RSS per title on the real
  411. // data, 300 fetches of a 440 KB JSON = +2.7 GB in a 10-line script) — the whole job in one process would add ~35 GB. Before every
  412. // step the job reads its own RSS (/proc/self/status); above TRACKER_CREDITS_MAX_MB (16000) it PAUSES for this process (logged) and
  413. // goes on with what is left after the next restart (deploy, `docker restart`).
  414. creditsOn = env('TRACKER_CREDITS') != '0' && syncEnabled
  415. creditsFast = env('TRACKER_CREDITS_FAST') == '1'
  416. creditsLogEvery = env('TRACKER_CREDITS_LOG_EVERY') != null ? toNumber(env('TRACKER_CREDITS_LOG_EVERY')) : 500
  417. creditsQueueIds = null
  418. creditsPos = 0
  419. creditsDone = false
  420. creditsAt = 0
  421. creditsNextAt = 0
  422. creditsTries = {}
  423. creditsMaxMb = env('TRACKER_CREDITS_MAX_MB') != null ? toNumber(env('TRACKER_CREDITS_MAX_MB')) : 16000
  424. creditsTotals = { titles = 0 done = 0 failed = 0 cast = 0 crew = 0 people = 0 requests = 0 retries = 0 slowest = 0 }
  425. // this process' resident memory in MB (VmRSS), 0 when unknown
  426. rssMb = () => {
  427. if (!exists('/proc/self/status')) { return 0 }
  428. t = readFile('/proc/self/status')
  429. i = t != null ? t.indexOf('VmRSS:') : -1
  430. if (i < 0) { return 0 }
  431. kb = toNumber(t.slice(i + 6, t.indexOf('kB', i)).trim())
  432. return kb != null ? (kb - kb % 1024) / 1024 : 0
  433. }
  434. creditsLine = () => {
  435. b = creditsTotals
  436. return b.titles + '/' + creditsQueueIds.length + ' titles, done=' + b.done + ' failed=' + b.failed + ' cast=' + b.cast + ' crew=' + b.crew + ' people=' + b.people
  437. + ' requests=' + b.requests + ' retries=' + b.retries + ' slowestStepMs=' + b.slowest + ' seconds=' + ((now() - creditsAt) / 1000)
  438. }
  439. creditsTick = () => {
  440. if (creditsDone) { return null }
  441. t = now()
  442. if (creditsQueueIds == null) {
  443. creditsQueueIds = creditsQueue()
  444. creditsAt = t
  445. console.log('credits job: start, ' + creditsQueueIds.length + ' titles without credits (' + (now() - t) + ' ms to find them)')
  446. if (creditsQueueIds.length == 0) { creditsDone = true console.log('credits job: nothing to do') }
  447. return null
  448. }
  449. if (t < creditsNextAt) { return null }
  450. mb = rssMb()
  451. if (mb > creditsMaxMb) {
  452. creditsDone = true
  453. persistCredits()
  454. console.log('credits job: paused, RSS ' + mb + ' MB > TRACKER_CREDITS_MAX_MB ' + creditsMaxMb + ' — ' + (creditsQueueIds.length - creditsPos) + ' titles left for the next start; ' + creditsLine())
  455. return null
  456. }
  457. id = creditsQueueIds[creditsPos]
  458. creditsPos = creditsPos + 1
  459. r = creditsStep(id)
  460. took = now() - t
  461. if (took > creditsTotals.slowest) { creditsTotals.slowest = took }
  462. if (took > 1500) { console.log('credits job: slow step, show ' + id + ', ' + took + ' ms, cast=' + r.cast + ' crew=' + r.crew + ' people=' + r.people) }
  463. if (r.error != '') { console.log('credits job: ' + r.error) }
  464. creditsNextAt = now() + (creditsFast ? 20 : r.requests * msPerRequest)
  465. if (r.retry && (creditsTries[id] == null || creditsTries[id] < 2)) {
  466. creditsTries[id] = creditsTries[id] == null ? 1 : creditsTries[id] + 1
  467. q = creditsQueueIds
  468. q.push(id)
  469. creditsQueueIds = q
  470. creditsTotals.retries = creditsTotals.retries + 1
  471. } else if (!r.skipped) {
  472. creditsTotals.titles = creditsTotals.titles + 1
  473. if (r.error != '') { creditsTotals.failed = creditsTotals.failed + 1 } else { creditsTotals.done = creditsTotals.done + 1 }
  474. creditsTotals.cast = creditsTotals.cast + r.cast
  475. creditsTotals.crew = creditsTotals.crew + r.crew
  476. creditsTotals.people = creditsTotals.people + r.people
  477. }
  478. creditsTotals.requests = creditsTotals.requests + r.requests
  479. finished = creditsPos >= creditsQueueIds.length
  480. if (finished || creditsPos % 25 == 0) { persistCredits() }
  481. if (!finished && !r.skipped && !r.retry && creditsTotals.titles > 0 && creditsTotals.titles % creditsLogEvery == 0) { console.log('credits job: ' + creditsLine()) }
  482. if (finished) {
  483. creditsDone = true
  484. console.log('credits job done: ' + creditsLine())
  485. }
  486. return null
  487. }
  488. // ---- THE DUPLICATE MERGE (tracker.worldapi.org#31, mission 026, merge.hl) ----------------------------------------------------
  489. // The old tracker's duplicate titles (same type + TMDB id) become ONE record each — follows, watches, seasons, cast, credits and
  490. // timelines moved to the keeper, the others kept as tombstones whose slug and short id lead to it — and seasons that name a title
  491. // without being in its list are merged into their twin or linked, and (tracker#39) a season's double episodes into one (merge.hl
  492. // has the rules). On its own clock (every 0.1 s) ONE
  493. // item per tick (a duplicate group, or one title's stray seasons; no TMDB), so pages are served between two items. The TMDB jobs
  494. // below (daily sync, backfills, repair, credits, collections) wait until it is done: none of them works on a record that is about
  495. // to become a tombstone. RESUMES after a restart (the queue = what is still duplicated / stray). Persisted every 10 items and at
  496. // the end. Log: start, one line per tombstone / repaired title, done; with nothing to do one line. TRACKER_MERGE=0 switches it off.
  497. mergeOn = env('TRACKER_MERGE') != '0'
  498. mergeRun = { started = false done = !mergeOn at = 0 items = 0 slowest = 0 }
  499. mergeTick = () => {
  500. if (mergeRun.done) { return null }
  501. t = now()
  502. if (!mergeRun.started) {
  503. mergeRun.started = true
  504. mergeRun.at = t
  505. n = mergePrepare()
  506. st = mergeStats()
  507. console.log('merge: start, ' + st.groups + ' duplicate groups (' + st.records + ' records), ' + st.strayTitles + ' titles with stray seasons, ' + st.doubleSeasons + ' seasons with double episodes (' + st.ms + ' ms to find them)')
  508. if (n == 0) {
  509. mergeRun.done = true
  510. console.log('merge: nothing to do')
  511. }
  512. return null
  513. }
  514. r = mergeStep()
  515. mergeRun.items = mergeRun.items + 1
  516. took = now() - t
  517. if (took > mergeRun.slowest) { mergeRun.slowest = took }
  518. if (took > 1500) { console.log('merge: slow step, ' + took + ' ms') }
  519. if (r.done || mergeRun.items % 10 == 0) { persistMerge() }
  520. if (r.done) {
  521. mergeRun.done = true
  522. console.log('merge done: ' + mergeLine() + ' items=' + mergeRun.items + ' slowestStepMs=' + mergeRun.slowest + ' seconds=' + ((now() - mergeRun.at) / 1000))
  523. }
  524. return null
  525. }
  526. if (!mergeOn) { console.log('merge: off (TRACKER_MERGE=0)') }
  527. else {
  528. mergeClock = every(0.1)
  529. on mergeClock.tick(x) {
  530. mergeTick()
  531. return null
  532. }
  533. }
  534. // ONE clock for all five (a tick every 0.1 s): the daily sync's step once a second as before; the backfill's steps in
  535. // between, only while no daily run is going on; the details repair only when neither has work (then its TVmaze status part,
  536. // tracker#35); then the kind backfill (#21);
  537. // the credits job (#28) last
  538. clockOn = syncOn || backfillOn || repairOn || kindsOn || creditsOn
  539. clockTicks = 0
  540. if (syncOn) { console.log('tmdb sync: daily at ' + syncHour + ':00 UTC') } else { console.log('tmdb sync: off (' + (syncEnabled ? 'TRACKER_SYNC=0' : 'no TMDB_READ_TOKEN') + ')') }
  541. if (!backfillOn) { console.log('adult backfill: off (' + (syncEnabled ? 'TRACKER_BACKFILL=0' : 'no TMDB_READ_TOKEN') + ')') }
  542. if (!repairOn) { console.log('details repair: off (' + (syncEnabled ? 'TRACKER_REPAIR=0' : 'no TMDB_READ_TOKEN') + ')') }
  543. if (!creditsOn) { console.log('credits job: off (' + (syncEnabled ? 'TRACKER_CREDITS=0' : 'no TMDB_READ_TOKEN') + ')') }
  544. if (!kindsOn) { console.log('kind backfill: off (' + (syncEnabled ? 'TRACKER_KINDS=0' : 'no TMDB_READ_TOKEN') + ')') }
  545. if (clockOn) {
  546. syncClock = every(0.1)
  547. on syncClock.tick(x) {
  548. clockTicks = clockTicks + 1
  549. // tracker#31: not before the duplicate merge is done
  550. if (!mergeRun.done) { return null }
  551. busy = syncQueue.length > 0 || syncPlanning
  552. if (syncOn && clockTicks % 10 == 0) { syncTick() }
  553. free = !busy && syncQueue.length == 0 && !syncPlanning
  554. if (backfillOn && free && !backfillDone) { backfillTick() }
  555. else if (repairOn && free && (!backfillOn || backfillDone) && !repairDone) { repairTick() }
  556. else if (repairOn && free && (!backfillOn || backfillDone) && !statusDone) { statusTick() }
  557. else if (kindsOn && free && (!backfillOn || backfillDone) && (!repairOn || (repairDone && statusDone)) && !kindDone) { kindTick() }
  558. else if (creditsOn && free && (!backfillOn || backfillDone) && (!repairOn || (repairDone && statusDone)) && (!kindsOn || kindDone)) { creditsTick() }
  559. return null
  560. }
  561. }
  562. // ---- THE SHORT-ID BACKFILL (mission 060, tracker.worldapi.org#27, shortids.hl shortIdStep) ----------------------------
  563. // Every show and person without a short id gets one in the background: on its own clock (every 0.1 s) ONE batch per tick,
  564. // as many records as fit into TRACKER_SHORTID_BUDGET_MS (default 40 ms) — no TMDB, only the tables — so a page waits at
  565. // most one batch. The queue is built at start (`ensureShortIds`) from what is still missing = it resumes after a restart.
  566. // Persisted every 20 batches and at the end. Log: start, every TRACKER_SHORTID_LOG_EVERY (5000) records, done; with nothing
  567. // to do one line. TRACKER_SHORTIDS=0 switches it off (the gate's resume check).
  568. shortIdOn = env('TRACKER_SHORTIDS') != '0'
  569. shortIdBudget = env('TRACKER_SHORTID_BUDGET_MS') != null ? toNumber(env('TRACKER_SHORTID_BUDGET_MS')) : 40
  570. shortIdLogEvery = env('TRACKER_SHORTID_LOG_EVERY') != null ? toNumber(env('TRACKER_SHORTID_LOG_EVERY')) : 5000
  571. shortIdRun = { at = 0 batches = 0 assigned = 0 nextLog = 0 slowest = 0 slowestPersist = 0 done = false }
  572. shortIdTick = () => {
  573. if (shortIdRun.done) { return null }
  574. t = now()
  575. r = shortIdStep(shortIdBudget)
  576. took = now() - t
  577. shortIdRun.batches = shortIdRun.batches + 1
  578. shortIdRun.assigned = shortIdRun.assigned + r.assigned
  579. if (took > shortIdRun.slowest) { shortIdRun.slowest = took }
  580. if (r.done || shortIdRun.batches % 20 == 0) {
  581. tp = now()
  582. persistShortIds()
  583. if (now() - tp > shortIdRun.slowestPersist) { shortIdRun.slowestPersist = now() - tp }
  584. }
  585. if (!r.done && shortIdRun.assigned >= shortIdRun.nextLog) {
  586. console.log('short ids: ' + shortIdRun.assigned + ' assigned, ' + r.left + ' left, ' + ((now() - shortIdRun.at) / 1000) + ' s')
  587. shortIdRun.nextLog = shortIdRun.nextLog + shortIdLogEvery
  588. }
  589. if (r.done) {
  590. shortIdRun.done = true
  591. s = shortIdStats()
  592. console.log('short ids done: assigned=' + shortIdRun.assigned + ' oldKept=' + s.oldKept + ' batches=' + shortIdRun.batches + ' slowestBatchMs=' + shortIdRun.slowest + ' slowestPersistMs=' + shortIdRun.slowestPersist + ' seconds=' + ((now() - shortIdRun.at) / 1000))
  593. }
  594. return null
  595. }
  596. if (!shortIdOn) { console.log('short ids: backfill off (TRACKER_SHORTIDS=0), ' + shortIdsLeft() + ' records without one') }
  597. else if (shortIdsLeft() == 0) { console.log('short ids: nothing to do') }
  598. else {
  599. shortIdRun.at = now()
  600. shortIdRun.nextLog = shortIdLogEvery
  601. console.log('short ids: backfill start, ' + shortIdsLeft() + ' records, ' + shortIdBudget + ' ms per batch')
  602. shortIdClock = every(0.1)
  603. on shortIdClock.tick(x) {
  604. shortIdTick()
  605. return null
  606. }
  607. }
  608. // ---- THE COLLECTION SEED (mission 066, tracker.worldapi.org#19, collections.hl seedStep) ------------------------------------
  609. // TMDB's movie collections become timelines: ONE movie per step (its details; once per collection the collection's parts), on
  610. // its own 0.1 s clock like the adult backfill — a pause of 260 ms per TMDB request made (≤ 40 / 10 s). It WAITS while the daily
  611. // sync or the adult backfill runs (they share TMDB's limit and the backfill brings the adult flags it skips by); since the merge
  612. // (mission 070) also until the details repair, the kind backfill and the credits job are done (or paused) — the LAST job. The queue =
  613. // every non-adult movie without a check row, so it RESUMES after a restart and does nothing once all are checked. Persisted
  614. // every TRACKER_COLLECTIONS_PERSIST_EVERY (25) steps and at the end. Log: start, every TRACKER_COLLECTIONS_LOG_EVERY (500) movies, done.
  615. // TRACKER_COLLECTIONS=0 switches it off; without TMDB_READ_TOKEN it is off too.
  616. collectionsOn = env('TRACKER_COLLECTIONS') != '0' && syncEnabled
  617. collectionsLogEvery = env('TRACKER_COLLECTIONS_LOG_EVERY') != null ? toNumber(env('TRACKER_COLLECTIONS_LOG_EVERY')) : 500
  618. collectionsPersistEvery = env('TRACKER_COLLECTIONS_PERSIST_EVERY') != null ? toNumber(env('TRACKER_COLLECTIONS_PERSIST_EVERY')) : 25
  619. collectionRun = { at = 0 steps = 0 nextAt = 0 done = false started = false slowest = 0 }
  620. collectionTick = () => {
  621. if (collectionRun.done) { return null }
  622. // mission 070 (merge): also behind the details repair, the kind backfill and the credits job — one TMDB pacing at a time
  623. if (!mergeRun.done || syncQueue.length > 0 || syncPlanning || (backfillOn && !backfillDone) || (repairOn && (!repairDone || !statusDone)) || (kindsOn && !kindDone) || (creditsOn && !creditsDone)) { return null }
  624. t = now()
  625. if (!collectionRun.started) {
  626. collectionRun.started = true
  627. collectionRun.at = t
  628. n = seedPrepare()
  629. console.log('collections: start, ' + n + ' movies to check (' + (now() - t) + ' ms to find them)')
  630. if (n == 0) { collectionRun.done = true console.log('collections: nothing to do') }
  631. return null
  632. }
  633. if (t < collectionRun.nextAt) { return null }
  634. r = seedStep()
  635. if (r.done) {
  636. collectionRun.done = true
  637. seedPersist()
  638. console.log('collections done: ' + seedLine() + ' seconds=' + ((now() - collectionRun.at) / 1000) + ' slowestStepMs=' + collectionRun.slowest)
  639. return null
  640. }
  641. collectionRun.steps = collectionRun.steps + 1
  642. took = now() - t
  643. if (took > collectionRun.slowest) { collectionRun.slowest = took }
  644. if (r.error != '') { console.log('collections: ' + r.error) }
  645. if (took > 1500) { console.log('collections: slow step, ' + took + ' ms') }
  646. collectionRun.nextAt = now() + r.requests * msPerRequest
  647. if (collectionRun.steps % collectionsPersistEvery == 0) { seedPersist() }
  648. if (collectionRun.steps % collectionsLogEvery == 0) { console.log('collections: ' + seedLine() + ' seconds=' + ((now() - collectionRun.at) / 1000)) }
  649. return null
  650. }
  651. if (!collectionsOn) { console.log('collections: off (' + (syncEnabled ? 'TRACKER_COLLECTIONS=0' : 'no TMDB_READ_TOKEN') + ')') }
  652. else {
  653. collectionClock = every(0.1)
  654. on collectionClock.tick(x) {
  655. collectionTick()
  656. return null
  657. }
  658. }
  659. // ---- THE CURATED FRANCHISE SEED (tracker.worldapi.org#40, mission 034, franchiseseed.hl) --------------------------------------
  660. // data/franchises.json → franchises + timelines, movies AND series. The LAST start job: it waits until the merge, the daily sync,
  661. // the backfills, the details repair, the kind backfill, the credits job and the collection seed are done (or off) — the
  662. // collection seed's timelines are what the file's `collections` attach. Then on its own 0.1 s clock ONE step per tick: the next
  663. // missing title imported (the search's import, complete with cast + crew; its log line `search import: …`), then the pause the daily sync makes after the same
  664. // requests (≤ 40 TMDB / 10 s, ≤ 20 TVmaze / 10 s); the queue done, the build (no TMDB). RESUMES after a restart (the queue = the
  665. // titles still missing), IDEMPOTENT (nothing missing + nothing changed = no write). Without TMDB_READ_TOKEN it only builds from
  666. // what we have. Log: start, every import, every TRACKER_FRANCHISE_SEED_LOG_EVERY (25) imports, done.
  667. // TRACKER_FRANCHISE_SEED=0 switches it off; TRACKER_FRANCHISE_FILE another data file (the gate's).
  668. franchiseSeedOn = env('TRACKER_FRANCHISE_SEED') != '0'
  669. franchiseSeedLogEvery = env('TRACKER_FRANCHISE_SEED_LOG_EVERY') != null ? toNumber(env('TRACKER_FRANCHISE_SEED_LOG_EVERY')) : 25
  670. franchiseRun = { at = 0 steps = 0 nextAt = 0 done = false started = false slowest = 0 }
  671. franchiseSeedTick = () => {
  672. if (franchiseRun.done) { return null }
  673. if (!mergeRun.done || syncQueue.length > 0 || syncPlanning || (backfillOn && !backfillDone) || (repairOn && (!repairDone || !statusDone)) || (kindsOn && !kindDone) || (creditsOn && !creditsDone) || (collectionsOn && !collectionRun.done)) { return null }
  674. t = now()
  675. if (!franchiseRun.started) {
  676. franchiseRun.started = true
  677. franchiseRun.at = t
  678. n = franchiseSeedPrepare(syncEnabled)
  679. st = franchiseSeedStats()
  680. if (!st.file) {
  681. franchiseRun.done = true
  682. console.log('franchise seed: ' + st.error + ' — nothing seeded')
  683. return null
  684. }
  685. console.log('franchise seed: start, ' + st.franchises + ' franchises, ' + st.timelines + ' timelines, ' + st.titles + ' titles in the file — ' + st.have + ' we have, ' + n + ' to import, ' + st.missing + ' TMDB lacks (' + (now() - t) + ' ms)')
  686. return null
  687. }
  688. if (t < franchiseRun.nextAt) { return null }
  689. r = franchiseSeedStep()
  690. took = now() - t
  691. if (took > franchiseRun.slowest) { franchiseRun.slowest = took }
  692. if (r.error != '') { console.log('franchise seed: ' + r.error) }
  693. if (took > 1500) { console.log('franchise seed: slow step, ' + took + ' ms') }
  694. if (r.done) {
  695. franchiseRun.done = true
  696. console.log('franchise seed done: ' + franchiseSeedLine() + ' seconds=' + ((now() - franchiseRun.at) / 1000) + ' slowestStepMs=' + franchiseRun.slowest)
  697. return null
  698. }
  699. franchiseRun.steps = franchiseRun.steps + 1
  700. franchiseRun.nextAt = now() + pauseMsAfter(r)
  701. if (franchiseRun.steps % franchiseSeedLogEvery == 0) { console.log('franchise seed: ' + franchiseSeedLine() + ' seconds=' + ((now() - franchiseRun.at) / 1000)) }
  702. return null
  703. }
  704. if (!franchiseSeedOn) { console.log('franchise seed: off (TRACKER_FRANCHISE_SEED=0)') }
  705. else {
  706. if (!syncEnabled) { console.log('franchise seed: no TMDB_READ_TOKEN — builds from the titles we have, imports nothing') }
  707. franchiseClock = every(0.1)
  708. on franchiseClock.tick(x) {
  709. franchiseSeedTick()
  710. return null
  711. }
  712. }
  713. // ---- THE ART BACKFILL + THE PHOTO BACKFILL (mission 032: tracker#33's The Remaining, tracker#37's /people) --------------------
  714. // The LAST start jobs, after the franchise seed (and everything it waits for): first every public title without a poster file /
  715. // public movie without runtime (backfill.hl artTitle: the poster, else the backdrop, + runtime), then every person without a photo
  716. // source (people.hl photoStep, /people's order). ONE item per step on their own 0.1 s clock, then n × 260 ms (≤ 40 TMDB requests
  717. // / 10 s). Resume after a restart (`artAt`/`artCheck`, `photoCheck`); a passing failure goes to the back of the queue (at most 2
  718. // more tries per start). Persisted every 25 items and at the end; the photo job pauses above TRACKER_PHOTOS_MAX_MB (16000) RSS
  719. // like the credits job. Log: start, every TRACKER_ART_LOG_EVERY (100) / TRACKER_PHOTOS_LOG_EVERY (1000) items, done.
  720. // TRACKER_ART=0 / TRACKER_PHOTOS=0 switch them off; without TMDB_READ_TOKEN they are off.
  721. artOn = env('TRACKER_ART') != '0' && syncEnabled
  722. photosOn = env('TRACKER_PHOTOS') != '0' && syncEnabled
  723. artLogEvery = env('TRACKER_ART_LOG_EVERY') != null ? toNumber(env('TRACKER_ART_LOG_EVERY')) : 100
  724. photosLogEvery = env('TRACKER_PHOTOS_LOG_EVERY') != null ? toNumber(env('TRACKER_PHOTOS_LOG_EVERY')) : 1000
  725. photosMaxMb = env('TRACKER_PHOTOS_MAX_MB') != null ? toNumber(env('TRACKER_PHOTOS_MAX_MB')) : 16000
  726. artRun = { queue = null pos = 0 done = !artOn at = 0 nextAt = 0 tries = {} titles = 0 posters = 0 backdrops = 0 runtimes = 0 failed = 0 requests = 0 retries = 0 slowest = 0 }
  727. photoRun = { queue = null pos = 0 done = !photosOn at = 0 nextAt = 0 tries = {} people = 0 found = 0 none = 0 failed = 0 requests = 0 retries = 0 slowest = 0 }
  728. artLine = () => {
  729. b = artRun
  730. return b.titles + '/' + b.queue.length + ' titles, posters=' + b.posters + ' backdrops=' + b.backdrops + ' runtimes=' + b.runtimes + ' failed=' + b.failed
  731. + ' requests=' + b.requests + ' retries=' + b.retries + ' slowestStepMs=' + b.slowest + ' seconds=' + ((now() - b.at) / 1000)
  732. }
  733. photoLine = () => {
  734. b = photoRun
  735. return b.people + '/' + b.queue.length + ' people, photos=' + b.found + ' none=' + b.none + ' failed=' + b.failed + ' requests=' + b.requests + ' retries=' + b.retries
  736. + ' slowestStepMs=' + b.slowest + ' seconds=' + ((now() - b.at) / 1000)
  737. }
  738. // everything the two wait for: the other start jobs (one TMDB pacing at a time)
  739. lastJobsMayRun = () => {
  740. return mergeRun.done && syncQueue.length == 0 && !syncPlanning && (!backfillOn || backfillDone) && (!repairOn || (repairDone && statusDone)) && (!kindsOn || kindDone)
  741. && (!creditsOn || creditsDone) && (!collectionsOn || collectionRun.done) && (!franchiseSeedOn || franchiseRun.done)
  742. }
  743. artTick = () => {
  744. t = now()
  745. if (artRun.queue == null) {
  746. artRun.queue = artQueue()
  747. artRun.at = t
  748. console.log('art backfill: start, ' + artRun.queue.length + ' public titles without a poster file or a movie runtime (' + (now() - t) + ' ms to find them)')
  749. if (artRun.queue.length == 0) { artRun.done = true console.log('art backfill: nothing to do') }
  750. return null
  751. }
  752. if (t < artRun.nextAt) { return null }
  753. id = artRun.queue[artRun.pos]
  754. artRun.pos = artRun.pos + 1
  755. r = artTitle(id)
  756. took = now() - t
  757. if (took > artRun.slowest) { artRun.slowest = took }
  758. if (r.error != '') { console.log('art backfill: ' + r.error) }
  759. artRun.nextAt = now() + r.requests * msPerRequest
  760. artRun.requests = artRun.requests + r.requests
  761. if (r.retry && (artRun.tries[id] == null || artRun.tries[id] < 2)) {
  762. artRun.tries[id] = artRun.tries[id] == null ? 1 : artRun.tries[id] + 1
  763. q = artRun.queue
  764. q.push(id)
  765. artRun.queue = q
  766. artRun.retries = artRun.retries + 1
  767. } else if (r.requests > 0) {
  768. artRun.titles = artRun.titles + 1
  769. artRun.posters = artRun.posters + r.posters
  770. artRun.backdrops = artRun.backdrops + r.backdrops
  771. if (r.runtime != null) { artRun.runtimes = artRun.runtimes + 1 }
  772. if (r.error != '') { artRun.failed = artRun.failed + 1 }
  773. if (r.posters + r.backdrops > 0) { refreshCatalogShow(id) }
  774. }
  775. finished = artRun.pos >= artRun.queue.length
  776. if (finished || artRun.pos % 25 == 0) { persistSync() }
  777. if (!finished && artRun.titles > 0 && artRun.pos % artLogEvery == 0) { console.log('art backfill: ' + artLine()) }
  778. if (finished) {
  779. artRun.done = true
  780. console.log('art backfill done: ' + artLine())
  781. }
  782. return null
  783. }
  784. photoTick = () => {
  785. t = now()
  786. if (photoRun.queue == null) {
  787. photoRun.queue = photoQueue()
  788. photoRun.at = t
  789. console.log('photo backfill: start, ' + photoRun.queue.length + ' people without a photo source (' + (now() - t) + ' ms to find them)')
  790. if (photoRun.queue.length == 0) { photoRun.done = true console.log('photo backfill: nothing to do') }
  791. return null
  792. }
  793. if (t < photoRun.nextAt) { return null }
  794. mb = rssMb()
  795. if (mb > photosMaxMb) {
  796. photoRun.done = true
  797. persistPhotos()
  798. console.log('photo backfill: paused, RSS ' + mb + ' MB > TRACKER_PHOTOS_MAX_MB ' + photosMaxMb + ' — ' + (photoRun.queue.length - photoRun.pos) + ' people left for the next start; ' + photoLine())
  799. return null
  800. }
  801. id = photoRun.queue[photoRun.pos]
  802. photoRun.pos = photoRun.pos + 1
  803. r = photoStep(id)
  804. took = now() - t
  805. if (took > photoRun.slowest) { photoRun.slowest = took }
  806. if (r.error != '') { console.log('photo backfill: ' + r.error) }
  807. photoRun.nextAt = now() + r.requests * msPerRequest
  808. photoRun.requests = photoRun.requests + r.requests
  809. if (r.retry && (photoRun.tries[id] == null || photoRun.tries[id] < 2)) {
  810. photoRun.tries[id] = photoRun.tries[id] == null ? 1 : photoRun.tries[id] + 1
  811. q = photoRun.queue
  812. q.push(id)
  813. photoRun.queue = q
  814. photoRun.retries = photoRun.retries + 1
  815. } else if (r.requests > 0) {
  816. photoRun.people = photoRun.people + 1
  817. if (r.found) { photoRun.found = photoRun.found + 1 } else if (r.error != '') { photoRun.failed = photoRun.failed + 1 } else { photoRun.none = photoRun.none + 1 }
  818. }
  819. finished = photoRun.pos >= photoRun.queue.length
  820. if (finished || photoRun.pos % 25 == 0) { persistPhotos() }
  821. if (!finished && photoRun.people > 0 && photoRun.pos % photosLogEvery == 0) { console.log('photo backfill: ' + photoLine()) }
  822. if (finished) {
  823. photoRun.done = true
  824. console.log('photo backfill done: ' + photoLine())
  825. }
  826. return null
  827. }
  828. if (!artOn) { console.log('art backfill: off (' + (syncEnabled ? 'TRACKER_ART=0' : 'no TMDB_READ_TOKEN') + ')') }
  829. if (!photosOn) { console.log('photo backfill: off (' + (syncEnabled ? 'TRACKER_PHOTOS=0' : 'no TMDB_READ_TOKEN') + ')') }
  830. if (artOn || photosOn) {
  831. lastClock = every(0.1)
  832. on lastClock.tick(x) {
  833. if (!lastJobsMayRun()) { return null }
  834. if (!artRun.done) { artTick() } else if (!photoRun.done) { photoTick() }
  835. return null
  836. }
  837. }

Branches

Latest commits

  • 3ed3a0efmission 042: gates old/new equal (browser #41 route-table regex accepts unescaped hl:web output), real-data TTFB, E2E blocked by lisbeth lockmre
  • d5b61e90LOG 042mre
  • 4acdbaccmission 042: memory comparison old vs ce6f2e0a (stopped: warm VmHWM higher), reportmre
  • d0f620d6mission 042: re-vendor Hybriel ce6f2e0a (plugins incl. mpackdb; bin runtime built from the same archive, git-ignored), compact=false keptmre
  • 5bd14e12mission 040: theme 76f99f6 gates + E2E green; 44 px fixes (skeleton row, type-word, genre-link/pill); README, reportmre
  • 77cfa45cmission 040: partial report (theme/modal/pagination re-copied, tilelist onPage, 44 px taps; gates new + E2E pending)mre
  • 4b14822fe2e-theme: menu marker by colourmre
  • 09780f4emission 040 WIP: theme, modal, pagination, tokens re-copied from layouts 76f99f6; tilelist uses onPage; tap sizes 44 px for tracker's own small buttons; tests/e2e-theme.mjsmre
  • fb80bb8chotfix: open the 5 show stores without compaction (compact = false) — compact-on-open left another handle on stale offsets, every read gave a bare Number (hybriel mpackdb refresh after a shrink); live down 2026-10-20 05:11mre
  • e00d3bf7paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • a8c7f234tracker#36 (mission 039): tools/tmdb-check.hl survives a dropped connection (on Error, asked again, failed lines) and resumes with --from N; tests/tmdbcheck.mjs 6/6 against a fake TMDBmre
  • 9c230516mission brief w39mre
  • 3609121dmission 038 docs + report (quota stop): README/kinds.md for #43 #42 #36, STATUS, LOG; HEAD browser gate 374/8 in the in-app daily run block (hour-boundary suspected, rerun pending), other gates green; #36 real run needs TMDB_READ_TOKENmre
  • a5d06e79tracker#36 (mission 038): tools/tmdb-check.hl + lib/tmdbcheck.hl — every live migrated title's migrated title + year against its TMDB entry (one GET /movie|tv/<id>?append_to_response=alternative_titles, 260 ms apart, passing failures asked once more): ok / ambiguous (one of title, year disagrees) / clear (both, or 404); read-only; token only from TMDB_READ_TOKEN; --stored = a title-only preview from the TMDB names already stored (no request). Tested against a fake TMDB (.scratch/w38/t36): 7 titles, 3 ok, 2 ambiguous, 2 clear, data unchangedmre
  • 982554f3tracker#42 (mission 038): series pages — an unknown slug no longer scans every title (lib/shows.hl showBySlug: the slug map only; new titles are noted by refreshTitleCaches, which both title puts already reach): 10.7–15.7 s → 0.013 s on the real copy (the one-request-at-a-time server was blocked meanwhile); cast/crew rows take name + slug from a person-link cache (personLinkOf) instead of whole person records (SNL: 303 cast with 4,761 credits, ~49k allocations per render) — allocations per series page −5…−40 %; HTML of 20 pages identical to beforemre
  • 4586b6e3docker-compose: init: true (hybriel#156)mre
  • 085c2e5ftracker#43 (mission 038): TVmaze's type makes Variety / Talk Show / Game Show / Reality / News / Panel Show / Award Show / Sports titles Shows (TMDB calls Saturday Night Live UK Scripted) — the #43 part of parked w37-wip a79c8c1 only (no re-vendor, no cache volume): tvmazeType stored by the sync and the TVmaze status backfill (asks every series with a TVmaze id once), kindOfTitle/kindWithTvmaze, tools/tvmaze-types.hl, gate fixture Variety Gate Series; start waits 150 smre
  • 4fa303b5mission brief w38mre
  • 975f6843tracker#41 (mission 037b): no request tap — an old /movies|/series slug renders the keeper's page (slug map) with og:url naming the keeper's address, /shows/ 301; no moved slug in any page's HTML. Real copy 168/168 old addresses, 0 moved slugs in 54 pages, 300x + 1000x stress without a crash; gates on 06617221 380/0, 32/0, 53/0, 229/0, 26/0, theme 0mre
  • 6decff4ftracker#41 (mission 037): moved slugs no longer listed in every page's HTML — a request tap answers their 301 by a slug-map lookup (shows.hl movedTitlePath); catalog.hl buildIndex pushes genre lists in place (the quadratic copy hung the boot > 20 min on today's data); gate: no moved slug in 19 page kinds signed out/in, immediate 301 after a merge. Gates on hybriel 06617221 green (380/0, 32/0, 53/0, 229/0, 26/0, theme 0); real copy 84/84 301, 0 moved slugs in 54 pages. NOT deployable: the tap faults after ~38 answers on 06617221 (report 037).mre