gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb8dac822b8dac822log: hybriel#309mreb8dac822/lib/jobs.hl

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

Branches

Latest commits

  • b8dac822log: hybriel#309mre
  • bf676d9elog: GC1 startedmre
  • c7828541mission GC1mre
  • ef949994conductor: neutral ticket rulemre
  • 89e91ef3log: RV39 livemre
  • ee2b677alog: RV39 deployingmre
  • c1c4178cMerge branch 'rv39'mre
  • a016e9abReapply "Merge branch 'rv39'"mre
  • f29e2502log: RV39 gate fixmre
  • d9fce111browser gate #26: exact frame count incl. SL1 people step (hybriel#308)mre
  • ad529ac1log: m572 verdictmre
  • 1b37d404RV39 on hold: hybriel#308mre
  • f9127c68Revert "Merge branch 'rv39'"mre
  • cd39f3d3log: hybriel#308mre
  • 51341d6elog: RV39 deploy startedmre
  • c9e29e06Merge branch 'rv39'mre
  • 0f8d1fdclog: RV39 report, browser 387/2mre
  • 50249b0bRV39: reportmre
  • 10ed9581compose: HL_GC_STATS=1mre
  • 955da19bRV39: re-vendor Hybriel set day39 (source 51e921d7, bin 6b481aa7)mre