gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd56e5054d56e5054Sync theme and shared tokens from layouts 17e0f03mred56e5054/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

  • d56e5054Sync theme and shared tokens from layouts 17e0f03mre
  • b1362547Re-vendor to Hybriel set day35mre
  • 89e10f08Records: mission 002 rollback wordingmre
  • 9485dee7Records: mission 002 brief (live step)mre
  • 9ba68fe7Records: S1b reviewedmre
  • 193dfff1Records: S1 result, S1b startedmre
  • 028ed776Records: mission 001 continuation S1bmre
  • 1838d7e8Records: S1 startedmre
  • a43fca3fRecords: mission 001 decided pointsmre
  • 41773828Records: statusmre
  • a82c0805Records: mission 001 brief (Style form)mre
  • a4bce6d1Records: ticket push subscriptionmre
  • 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
  • 0433e9a8jukebox: records: gitoria remote added and pushedmre
  • 1722d707jukebox: records: repo creation handed to Gitoriamre
  • b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
  • 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
  • b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
  • 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
  • 06c548d6jukebox JB15: report, log linemre