gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/lib/schedule.hl

2.8 KB

  1. // lib/schedule.hl — WHEN A SCAN RUNS, and how: once shortly after the start, then every `rescanMinutes` (settings, default
  2. // 30) after the last one finished, and whenever someone asks (POST /api/rescan sets `wanted`). project.hl calls `tick`
  3. // once a second. Only one scan runs at a time. JUKEBOX_AUTOSCAN=0 turns the timed scans off (tests); asking still works.
  4. // THE SCAN RUNS IN A CHILD PROCESS (tools/scan.hl, under nice and ionice): a scan inside the server would hold it up for
  5. // minutes (measured: 2,391 files, the server answered nothing until the scan was over). The child prints its progress as
  6. // lines; the server shows them in /api/stats, and drops its order lists when the child has exited.
  7. import { now } from 'hl:time'
  8. import { spawnArgs, dir } from 'hl:proc'
  9. import { loadSettings } from './settings.hl'
  10. import { scanState } from './library.hl'
  11. import { dropOrderCache } from './state.hl'
  12. import { envOr, storageRoot } from './util.hl'
  13. static autoscan = envOr('JUKEBOX_AUTOSCAN', '1') != '0'
  14. static armFirst = () => {
  15. scanState.nextAt = autoscan ? now() + 2000 : 0
  16. }
  17. // ask for a scan; answers false when one is already running
  18. static requestScan = () => {
  19. if (scanState.running) { return false }
  20. scanState.wanted = true
  21. return true
  22. }
  23. // a progress line of the child → the state
  24. static applyLine = (line) => {
  25. if (!line.startsWith('JB-PROGRESS ')) { return false }
  26. p = JSON.parse(line.slice(12))
  27. st = scanState
  28. st.phase = p.phase
  29. st.seen = p.seen
  30. st.added = p.added
  31. st.updated = p.updated
  32. st.removed = p.removed
  33. st.unreadable = p.unreadable
  34. return true
  35. }
  36. // start the child; answers the Process (project.hl listens to its events) or null when nothing was due
  37. static tick = () => {
  38. st = scanState
  39. if (st.running) { return null }
  40. due = st.wanted || (st.nextAt > 0 && now() >= st.nextAt)
  41. if (!due) { return null }
  42. st.wanted = false
  43. st.running = true
  44. st.phase = 'starting'
  45. st.seen = 0
  46. st.added = 0
  47. st.updated = 0
  48. st.removed = 0
  49. st.unreadable = 0
  50. st.startedAt = now()
  51. st.lastError = ''
  52. app = dir()
  53. child = spawnArgs(['nice', '-n', '19', 'ionice', '-c3', app + '/bin/hybriel', app + '/tools/scan.hl'], { env = { JUKEBOX_PROGRESS = '1' JUKEBOX_STORAGE = storageAbs() } })
  54. return child
  55. }
  56. // the child's storage path must be absolute (its script directory differs)
  57. static storageAbs = () => {
  58. if (storageRoot.startsWith('/')) { return storageRoot }
  59. return dir() + '/' + (storageRoot.startsWith('./') ? storageRoot.slice(2) : storageRoot)
  60. }
  61. // the child ended
  62. static finished = (exitCode) => {
  63. st = scanState
  64. st.running = false
  65. st.finishedAt = now()
  66. st.lastSeconds = (st.finishedAt - st.startedAt) / 1000
  67. if (exitCode != 0) { st.lastError = 'the scan process ended with status ' + exitCode }
  68. st.phase = 'idle'
  69. dropOrderCache()
  70. s = loadSettings()
  71. st.nextAt = autoscan ? now() + s.rescanMinutes * 60000 : 0
  72. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre