gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit12595e4712595e47mission 035: theme re-vendored from layouts.worldapi.org 0222f67 (two corner radii: radiusSmall 5px, radiusLarge 10px); the tracker's 18 own radii -> radiusSmall/radiusLarge (--layout-radius is gone); check-theme 0; gates 379/0, 32/0, 53/0, 229/0, 26/0; real copy: every computed radius in {0, 5px, 10px, 50%}mre12595e47/lib/jobs.hl

46.6 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 notFound = 0 requests = 0 errors = 0 retries = 0 }
  289. statusLine = () => {
  290. b = statusTotals
  291. return b.series + '/' + statusQueue.length + ' series, stored=' + b.stored + ' 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 (' + (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. }
  317. statusTotals.requests = statusTotals.requests + r.tvmazeRequests
  318. statusTotals.errors = statusTotals.errors + r.errors
  319. if (r.error != '') { console.log('tvmaze status: ' + r.error) }
  320. statusNextAt = now() + (repairFast ? 20 : pauseMsAfter(r))
  321. finished = statusPos >= statusQueue.length
  322. if (finished || statusPos % 25 == 0) { persistSync() }
  323. if (!finished && !r.retry && statusTotals.series > 0 && statusTotals.series % repairLogEvery == 0) { console.log('tvmaze status: ' + statusLine()) }
  324. if (finished) {
  325. statusDone = true
  326. console.log('tvmaze status done: ' + statusLine())
  327. }
  328. return null
  329. }
  330. // ---- THE KIND BACKFILL (tracker.worldapi.org#21, mission 068, backfill.hl kindTitle) ----------------------------------
  331. // Every TV title without a stored kind gets one: a title with a show genre (Reality / Talk / News) from the stored data (no
  332. // request), any other from TMDB's TV type (ONE request: GET /tv/<id>, then 260 ms pause). Starts once the adult backfill is
  333. // 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
  334. // /series to /shows at once (catalog.hl refreshCatalogShow). RESUMES after a restart (the queue = every TV title still without
  335. // a kind); a 404 stores the genres' answer + `kindCheck`; a passing failure goes to the back of the queue, at most 2 more tries
  336. // per start. Log: start, every TRACKER_KINDS_LOG_EVERY (500) titles, done. TRACKER_KINDS=0 switches it off; without
  337. // TMDB_READ_TOKEN it is off too (the lists still split by genre: shows.hl titleKind).
  338. kindsOn = env('TRACKER_KINDS') != '0' && syncEnabled
  339. kindsLogEvery = env('TRACKER_KINDS_LOG_EVERY') != null ? toNumber(env('TRACKER_KINDS_LOG_EVERY')) : 500
  340. kindQueue = null
  341. kindPos = 0
  342. kindDone = false
  343. kindAt = 0
  344. kindNextAt = 0
  345. kindTries = {}
  346. kindTotals = { titles = 0 series = 0 shows = 0 fromGenre = 0 moved = 0 requests = 0 errors = 0 retries = 0 }
  347. kindLine = () => {
  348. k = kindTotals
  349. return k.titles + '/' + kindQueue.length + ' titles, series=' + k.series + ' shows=' + k.shows + ' fromGenre=' + k.fromGenre + ' moved=' + k.moved
  350. + ' requests=' + k.requests + ' errors=' + k.errors + ' retries=' + k.retries + ' seconds=' + ((now() - kindAt) / 1000)
  351. }
  352. kindTick = () => {
  353. if (kindDone) { return null }
  354. t = now()
  355. if (kindQueue == null) {
  356. kindQueue = kindBackfillQueue()
  357. kindAt = t
  358. console.log('kind backfill: start, ' + kindQueue.length + ' TV titles without a kind (' + (now() - t) + ' ms to find them)')
  359. if (kindQueue.length == 0) { kindDone = true console.log('kind backfill: nothing to do') }
  360. return null
  361. }
  362. if (t < kindNextAt) { return null }
  363. id = kindQueue[kindPos]
  364. kindPos = kindPos + 1
  365. r = kindTitle(id)
  366. if (r.retry && (kindTries[id] == null || kindTries[id] < 2)) {
  367. kindTries[id] = kindTries[id] == null ? 1 : kindTries[id] + 1
  368. q = kindQueue
  369. q.push(id)
  370. kindQueue = q
  371. kindTotals.retries = kindTotals.retries + 1
  372. } else {
  373. kindTotals.titles = kindTotals.titles + 1
  374. if (r.kind == 'show') { kindTotals.shows = kindTotals.shows + 1 } else if (r.kind == 'series') { kindTotals.series = kindTotals.series + 1 }
  375. if (r.fromGenre) { kindTotals.fromGenre = kindTotals.fromGenre + 1 }
  376. }
  377. kindTotals.requests = kindTotals.requests + r.requests
  378. kindTotals.errors = kindTotals.errors + r.errors
  379. if (r.error != '') { console.log('kind backfill: ' + r.error) }
  380. // until now the genres decided (no show genre = series): only a title TMDB's type makes a SHOW changes its list
  381. if (r.kind == 'show' && !r.fromGenre) {
  382. refreshCatalogShow(id)
  383. kindTotals.moved = kindTotals.moved + 1
  384. }
  385. took = now() - t
  386. if (took > 1500) { console.log('kind backfill: slow step, show ' + id + ', ' + took + ' ms') }
  387. kindNextAt = now() + r.requests * msPerRequest
  388. finished = kindPos >= kindQueue.length
  389. if (finished || kindPos % 25 == 0) { persistSync() }
  390. if (!finished && kindTotals.titles > 0 && kindTotals.titles % kindsLogEvery == 0 && !r.retry) { console.log('kind backfill: ' + kindLine()) }
  391. if (finished) {
  392. kindDone = true
  393. console.log('kind backfill done: ' + kindLine())
  394. }
  395. return null
  396. }
  397. // ---- THE CREDITS JOB (tracker.worldapi.org#28, credits.hl creditsStep) ------------------------------------------------------
  398. // Every PUBLIC title (adult == false; adult/unknown are skipped) with a TMDB id gets its whole cast + crew once: at start (after
  399. // the adult backfill and the details repair are done) the job collects every such title without `creditsAt` and asks ONE per
  400. // step on the app's clock — one request (details + credits / aggregate_credits), then 260 ms (≤ 40 / 10 s); requests queued
  401. // meanwhile are served between two steps. Titles the details repair, the daily sync or an import already did carry `creditsAt`
  402. // and are not asked. RESUMES after a restart (the queue = what still has no credits); TMDB 404 / adult → `creditsCheck`, never
  403. // asked again; a passing failure goes to the back of the queue (at most 2 more tries per start). Waits while the daily sync
  404. // runs. Persisted (shows + people only) every 25 titles + at the end. Log: start, every TRACKER_CREDITS_LOG_EVERY (500) titles, failures, slow steps
  405. // (> 1.5 s), done. TRACKER_CREDITS=0 switches it off (off too without TMDB_READ_TOKEN). TRACKER_CREDITS_FAST=1 (the gate, fake
  406. // TMDB only): 20 ms pause instead.
  407. // MEMORY LIMIT: the runtime never frees what a fetch + JSON parse took (mission 061 measured ~6 MB more RSS per title on the real
  408. // 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
  409. // step the job reads its own RSS (/proc/self/status); above TRACKER_CREDITS_MAX_MB (16000) it PAUSES for this process (logged) and
  410. // goes on with what is left after the next restart (deploy, `docker restart`).
  411. creditsOn = env('TRACKER_CREDITS') != '0' && syncEnabled
  412. creditsFast = env('TRACKER_CREDITS_FAST') == '1'
  413. creditsLogEvery = env('TRACKER_CREDITS_LOG_EVERY') != null ? toNumber(env('TRACKER_CREDITS_LOG_EVERY')) : 500
  414. creditsQueueIds = null
  415. creditsPos = 0
  416. creditsDone = false
  417. creditsAt = 0
  418. creditsNextAt = 0
  419. creditsTries = {}
  420. creditsMaxMb = env('TRACKER_CREDITS_MAX_MB') != null ? toNumber(env('TRACKER_CREDITS_MAX_MB')) : 16000
  421. creditsTotals = { titles = 0 done = 0 failed = 0 cast = 0 crew = 0 people = 0 requests = 0 retries = 0 slowest = 0 }
  422. // this process' resident memory in MB (VmRSS), 0 when unknown
  423. rssMb = () => {
  424. if (!exists('/proc/self/status')) { return 0 }
  425. t = readFile('/proc/self/status')
  426. i = t != null ? t.indexOf('VmRSS:') : -1
  427. if (i < 0) { return 0 }
  428. kb = toNumber(t.slice(i + 6, t.indexOf('kB', i)).trim())
  429. return kb != null ? (kb - kb % 1024) / 1024 : 0
  430. }
  431. creditsLine = () => {
  432. b = creditsTotals
  433. return b.titles + '/' + creditsQueueIds.length + ' titles, done=' + b.done + ' failed=' + b.failed + ' cast=' + b.cast + ' crew=' + b.crew + ' people=' + b.people
  434. + ' requests=' + b.requests + ' retries=' + b.retries + ' slowestStepMs=' + b.slowest + ' seconds=' + ((now() - creditsAt) / 1000)
  435. }
  436. creditsTick = () => {
  437. if (creditsDone) { return null }
  438. t = now()
  439. if (creditsQueueIds == null) {
  440. creditsQueueIds = creditsQueue()
  441. creditsAt = t
  442. console.log('credits job: start, ' + creditsQueueIds.length + ' titles without credits (' + (now() - t) + ' ms to find them)')
  443. if (creditsQueueIds.length == 0) { creditsDone = true console.log('credits job: nothing to do') }
  444. return null
  445. }
  446. if (t < creditsNextAt) { return null }
  447. mb = rssMb()
  448. if (mb > creditsMaxMb) {
  449. creditsDone = true
  450. persistCredits()
  451. console.log('credits job: paused, RSS ' + mb + ' MB > TRACKER_CREDITS_MAX_MB ' + creditsMaxMb + ' — ' + (creditsQueueIds.length - creditsPos) + ' titles left for the next start; ' + creditsLine())
  452. return null
  453. }
  454. id = creditsQueueIds[creditsPos]
  455. creditsPos = creditsPos + 1
  456. r = creditsStep(id)
  457. took = now() - t
  458. if (took > creditsTotals.slowest) { creditsTotals.slowest = took }
  459. if (took > 1500) { console.log('credits job: slow step, show ' + id + ', ' + took + ' ms, cast=' + r.cast + ' crew=' + r.crew + ' people=' + r.people) }
  460. if (r.error != '') { console.log('credits job: ' + r.error) }
  461. creditsNextAt = now() + (creditsFast ? 20 : r.requests * msPerRequest)
  462. if (r.retry && (creditsTries[id] == null || creditsTries[id] < 2)) {
  463. creditsTries[id] = creditsTries[id] == null ? 1 : creditsTries[id] + 1
  464. q = creditsQueueIds
  465. q.push(id)
  466. creditsQueueIds = q
  467. creditsTotals.retries = creditsTotals.retries + 1
  468. } else if (!r.skipped) {
  469. creditsTotals.titles = creditsTotals.titles + 1
  470. if (r.error != '') { creditsTotals.failed = creditsTotals.failed + 1 } else { creditsTotals.done = creditsTotals.done + 1 }
  471. creditsTotals.cast = creditsTotals.cast + r.cast
  472. creditsTotals.crew = creditsTotals.crew + r.crew
  473. creditsTotals.people = creditsTotals.people + r.people
  474. }
  475. creditsTotals.requests = creditsTotals.requests + r.requests
  476. finished = creditsPos >= creditsQueueIds.length
  477. if (finished || creditsPos % 25 == 0) { persistCredits() }
  478. if (!finished && !r.skipped && !r.retry && creditsTotals.titles > 0 && creditsTotals.titles % creditsLogEvery == 0) { console.log('credits job: ' + creditsLine()) }
  479. if (finished) {
  480. creditsDone = true
  481. console.log('credits job done: ' + creditsLine())
  482. }
  483. return null
  484. }
  485. // ---- THE DUPLICATE MERGE (tracker.worldapi.org#31, mission 026, merge.hl) ----------------------------------------------------
  486. // The old tracker's duplicate titles (same type + TMDB id) become ONE record each — follows, watches, seasons, cast, credits and
  487. // timelines moved to the keeper, the others kept as tombstones whose slug and short id lead to it — and seasons that name a title
  488. // without being in its list are merged into their twin or linked, and (tracker#39) a season's double episodes into one (merge.hl
  489. // has the rules). On its own clock (every 0.1 s) ONE
  490. // item per tick (a duplicate group, or one title's stray seasons; no TMDB), so pages are served between two items. The TMDB jobs
  491. // below (daily sync, backfills, repair, credits, collections) wait until it is done: none of them works on a record that is about
  492. // to become a tombstone. RESUMES after a restart (the queue = what is still duplicated / stray). Persisted every 10 items and at
  493. // the end. Log: start, one line per tombstone / repaired title, done; with nothing to do one line. TRACKER_MERGE=0 switches it off.
  494. mergeOn = env('TRACKER_MERGE') != '0'
  495. mergeRun = { started = false done = !mergeOn at = 0 items = 0 slowest = 0 }
  496. mergeTick = () => {
  497. if (mergeRun.done) { return null }
  498. t = now()
  499. if (!mergeRun.started) {
  500. mergeRun.started = true
  501. mergeRun.at = t
  502. n = mergePrepare()
  503. st = mergeStats()
  504. 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)')
  505. if (n == 0) {
  506. mergeRun.done = true
  507. console.log('merge: nothing to do')
  508. }
  509. return null
  510. }
  511. r = mergeStep()
  512. mergeRun.items = mergeRun.items + 1
  513. took = now() - t
  514. if (took > mergeRun.slowest) { mergeRun.slowest = took }
  515. if (took > 1500) { console.log('merge: slow step, ' + took + ' ms') }
  516. if (r.done || mergeRun.items % 10 == 0) { persistMerge() }
  517. if (r.done) {
  518. mergeRun.done = true
  519. console.log('merge done: ' + mergeLine() + ' items=' + mergeRun.items + ' slowestStepMs=' + mergeRun.slowest + ' seconds=' + ((now() - mergeRun.at) / 1000))
  520. }
  521. return null
  522. }
  523. if (!mergeOn) { console.log('merge: off (TRACKER_MERGE=0)') }
  524. else {
  525. mergeClock = every(0.1)
  526. on mergeClock.tick(x) {
  527. mergeTick()
  528. return null
  529. }
  530. }
  531. // 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
  532. // between, only while no daily run is going on; the details repair only when neither has work (then its TVmaze status part,
  533. // tracker#35); then the kind backfill (#21);
  534. // the credits job (#28) last
  535. clockOn = syncOn || backfillOn || repairOn || kindsOn || creditsOn
  536. clockTicks = 0
  537. 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') + ')') }
  538. if (!backfillOn) { console.log('adult backfill: off (' + (syncEnabled ? 'TRACKER_BACKFILL=0' : 'no TMDB_READ_TOKEN') + ')') }
  539. if (!repairOn) { console.log('details repair: off (' + (syncEnabled ? 'TRACKER_REPAIR=0' : 'no TMDB_READ_TOKEN') + ')') }
  540. if (!creditsOn) { console.log('credits job: off (' + (syncEnabled ? 'TRACKER_CREDITS=0' : 'no TMDB_READ_TOKEN') + ')') }
  541. if (!kindsOn) { console.log('kind backfill: off (' + (syncEnabled ? 'TRACKER_KINDS=0' : 'no TMDB_READ_TOKEN') + ')') }
  542. if (clockOn) {
  543. syncClock = every(0.1)
  544. on syncClock.tick(x) {
  545. clockTicks = clockTicks + 1
  546. // tracker#31: not before the duplicate merge is done
  547. if (!mergeRun.done) { return null }
  548. busy = syncQueue.length > 0 || syncPlanning
  549. if (syncOn && clockTicks % 10 == 0) { syncTick() }
  550. free = !busy && syncQueue.length == 0 && !syncPlanning
  551. if (backfillOn && free && !backfillDone) { backfillTick() }
  552. else if (repairOn && free && (!backfillOn || backfillDone) && !repairDone) { repairTick() }
  553. else if (repairOn && free && (!backfillOn || backfillDone) && !statusDone) { statusTick() }
  554. else if (kindsOn && free && (!backfillOn || backfillDone) && (!repairOn || (repairDone && statusDone)) && !kindDone) { kindTick() }
  555. else if (creditsOn && free && (!backfillOn || backfillDone) && (!repairOn || (repairDone && statusDone)) && (!kindsOn || kindDone)) { creditsTick() }
  556. return null
  557. }
  558. }
  559. // ---- THE SHORT-ID BACKFILL (mission 060, tracker.worldapi.org#27, shortids.hl shortIdStep) ----------------------------
  560. // 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,
  561. // as many records as fit into TRACKER_SHORTID_BUDGET_MS (default 40 ms) — no TMDB, only the tables — so a page waits at
  562. // most one batch. The queue is built at start (`ensureShortIds`) from what is still missing = it resumes after a restart.
  563. // Persisted every 20 batches and at the end. Log: start, every TRACKER_SHORTID_LOG_EVERY (5000) records, done; with nothing
  564. // to do one line. TRACKER_SHORTIDS=0 switches it off (the gate's resume check).
  565. shortIdOn = env('TRACKER_SHORTIDS') != '0'
  566. shortIdBudget = env('TRACKER_SHORTID_BUDGET_MS') != null ? toNumber(env('TRACKER_SHORTID_BUDGET_MS')) : 40
  567. shortIdLogEvery = env('TRACKER_SHORTID_LOG_EVERY') != null ? toNumber(env('TRACKER_SHORTID_LOG_EVERY')) : 5000
  568. shortIdRun = { at = 0 batches = 0 assigned = 0 nextLog = 0 slowest = 0 slowestPersist = 0 done = false }
  569. shortIdTick = () => {
  570. if (shortIdRun.done) { return null }
  571. t = now()
  572. r = shortIdStep(shortIdBudget)
  573. took = now() - t
  574. shortIdRun.batches = shortIdRun.batches + 1
  575. shortIdRun.assigned = shortIdRun.assigned + r.assigned
  576. if (took > shortIdRun.slowest) { shortIdRun.slowest = took }
  577. if (r.done || shortIdRun.batches % 20 == 0) {
  578. tp = now()
  579. persistShortIds()
  580. if (now() - tp > shortIdRun.slowestPersist) { shortIdRun.slowestPersist = now() - tp }
  581. }
  582. if (!r.done && shortIdRun.assigned >= shortIdRun.nextLog) {
  583. console.log('short ids: ' + shortIdRun.assigned + ' assigned, ' + r.left + ' left, ' + ((now() - shortIdRun.at) / 1000) + ' s')
  584. shortIdRun.nextLog = shortIdRun.nextLog + shortIdLogEvery
  585. }
  586. if (r.done) {
  587. shortIdRun.done = true
  588. s = shortIdStats()
  589. 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))
  590. }
  591. return null
  592. }
  593. if (!shortIdOn) { console.log('short ids: backfill off (TRACKER_SHORTIDS=0), ' + shortIdsLeft() + ' records without one') }
  594. else if (shortIdsLeft() == 0) { console.log('short ids: nothing to do') }
  595. else {
  596. shortIdRun.at = now()
  597. shortIdRun.nextLog = shortIdLogEvery
  598. console.log('short ids: backfill start, ' + shortIdsLeft() + ' records, ' + shortIdBudget + ' ms per batch')
  599. shortIdClock = every(0.1)
  600. on shortIdClock.tick(x) {
  601. shortIdTick()
  602. return null
  603. }
  604. }
  605. // ---- THE COLLECTION SEED (mission 066, tracker.worldapi.org#19, collections.hl seedStep) ------------------------------------
  606. // TMDB's movie collections become timelines: ONE movie per step (its details; once per collection the collection's parts), on
  607. // 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
  608. // sync or the adult backfill runs (they share TMDB's limit and the backfill brings the adult flags it skips by); since the merge
  609. // (mission 070) also until the details repair, the kind backfill and the credits job are done (or paused) — the LAST job. The queue =
  610. // every non-adult movie without a check row, so it RESUMES after a restart and does nothing once all are checked. Persisted
  611. // every TRACKER_COLLECTIONS_PERSIST_EVERY (25) steps and at the end. Log: start, every TRACKER_COLLECTIONS_LOG_EVERY (500) movies, done.
  612. // TRACKER_COLLECTIONS=0 switches it off; without TMDB_READ_TOKEN it is off too.
  613. collectionsOn = env('TRACKER_COLLECTIONS') != '0' && syncEnabled
  614. collectionsLogEvery = env('TRACKER_COLLECTIONS_LOG_EVERY') != null ? toNumber(env('TRACKER_COLLECTIONS_LOG_EVERY')) : 500
  615. collectionsPersistEvery = env('TRACKER_COLLECTIONS_PERSIST_EVERY') != null ? toNumber(env('TRACKER_COLLECTIONS_PERSIST_EVERY')) : 25
  616. collectionRun = { at = 0 steps = 0 nextAt = 0 done = false started = false slowest = 0 }
  617. collectionTick = () => {
  618. if (collectionRun.done) { return null }
  619. // mission 070 (merge): also behind the details repair, the kind backfill and the credits job — one TMDB pacing at a time
  620. if (!mergeRun.done || syncQueue.length > 0 || syncPlanning || (backfillOn && !backfillDone) || (repairOn && (!repairDone || !statusDone)) || (kindsOn && !kindDone) || (creditsOn && !creditsDone)) { return null }
  621. t = now()
  622. if (!collectionRun.started) {
  623. collectionRun.started = true
  624. collectionRun.at = t
  625. n = seedPrepare()
  626. console.log('collections: start, ' + n + ' movies to check (' + (now() - t) + ' ms to find them)')
  627. if (n == 0) { collectionRun.done = true console.log('collections: nothing to do') }
  628. return null
  629. }
  630. if (t < collectionRun.nextAt) { return null }
  631. r = seedStep()
  632. if (r.done) {
  633. collectionRun.done = true
  634. seedPersist()
  635. console.log('collections done: ' + seedLine() + ' seconds=' + ((now() - collectionRun.at) / 1000) + ' slowestStepMs=' + collectionRun.slowest)
  636. return null
  637. }
  638. collectionRun.steps = collectionRun.steps + 1
  639. took = now() - t
  640. if (took > collectionRun.slowest) { collectionRun.slowest = took }
  641. if (r.error != '') { console.log('collections: ' + r.error) }
  642. if (took > 1500) { console.log('collections: slow step, ' + took + ' ms') }
  643. collectionRun.nextAt = now() + r.requests * msPerRequest
  644. if (collectionRun.steps % collectionsPersistEvery == 0) { seedPersist() }
  645. if (collectionRun.steps % collectionsLogEvery == 0) { console.log('collections: ' + seedLine() + ' seconds=' + ((now() - collectionRun.at) / 1000)) }
  646. return null
  647. }
  648. if (!collectionsOn) { console.log('collections: off (' + (syncEnabled ? 'TRACKER_COLLECTIONS=0' : 'no TMDB_READ_TOKEN') + ')') }
  649. else {
  650. collectionClock = every(0.1)
  651. on collectionClock.tick(x) {
  652. collectionTick()
  653. return null
  654. }
  655. }
  656. // ---- THE CURATED FRANCHISE SEED (tracker.worldapi.org#40, mission 034, franchiseseed.hl) --------------------------------------
  657. // data/franchises.json → franchises + timelines, movies AND series. The LAST start job: it waits until the merge, the daily sync,
  658. // the backfills, the details repair, the kind backfill, the credits job and the collection seed are done (or off) — the
  659. // 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
  660. // 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
  661. // requests (≤ 40 TMDB / 10 s, ≤ 20 TVmaze / 10 s); the queue done, the build (no TMDB). RESUMES after a restart (the queue = the
  662. // titles still missing), IDEMPOTENT (nothing missing + nothing changed = no write). Without TMDB_READ_TOKEN it only builds from
  663. // what we have. Log: start, every import, every TRACKER_FRANCHISE_SEED_LOG_EVERY (25) imports, done.
  664. // TRACKER_FRANCHISE_SEED=0 switches it off; TRACKER_FRANCHISE_FILE another data file (the gate's).
  665. franchiseSeedOn = env('TRACKER_FRANCHISE_SEED') != '0'
  666. franchiseSeedLogEvery = env('TRACKER_FRANCHISE_SEED_LOG_EVERY') != null ? toNumber(env('TRACKER_FRANCHISE_SEED_LOG_EVERY')) : 25
  667. franchiseRun = { at = 0 steps = 0 nextAt = 0 done = false started = false slowest = 0 }
  668. franchiseSeedTick = () => {
  669. if (franchiseRun.done) { return null }
  670. if (!mergeRun.done || syncQueue.length > 0 || syncPlanning || (backfillOn && !backfillDone) || (repairOn && (!repairDone || !statusDone)) || (kindsOn && !kindDone) || (creditsOn && !creditsDone) || (collectionsOn && !collectionRun.done)) { return null }
  671. t = now()
  672. if (!franchiseRun.started) {
  673. franchiseRun.started = true
  674. franchiseRun.at = t
  675. n = franchiseSeedPrepare(syncEnabled)
  676. st = franchiseSeedStats()
  677. if (!st.file) {
  678. franchiseRun.done = true
  679. console.log('franchise seed: ' + st.error + ' — nothing seeded')
  680. return null
  681. }
  682. 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)')
  683. return null
  684. }
  685. if (t < franchiseRun.nextAt) { return null }
  686. r = franchiseSeedStep()
  687. took = now() - t
  688. if (took > franchiseRun.slowest) { franchiseRun.slowest = took }
  689. if (r.error != '') { console.log('franchise seed: ' + r.error) }
  690. if (took > 1500) { console.log('franchise seed: slow step, ' + took + ' ms') }
  691. if (r.done) {
  692. franchiseRun.done = true
  693. console.log('franchise seed done: ' + franchiseSeedLine() + ' seconds=' + ((now() - franchiseRun.at) / 1000) + ' slowestStepMs=' + franchiseRun.slowest)
  694. return null
  695. }
  696. franchiseRun.steps = franchiseRun.steps + 1
  697. franchiseRun.nextAt = now() + pauseMsAfter(r)
  698. if (franchiseRun.steps % franchiseSeedLogEvery == 0) { console.log('franchise seed: ' + franchiseSeedLine() + ' seconds=' + ((now() - franchiseRun.at) / 1000)) }
  699. return null
  700. }
  701. if (!franchiseSeedOn) { console.log('franchise seed: off (TRACKER_FRANCHISE_SEED=0)') }
  702. else {
  703. if (!syncEnabled) { console.log('franchise seed: no TMDB_READ_TOKEN — builds from the titles we have, imports nothing') }
  704. franchiseClock = every(0.1)
  705. on franchiseClock.tick(x) {
  706. franchiseSeedTick()
  707. return null
  708. }
  709. }
  710. // ---- THE ART BACKFILL + THE PHOTO BACKFILL (mission 032: tracker#33's The Remaining, tracker#37's /people) --------------------
  711. // The LAST start jobs, after the franchise seed (and everything it waits for): first every public title without a poster file /
  712. // public movie without runtime (backfill.hl artTitle: the poster, else the backdrop, + runtime), then every person without a photo
  713. // 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
  714. // / 10 s). Resume after a restart (`artAt`/`artCheck`, `photoCheck`); a passing failure goes to the back of the queue (at most 2
  715. // more tries per start). Persisted every 25 items and at the end; the photo job pauses above TRACKER_PHOTOS_MAX_MB (16000) RSS
  716. // like the credits job. Log: start, every TRACKER_ART_LOG_EVERY (100) / TRACKER_PHOTOS_LOG_EVERY (1000) items, done.
  717. // TRACKER_ART=0 / TRACKER_PHOTOS=0 switch them off; without TMDB_READ_TOKEN they are off.
  718. artOn = env('TRACKER_ART') != '0' && syncEnabled
  719. photosOn = env('TRACKER_PHOTOS') != '0' && syncEnabled
  720. artLogEvery = env('TRACKER_ART_LOG_EVERY') != null ? toNumber(env('TRACKER_ART_LOG_EVERY')) : 100
  721. photosLogEvery = env('TRACKER_PHOTOS_LOG_EVERY') != null ? toNumber(env('TRACKER_PHOTOS_LOG_EVERY')) : 1000
  722. photosMaxMb = env('TRACKER_PHOTOS_MAX_MB') != null ? toNumber(env('TRACKER_PHOTOS_MAX_MB')) : 16000
  723. 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 }
  724. 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 }
  725. artLine = () => {
  726. b = artRun
  727. return b.titles + '/' + b.queue.length + ' titles, posters=' + b.posters + ' backdrops=' + b.backdrops + ' runtimes=' + b.runtimes + ' failed=' + b.failed
  728. + ' requests=' + b.requests + ' retries=' + b.retries + ' slowestStepMs=' + b.slowest + ' seconds=' + ((now() - b.at) / 1000)
  729. }
  730. photoLine = () => {
  731. b = photoRun
  732. return b.people + '/' + b.queue.length + ' people, photos=' + b.found + ' none=' + b.none + ' failed=' + b.failed + ' requests=' + b.requests + ' retries=' + b.retries
  733. + ' slowestStepMs=' + b.slowest + ' seconds=' + ((now() - b.at) / 1000)
  734. }
  735. // everything the two wait for: the other start jobs (one TMDB pacing at a time)
  736. lastJobsMayRun = () => {
  737. return mergeRun.done && syncQueue.length == 0 && !syncPlanning && (!backfillOn || backfillDone) && (!repairOn || (repairDone && statusDone)) && (!kindsOn || kindDone)
  738. && (!creditsOn || creditsDone) && (!collectionsOn || collectionRun.done) && (!franchiseSeedOn || franchiseRun.done)
  739. }
  740. artTick = () => {
  741. t = now()
  742. if (artRun.queue == null) {
  743. artRun.queue = artQueue()
  744. artRun.at = t
  745. console.log('art backfill: start, ' + artRun.queue.length + ' public titles without a poster file or a movie runtime (' + (now() - t) + ' ms to find them)')
  746. if (artRun.queue.length == 0) { artRun.done = true console.log('art backfill: nothing to do') }
  747. return null
  748. }
  749. if (t < artRun.nextAt) { return null }
  750. id = artRun.queue[artRun.pos]
  751. artRun.pos = artRun.pos + 1
  752. r = artTitle(id)
  753. took = now() - t
  754. if (took > artRun.slowest) { artRun.slowest = took }
  755. if (r.error != '') { console.log('art backfill: ' + r.error) }
  756. artRun.nextAt = now() + r.requests * msPerRequest
  757. artRun.requests = artRun.requests + r.requests
  758. if (r.retry && (artRun.tries[id] == null || artRun.tries[id] < 2)) {
  759. artRun.tries[id] = artRun.tries[id] == null ? 1 : artRun.tries[id] + 1
  760. q = artRun.queue
  761. q.push(id)
  762. artRun.queue = q
  763. artRun.retries = artRun.retries + 1
  764. } else if (r.requests > 0) {
  765. artRun.titles = artRun.titles + 1
  766. artRun.posters = artRun.posters + r.posters
  767. artRun.backdrops = artRun.backdrops + r.backdrops
  768. if (r.runtime != null) { artRun.runtimes = artRun.runtimes + 1 }
  769. if (r.error != '') { artRun.failed = artRun.failed + 1 }
  770. if (r.posters + r.backdrops > 0) { refreshCatalogShow(id) }
  771. }
  772. finished = artRun.pos >= artRun.queue.length
  773. if (finished || artRun.pos % 25 == 0) { persistSync() }
  774. if (!finished && artRun.titles > 0 && artRun.pos % artLogEvery == 0) { console.log('art backfill: ' + artLine()) }
  775. if (finished) {
  776. artRun.done = true
  777. console.log('art backfill done: ' + artLine())
  778. }
  779. return null
  780. }
  781. photoTick = () => {
  782. t = now()
  783. if (photoRun.queue == null) {
  784. photoRun.queue = photoQueue()
  785. photoRun.at = t
  786. console.log('photo backfill: start, ' + photoRun.queue.length + ' people without a photo source (' + (now() - t) + ' ms to find them)')
  787. if (photoRun.queue.length == 0) { photoRun.done = true console.log('photo backfill: nothing to do') }
  788. return null
  789. }
  790. if (t < photoRun.nextAt) { return null }
  791. mb = rssMb()
  792. if (mb > photosMaxMb) {
  793. photoRun.done = true
  794. persistPhotos()
  795. 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())
  796. return null
  797. }
  798. id = photoRun.queue[photoRun.pos]
  799. photoRun.pos = photoRun.pos + 1
  800. r = photoStep(id)
  801. took = now() - t
  802. if (took > photoRun.slowest) { photoRun.slowest = took }
  803. if (r.error != '') { console.log('photo backfill: ' + r.error) }
  804. photoRun.nextAt = now() + r.requests * msPerRequest
  805. photoRun.requests = photoRun.requests + r.requests
  806. if (r.retry && (photoRun.tries[id] == null || photoRun.tries[id] < 2)) {
  807. photoRun.tries[id] = photoRun.tries[id] == null ? 1 : photoRun.tries[id] + 1
  808. q = photoRun.queue
  809. q.push(id)
  810. photoRun.queue = q
  811. photoRun.retries = photoRun.retries + 1
  812. } else if (r.requests > 0) {
  813. photoRun.people = photoRun.people + 1
  814. if (r.found) { photoRun.found = photoRun.found + 1 } else if (r.error != '') { photoRun.failed = photoRun.failed + 1 } else { photoRun.none = photoRun.none + 1 }
  815. }
  816. finished = photoRun.pos >= photoRun.queue.length
  817. if (finished || photoRun.pos % 25 == 0) { persistPhotos() }
  818. if (!finished && photoRun.people > 0 && photoRun.pos % photosLogEvery == 0) { console.log('photo backfill: ' + photoLine()) }
  819. if (finished) {
  820. photoRun.done = true
  821. console.log('photo backfill done: ' + photoLine())
  822. }
  823. return null
  824. }
  825. if (!artOn) { console.log('art backfill: off (' + (syncEnabled ? 'TRACKER_ART=0' : 'no TMDB_READ_TOKEN') + ')') }
  826. if (!photosOn) { console.log('photo backfill: off (' + (syncEnabled ? 'TRACKER_PHOTOS=0' : 'no TMDB_READ_TOKEN') + ')') }
  827. if (artOn || photosOn) {
  828. lastClock = every(0.1)
  829. on lastClock.tick(x) {
  830. if (!lastJobsMayRun()) { return null }
  831. if (!artRun.done) { artTick() } else if (!photoRun.done) { photoTick() }
  832. return null
  833. }
  834. }

Branches

Latest commits

  • 12595e47mission 035: theme re-vendored from layouts.worldapi.org 0222f67 (two corner radii: radiusSmall 5px, radiusLarge 10px); the tracker's 18 own radii -> radiusSmall/radiusLarge (--layout-radius is gone); check-theme 0; gates 379/0, 32/0, 53/0, 229/0, 26/0; real copy: every computed radius in {0, 5px, 10px, 50%}mre
  • e7305014tracker: report 032 (art + photos)mre
  • fbb903cctracker#33/#37 (mission 032): a title without a TMDB poster gets its backdrop (w780, posterFromBackdrop, shown 2:3 centre-cropped); movies store runtime, the page shows release date + runtime; art backfill (public titles without poster file / movies without runtime) and person photo backfill (tmdbProfile / photoCheck) as the last start jobs (TRACKER_ART, TRACKER_PHOTOS; off in every gate start); gates 379/0, 32/0, 53/0, 229/0, 26/0, check-theme 0; live copy: art 4977 titles in 42 min (115 posters, 15 backdrops, 4609 runtimes), The Remaining shows its backdrop + 7 minmre
  • 4ecc67b2tracker: STATUS/LOG for the t38 + t40 merge (gates 374/0, 32/0, 53/0, 229/0, 26/0, check-theme 0; live copy checks)mre
  • e5945d2fMerge t40 (tracker#40 curated franchises, Franchises menu, superseded collections) into main: deploy.sh lists all six gates (browser, kinds, franchises, pager, franchiseseed, check-theme); search.hl keeps #37's personPhotoOf + #40's importWithCredits; pager gate runs with TRACKER_FRANCHISE_SEED=0; gates 374/0, 32/0, 53/0, 229/0, 26/0, check-theme 0mre
  • b638e99dMerge t38 (tracker#38 pagination, #35 Returning/Airing label) into main: LOG/STATUS keep both sides; pager gate follows #37's /people (everyone, last updated first: seed updatedAt); gates pager 229/0, browser 374/0mre
  • f8ffa4dbtracker: report 032 + The Remaining + #39mre
  • 7565a863tracker: LOG timemre
  • b10f00c8tracker#39: double episodes — migrated episodes whose TMDB id TMDB replaced are adopted by their number in the sync (old id -> migratedTmdbId); merge.hl step 3 merges each season's doubles at start (keeper: most watches > synced > first; watches moved/parked; tombstones into mergedEpisodes, nothing deleted); tools/count-duplicate-episodes.hl; gate fixture + paths-m039; live copy 850 -> 0 in 64 s; gates 373/0, 32/0, 52/0mre
  • 8f1d4542tracker#40: superseded collections — a TMDB collection timeline whose titles are all in one curated timeline is hidden (supersededBy; kept: own page + editor finder), set by the collection seed when it makes one and by the curated build (lifted when the cover is gone); partly covered ones join that franchise; no second widget (First Contact: only Star Trek — Prime); gate franchiseseed 26/0, browser 365/0, kinds 32/0, franchises 53/0, check-theme 0; real copy 24 supersededmre
  • 9448d643tracker#35 follow-up (mission 033): TVmaze 'Running' is labelled 'Returning', or 'Airing' while a non-special episode of the two newest seasons is released within today +-7 days (data unchanged); gate fixtures Running/Airing/Aired + a special; browser 366/0, kinds 32/0, franchises 52/0, pager 229/0, check-theme 0mre
  • 7d4b293dtracker#40: "Franchises" in the main menu (desktop header after People, phone sidebar) → /franchises, marked on franchise and timeline pages; gates 365/0, 32/0, 53/0, 24/0, check-theme 0mre
  • 8751adb8tracker: report 032mre
  • 9bce1f65tracker mission 032: STATUS gate files + the hour-boundary flakemre
  • 718bfb89tracker#37 (mission 032): /people = everyone, last updated first (updatedAt stamped by the person fill; view built at boot, touched people first at once), photo + name tiles (person colour) with the /movies pagination, /people/<letter> removed; photo = our file, tmdbProfile, a cast/crew entry's profile (in-memory map at boot), else the new 'no photo' placeholder; new cast/crew/created_by people keep tmdbProfile; search people rows with the photo; /settings = the heading only; util.hl sortDesc starts from sorted runs (same result, 105k: 1.6 s -> 0.15 s); gates 369/0, 32/0, 52/0, check-theme 0; README/STATUS/LOGmre
  • 93dfb0batracker#40 (mission 034): the curated franchises — data/franchises.json (17 franchises, 31 timelines, 285 TMDB titles, movies + series, in-universe/release order, 12 TMDB collections attached); lib/franchiseseed.hl + jobs.hl franchiseSeedTick (last start job, imports missing titles via details.hl importWithCredits = the search's Add, one per step paced, then one build; franchiseseed.db: editor changes win, the creator's same-name franchise adopted / timeline left alone, 404 remembered, resumable, idempotent); timeline heads 'N titles · in-universe order' (orderKind) and wrap on a phone; series pages show the widget; new gate tests/franchiseseed.mjs (5th in deploy.sh), the others run with TRACKER_FRANCHISE_SEED=0; gates 365/0, 32/0, 52/0, 24/0, check-theme 0; real copy 196 imported, 0 failed, 7 min, restart unchanged=31mre
  • 03ec792ftracker#38 (mission 033): pagination goes exactly to the clicked page — tilelist read the clicked button's text after pagination.hl's own handler had rebuilt the buttons (real clicks only); now li.current, else the button's own text; new gate tests/pager.mjs (5 lists x 11 pages, 390/1280, real + script clicks, Back/Forward) 229/0; browser 365/0, kinds 32/0, franchises 52/0, check-theme 0mre
  • 96ba683adeploy.sh: a gate without a 'passed,' line (check-theme) no longer ends the scriptmre
  • eb3b9205tracker: report 031mre
  • 9b5d2e89tracker mission 031: README (What it does, Test: four gates + the #32 checks, Files: theme/, new pages), STATUS (real copy, A/B load, how to repeat, open points), LOGmre