jukebox
All repositories: gitoria
2.8 KB
// lib/schedule.hl — WHEN A SCAN RUNS, and how: once shortly after the start, then every `rescanMinutes` (settings, default// 30) after the last one finished, and whenever someone asks (POST /api/rescan sets `wanted`). project.hl calls `tick`// once a second. Only one scan runs at a time. JUKEBOX_AUTOSCAN=0 turns the timed scans off (tests); asking still works.// THE SCAN RUNS IN A CHILD PROCESS (tools/scan.hl, under nice and ionice): a scan inside the server would hold it up for// minutes (measured: 2,391 files, the server answered nothing until the scan was over). The child prints its progress as// lines; the server shows them in /api/stats, and drops its order lists when the child has exited.import { now } from 'hl:time'import { spawnArgs, dir } from 'hl:proc'import { loadSettings } from './settings.hl'import { scanState } from './library.hl'import { dropOrderCache } from './state.hl'import { envOr, storageRoot } from './util.hl'static autoscan = envOr('JUKEBOX_AUTOSCAN', '1') != '0'static armFirst = () => {scanState.nextAt = autoscan ? now() + 2000 : 0}// ask for a scan; answers false when one is already runningstatic requestScan = () => {if (scanState.running) { return false }scanState.wanted = truereturn true}// a progress line of the child → the statestatic applyLine = (line) => {if (!line.startsWith('JB-PROGRESS ')) { return false }p = JSON.parse(line.slice(12))st = scanStatest.phase = p.phasest.seen = p.seenst.added = p.addedst.updated = p.updatedst.removed = p.removedst.unreadable = p.unreadablereturn true}// start the child; answers the Process (project.hl listens to its events) or null when nothing was duestatic tick = () => {st = scanStateif (st.running) { return null }due = st.wanted || (st.nextAt > 0 && now() >= st.nextAt)if (!due) { return null }st.wanted = falsest.running = truest.phase = 'starting'st.seen = 0st.added = 0st.updated = 0st.removed = 0st.unreadable = 0st.startedAt = now()st.lastError = ''app = dir()child = spawnArgs(['nice', '-n', '19', 'ionice', '-c3', app + '/bin/hybriel', app + '/tools/scan.hl'], { env = { JUKEBOX_PROGRESS = '1' JUKEBOX_STORAGE = storageAbs() } })return child}// the child's storage path must be absolute (its script directory differs)static storageAbs = () => {if (storageRoot.startsWith('/')) { return storageRoot }return dir() + '/' + (storageRoot.startsWith('./') ? storageRoot.slice(2) : storageRoot)}// the child endedstatic finished = (exitCode) => {st = scanStatest.running = falsest.finishedAt = now()st.lastSeconds = (st.finishedAt - st.startedAt) / 1000if (exitCode != 0) { st.lastError = 'the scan process ended with status ' + exitCode }st.phase = 'idle'dropOrderCache()s = loadSettings()st.nextAt = autoscan ? now() + s.rescanMinutes * 60000 : 0}
Branches
Latest commits
- 8ed6da45|||PR README: media-player pin df9e7dfmre
- d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
- 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
- 1fecd6a7Merge branch 'records-day39' into mainJukebox
- d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
- 5fa19e89Merge branch 'day39' into mainJukebox
- cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
- f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
- 9f5c6532Merge branch 'status-22-pr' into mainJukebox
- 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
- 29d2029cRecords: PR-only rule (Kunibert)mre
- adacb0abRecords: neutral ticket rule, #22 decision recordedmre
- b2d6f121Records: #308 hold liftedmre
- 7ab67533Records: day39 on hold (hybriel#308)mre
- 22f21739Records: note rebase before mergemre
- f7368ea3Records: mission 011 day39, waits for lisbethmre
- 9515d2cdRecords: jb20 livemre
- 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
- 277a2722README: provenance linemre
- 4169ca6aREADME: sync pinmre