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
- 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
- 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