Harden cross-session background persistence
Add periodic stream expiry sweeping, throttle thumbnail cache sync heavily while streams are active, and cap subtitle log context memory with LRU trimming to prevent long-run buildup.
@@ -18,6 +18,7 @@
18
18
- appendSubtitleDebugEntries({ videoName, entries, context }) - Appends client-side subtitle timing events to a timestamped per-video text log under the local temp cache (`<unix-ms>-videoname-subtitlelog.txt`), reusing one file per playback context and pruning the oldest files when the subtitle log cache exceeds 10 MB.
19
19
- getSubtitleDebugLogSnapshot() - Reports the subtitle log cache directory path, existence, file count, byte total, and latest file so the debug dashboard can expose it as a folder.
20
20
- pruneSubtitleDebugLogDir() - Enforces the 10 MB total subtitle-log cache budget by deleting the oldest log files first.
21
- subtitleLogFileByContext (LRU) - In-memory context-to-logfile mapping is trimmed with `SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX` (default 512, capped at 4096) to prevent unbounded session-key growth.
21
22
- subtitleDebugLogFilePath(videoName, timestampMs) - Resolves the timestamped subtitle timing log path for a video name using a filesystem-safe filename.
22
23
23
24
## server/image-subtitles.js
@@ -32,7 +33,7 @@
32
33
33
34
## server/thumbnail-cache.js
34
35
- syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files, video files (ffprobe/ffmpeg frame at ~10%), and 3D model files (STL/OBJ, isometric SVG projection via Sharp); removes stale thumbnails and updates manifest hashes.
35
- startThumbnailCacheLoop() - Schedules cache sync runs 5 seconds after boot and 5 seconds after each run completes.
36
- startThumbnailCacheLoop() - Schedules cache sync runs with adaptive cadence: default 5s when idle and heavily throttled while active streams exist (`THUMBNAIL_CACHE_ACTIVE_STREAM_DELAY_MS`, default 120000 ms).
36
37
- stopThumbnailCacheLoop() - Stops scheduled background cache sync runs.
37
38
- getCachedThumbnailInfo(sourceFile) - Returns cached thumbnail path/content-type when present; null when not ready.
38
39
- getThumbPathForSourceFile(sourceFile) - Maps an absolute source file path to its mirrored cache thumbnail path.
@@ -155,6 +156,7 @@
155
156
- hasInitialHlsAssets(outputDir) - Returns true once master.m3u8 or at least one variant playlist/segment exists in an HLS output directory; accepts named variant filenames (stream_video.m3u8) in addition to numeric ones.
156
157
- purgeStreamRootDir() - Best-effort recursive delete of the temporary HLS stream root; used at startup and by idle sweeps to remove orphaned stream session directories from previous runs.
157
158
- cleanupIdleStreamArtifacts() - Periodic idle-time cleaner that purges stream-root artifacts when there are no active sessions or leases, preventing stale segment directories from accumulating after failures.
159
- pruneExpiredSessions() sweep - A dedicated interval sweep (`VIDEO_STREAM_EXPIRY_SWEEP_MS`, default 60000 ms) reclaims expired leases/sessions even when no new stream requests arrive.
158
160
- getVideoStreamDebugSnapshot() - Returns active stream sessions with viewer counts and output-dir stats (segment counts/bytes), plus orphaned output directories left under the stream root for diagnostics.
159
161
- getVideoStreamDebugSnapshot() - Includes segment retention and prune telemetry per session (segmentRetentionCount, slowestViewerSegmentIndex, segmentPrunedUntil, lastSegmentPrune, streamPruneDeleteBatchSize) to explain growth or cleanup stalls in segment counts.
160
162
- STREAM_ROOT_DIR - Temporary root directory for active ffmpeg-backed stream sessions.
@@ -18,6 +18,7 @@18
- appendSubtitleDebugEntries({ videoName, entries, context }) - Appends client-side subtitle timing events to a timestamped per-video text log under the local temp cache (`<unix-ms>-videoname-subtitlelog.txt`), reusing one file per playback context and pruning the oldest files when the subtitle log cache exceeds 10 MB.
18
- appendSubtitleDebugEntries({ videoName, entries, context }) - Appends client-side subtitle timing events to a timestamped per-video text log under the local temp cache (`<unix-ms>-videoname-subtitlelog.txt`), reusing one file per playback context and pruning the oldest files when the subtitle log cache exceeds 10 MB.
19
- getSubtitleDebugLogSnapshot() - Reports the subtitle log cache directory path, existence, file count, byte total, and latest file so the debug dashboard can expose it as a folder.
19
- getSubtitleDebugLogSnapshot() - Reports the subtitle log cache directory path, existence, file count, byte total, and latest file so the debug dashboard can expose it as a folder.
20
- pruneSubtitleDebugLogDir() - Enforces the 10 MB total subtitle-log cache budget by deleting the oldest log files first.
20
- pruneSubtitleDebugLogDir() - Enforces the 10 MB total subtitle-log cache budget by deleting the oldest log files first.
21
- subtitleLogFileByContext (LRU) - In-memory context-to-logfile mapping is trimmed with `SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX` (default 512, capped at 4096) to prevent unbounded session-key growth.
21
- subtitleDebugLogFilePath(videoName, timestampMs) - Resolves the timestamped subtitle timing log path for a video name using a filesystem-safe filename.
22
- subtitleDebugLogFilePath(videoName, timestampMs) - Resolves the timestamped subtitle timing log path for a video name using a filesystem-safe filename.
22
23
23
## server/image-subtitles.js
24
## server/image-subtitles.js
@@ -32,7 +33,7 @@32
33
33
## server/thumbnail-cache.js
34
## server/thumbnail-cache.js
34
- syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files, video files (ffprobe/ffmpeg frame at ~10%), and 3D model files (STL/OBJ, isometric SVG projection via Sharp); removes stale thumbnails and updates manifest hashes.
35
- syncThumbnailCacheOnce() - Mirrors configured roots and userPath into public/thumbs and generates 99x99-max WebP thumbnails for image files, video files (ffprobe/ffmpeg frame at ~10%), and 3D model files (STL/OBJ, isometric SVG projection via Sharp); removes stale thumbnails and updates manifest hashes.
35
- startThumbnailCacheLoop() - Schedules cache sync runs 5 seconds after boot and 5 seconds after each run completes.
36
- startThumbnailCacheLoop() - Schedules cache sync runs with adaptive cadence: default 5s when idle and heavily throttled while active streams exist (`THUMBNAIL_CACHE_ACTIVE_STREAM_DELAY_MS`, default 120000 ms).
36
- stopThumbnailCacheLoop() - Stops scheduled background cache sync runs.
37
- stopThumbnailCacheLoop() - Stops scheduled background cache sync runs.
37
- getCachedThumbnailInfo(sourceFile) - Returns cached thumbnail path/content-type when present; null when not ready.
38
- getCachedThumbnailInfo(sourceFile) - Returns cached thumbnail path/content-type when present; null when not ready.
38
- getThumbPathForSourceFile(sourceFile) - Maps an absolute source file path to its mirrored cache thumbnail path.
39
- getThumbPathForSourceFile(sourceFile) - Maps an absolute source file path to its mirrored cache thumbnail path.
@@ -155,6 +156,7 @@155
- hasInitialHlsAssets(outputDir) - Returns true once master.m3u8 or at least one variant playlist/segment exists in an HLS output directory; accepts named variant filenames (stream_video.m3u8) in addition to numeric ones.
156
- hasInitialHlsAssets(outputDir) - Returns true once master.m3u8 or at least one variant playlist/segment exists in an HLS output directory; accepts named variant filenames (stream_video.m3u8) in addition to numeric ones.
156
- purgeStreamRootDir() - Best-effort recursive delete of the temporary HLS stream root; used at startup and by idle sweeps to remove orphaned stream session directories from previous runs.
157
- purgeStreamRootDir() - Best-effort recursive delete of the temporary HLS stream root; used at startup and by idle sweeps to remove orphaned stream session directories from previous runs.
157
- cleanupIdleStreamArtifacts() - Periodic idle-time cleaner that purges stream-root artifacts when there are no active sessions or leases, preventing stale segment directories from accumulating after failures.
158
- cleanupIdleStreamArtifacts() - Periodic idle-time cleaner that purges stream-root artifacts when there are no active sessions or leases, preventing stale segment directories from accumulating after failures.
159
- pruneExpiredSessions() sweep - A dedicated interval sweep (`VIDEO_STREAM_EXPIRY_SWEEP_MS`, default 60000 ms) reclaims expired leases/sessions even when no new stream requests arrive.
158
- getVideoStreamDebugSnapshot() - Returns active stream sessions with viewer counts and output-dir stats (segment counts/bytes), plus orphaned output directories left under the stream root for diagnostics.
160
- getVideoStreamDebugSnapshot() - Returns active stream sessions with viewer counts and output-dir stats (segment counts/bytes), plus orphaned output directories left under the stream root for diagnostics.
159
- getVideoStreamDebugSnapshot() - Includes segment retention and prune telemetry per session (segmentRetentionCount, slowestViewerSegmentIndex, segmentPrunedUntil, lastSegmentPrune, streamPruneDeleteBatchSize) to explain growth or cleanup stalls in segment counts.
161
- getVideoStreamDebugSnapshot() - Includes segment retention and prune telemetry per session (segmentRetentionCount, slowestViewerSegmentIndex, segmentPrunedUntil, lastSegmentPrune, streamPruneDeleteBatchSize) to explain growth or cleanup stalls in segment counts.
160
- STREAM_ROOT_DIR - Temporary root directory for active ffmpeg-backed stream sessions.
162
- STREAM_ROOT_DIR - Temporary root directory for active ffmpeg-backed stream sessions.
@@ -6,8 +6,19 @@ const path = require('path');
6
6
7
7
const SUBTITLE_DEBUG_LOG_DIR = path.join(os.tmpdir(), 'filemanager-subtitle-debug-logs');
8
8
const MAX_SUBTITLE_DEBUG_LOG_BYTES = 10 * 1024 * 1024;
9
const MAX_SUBTITLE_LOG_CONTEXT_KEYS = parseContextLimit(process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX, 512);
9
10
const subtitleLogFileByContext = new Map();
10
11
12
function parseContextLimit (value, fallback)
13
{
14
const parsed = Number.parseInt(String(value || ''), 10);
15
if (Number.isInteger(parsed) && parsed >= 1)
16
{
17
return Math.min(parsed, 4096);
18
}
19
return fallback;
20
}
21
11
22
function normaliseTimestampMs (timestampMs)
12
23
{
13
24
const parsed = Number(timestampMs);
@@ -41,13 +52,43 @@ function resolveSubtitleLogPath (videoName, timestampMs, context)
41
52
{
42
53
const key = subtitleLogContextKey(videoName, context);
43
54
const existing = subtitleLogFileByContext.get(key);
44
if (existing) return existing;
55
if (existing)
56
{
57
// LRU refresh for active contexts.
58
subtitleLogFileByContext.delete(key);
59
subtitleLogFileByContext.set(key, existing);
60
return existing;
61
}
62
63
trimSubtitleLogContextMap();
45
64
46
65
const targetPath = subtitleDebugLogFilePath(videoName, timestampMs);
47
66
subtitleLogFileByContext.set(key, targetPath);
67
trimSubtitleLogContextMap();
48
68
return targetPath;
49
69
}
50
70
71
function trimSubtitleLogContextMap ()
72
{
73
if (subtitleLogFileByContext.size <= MAX_SUBTITLE_LOG_CONTEXT_KEYS) return;
74
75
// Remove oldest references first when they no longer point at existing files.
76
for (const [key, filePath] of subtitleLogFileByContext)
77
{
78
if (subtitleLogFileByContext.size <= MAX_SUBTITLE_LOG_CONTEXT_KEYS) break;
79
if (fs.existsSync(filePath)) continue;
80
subtitleLogFileByContext.delete(key);
81
}
82
83
// Hard cap fallback: evict oldest entries (LRU order) until under limit.
84
while (subtitleLogFileByContext.size > MAX_SUBTITLE_LOG_CONTEXT_KEYS)
85
{
86
const oldestKey = subtitleLogFileByContext.keys().next().value;
87
if (!oldestKey) break;
88
subtitleLogFileByContext.delete(oldestKey);
89
}
90
}
91
51
92
function ensureSubtitleDebugLogDir ()
52
93
{
53
94
fs.mkdirSync(SUBTITLE_DEBUG_LOG_DIR, { recursive: true });
@@ -154,10 +195,16 @@ function appendSubtitleDebugEntries ({ videoName, entries, context = null })
154
195
}
155
196
}
156
197
198
function getSubtitleDebugLogContextCount ()
199
{
200
return subtitleLogFileByContext.size;
201
}
202
157
203
module.exports = {
158
204
SUBTITLE_DEBUG_LOG_DIR,
159
205
MAX_SUBTITLE_DEBUG_LOG_BYTES,
160
206
getSubtitleDebugLogSnapshot,
207
getSubtitleDebugLogContextCount,
161
208
subtitleDebugLogFilePath,
162
209
pruneSubtitleDebugLogDir,
163
210
appendSubtitleDebugEntries,
@@ -6,8 +6,19 @@ const path = require('path');6
6
7
const SUBTITLE_DEBUG_LOG_DIR = path.join(os.tmpdir(), 'filemanager-subtitle-debug-logs');7
const SUBTITLE_DEBUG_LOG_DIR = path.join(os.tmpdir(), 'filemanager-subtitle-debug-logs');8
const MAX_SUBTITLE_DEBUG_LOG_BYTES = 10 * 1024 * 1024;8
const MAX_SUBTITLE_DEBUG_LOG_BYTES = 10 * 1024 * 1024;9
const MAX_SUBTITLE_LOG_CONTEXT_KEYS = parseContextLimit(process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX, 512);9
const subtitleLogFileByContext = new Map();10
const subtitleLogFileByContext = new Map();10
11
12
function parseContextLimit (value, fallback)13
{14
const parsed = Number.parseInt(String(value || ''), 10);15
if (Number.isInteger(parsed) && parsed >= 1)16
{17
return Math.min(parsed, 4096);18
}19
return fallback;20
}21
11
function normaliseTimestampMs (timestampMs)22
function normaliseTimestampMs (timestampMs)12
{23
{13
const parsed = Number(timestampMs);24
const parsed = Number(timestampMs);@@ -41,13 +52,43 @@ function resolveSubtitleLogPath (videoName, timestampMs, context)41
{52
{42
const key = subtitleLogContextKey(videoName, context);53
const key = subtitleLogContextKey(videoName, context);43
const existing = subtitleLogFileByContext.get(key);54
const existing = subtitleLogFileByContext.get(key);44
if (existing) return existing;55
if (existing)56
{57
// LRU refresh for active contexts.58
subtitleLogFileByContext.delete(key);59
subtitleLogFileByContext.set(key, existing);60
return existing;61
}62
63
trimSubtitleLogContextMap();45
64
46
const targetPath = subtitleDebugLogFilePath(videoName, timestampMs);65
const targetPath = subtitleDebugLogFilePath(videoName, timestampMs);47
subtitleLogFileByContext.set(key, targetPath);66
subtitleLogFileByContext.set(key, targetPath);67
trimSubtitleLogContextMap();48
return targetPath;68
return targetPath;49
}69
}50
70
71
function trimSubtitleLogContextMap ()72
{73
if (subtitleLogFileByContext.size <= MAX_SUBTITLE_LOG_CONTEXT_KEYS) return;74
75
// Remove oldest references first when they no longer point at existing files.76
for (const [key, filePath] of subtitleLogFileByContext)77
{78
if (subtitleLogFileByContext.size <= MAX_SUBTITLE_LOG_CONTEXT_KEYS) break;79
if (fs.existsSync(filePath)) continue;80
subtitleLogFileByContext.delete(key);81
}82
83
// Hard cap fallback: evict oldest entries (LRU order) until under limit.84
while (subtitleLogFileByContext.size > MAX_SUBTITLE_LOG_CONTEXT_KEYS)85
{86
const oldestKey = subtitleLogFileByContext.keys().next().value;87
if (!oldestKey) break;88
subtitleLogFileByContext.delete(oldestKey);89
}90
}91
51
function ensureSubtitleDebugLogDir ()92
function ensureSubtitleDebugLogDir ()52
{93
{53
fs.mkdirSync(SUBTITLE_DEBUG_LOG_DIR, { recursive: true });94
fs.mkdirSync(SUBTITLE_DEBUG_LOG_DIR, { recursive: true });@@ -154,10 +195,16 @@ function appendSubtitleDebugEntries ({ videoName, entries, context = null })154
}195
}155
}196
}156
197
198
function getSubtitleDebugLogContextCount ()199
{200
return subtitleLogFileByContext.size;201
}202
157
module.exports = {203
module.exports = {158
SUBTITLE_DEBUG_LOG_DIR,204
SUBTITLE_DEBUG_LOG_DIR,159
MAX_SUBTITLE_DEBUG_LOG_BYTES,205
MAX_SUBTITLE_DEBUG_LOG_BYTES,160
getSubtitleDebugLogSnapshot,206
getSubtitleDebugLogSnapshot,207
getSubtitleDebugLogContextCount,161
subtitleDebugLogFilePath,208
subtitleDebugLogFilePath,162
pruneSubtitleDebugLogDir,209
pruneSubtitleDebugLogDir,163
appendSubtitleDebugEntries,210
appendSubtitleDebugEntries,@@ -13,6 +13,7 @@ const THUMBS_DIR = path.resolve(process.env.FILEMANAGER_THUMBS_DIR || path.join(
13
13
const MANIFEST_PATH = path.join(THUMBS_DIR, '.manifest.json');
14
14
const FOLDER_ICONS_PATH = path.join(THUMBS_DIR, '.folder-icons.json');
15
15
const LOOP_DELAY_MS = 5000;
16
const ACTIVE_STREAM_LOOP_DELAY_MS = toPositiveInt(process.env.THUMBNAIL_CACHE_ACTIVE_STREAM_DELAY_MS, 120000);
16
17
17
18
const IMAGE_EXTS = new Set([
18
19
'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',
@@ -128,6 +129,33 @@ function runCommand (command, args)
128
129
});
129
130
}
130
131
132
function toPositiveInt (value, fallback)
133
{
134
const parsed = Number.parseInt(String(value || ''), 10);
135
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
136
}
137
138
function activeThumbnailLoopDelayMs ()
139
{
140
try
141
{
142
const stream = require('./video-stream');
143
const count = Number(stream && typeof stream.getActiveStreamCount === 'function'
144
? stream.getActiveStreamCount()
145
: 0);
146
if (Number.isFinite(count) && count > 0)
147
{
148
return Math.max(LOOP_DELAY_MS, ACTIVE_STREAM_LOOP_DELAY_MS);
149
}
150
}
151
catch
152
{
153
// Fallback to normal cadence if stream state is unavailable.
154
}
155
156
return LOOP_DELAY_MS;
157
}
158
131
159
async function probeDurationSeconds (sourceFile)
132
160
{
133
161
const { stdout } = await runCommand(ffprobePath(), [
@@ -773,7 +801,7 @@ function scheduleNextRun (delayMs)
773
801
}
774
802
finally
775
803
{
776
if (loopStarted) scheduleNextRun(LOOP_DELAY_MS);
804
if (loopStarted) scheduleNextRun(activeThumbnailLoopDelayMs());
777
805
}
778
806
}, delayMs);
779
807
@@ -784,7 +812,7 @@ function startThumbnailCacheLoop ()
784
812
{
785
813
if (loopStarted) return;
786
814
loopStarted = true;
787
scheduleNextRun(LOOP_DELAY_MS);
815
scheduleNextRun(activeThumbnailLoopDelayMs());
788
816
}
789
817
790
818
function stopThumbnailCacheLoop ()
@@ -13,6 +13,7 @@ const THUMBS_DIR = path.resolve(process.env.FILEMANAGER_THUMBS_DIR || path.join(13
const MANIFEST_PATH = path.join(THUMBS_DIR, '.manifest.json');13
const MANIFEST_PATH = path.join(THUMBS_DIR, '.manifest.json');14
const FOLDER_ICONS_PATH = path.join(THUMBS_DIR, '.folder-icons.json');14
const FOLDER_ICONS_PATH = path.join(THUMBS_DIR, '.folder-icons.json');15
const LOOP_DELAY_MS = 5000;15
const LOOP_DELAY_MS = 5000;16
const ACTIVE_STREAM_LOOP_DELAY_MS = toPositiveInt(process.env.THUMBNAIL_CACHE_ACTIVE_STREAM_DELAY_MS, 120000);16
17
17
const IMAGE_EXTS = new Set([18
const IMAGE_EXTS = new Set([18
'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',19
'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'svg', 'ico',@@ -128,6 +129,33 @@ function runCommand (command, args)128
});129
});129
}130
}130
131
132
function toPositiveInt (value, fallback)133
{134
const parsed = Number.parseInt(String(value || ''), 10);135
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;136
}137
138
function activeThumbnailLoopDelayMs ()139
{140
try141
{142
const stream = require('./video-stream');143
const count = Number(stream && typeof stream.getActiveStreamCount === 'function'144
? stream.getActiveStreamCount()145
: 0);146
if (Number.isFinite(count) && count > 0)147
{148
return Math.max(LOOP_DELAY_MS, ACTIVE_STREAM_LOOP_DELAY_MS);149
}150
}151
catch152
{153
// Fallback to normal cadence if stream state is unavailable.154
}155
156
return LOOP_DELAY_MS;157
}158
131
async function probeDurationSeconds (sourceFile)159
async function probeDurationSeconds (sourceFile)132
{160
{133
const { stdout } = await runCommand(ffprobePath(), [161
const { stdout } = await runCommand(ffprobePath(), [@@ -773,7 +801,7 @@ function scheduleNextRun (delayMs)773
}801
}774
finally802
finally775
{803
{776
if (loopStarted) scheduleNextRun(LOOP_DELAY_MS);804
if (loopStarted) scheduleNextRun(activeThumbnailLoopDelayMs());777
}805
}778
}, delayMs);806
}, delayMs);779
807
@@ -784,7 +812,7 @@ function startThumbnailCacheLoop ()784
{812
{785
if (loopStarted) return;813
if (loopStarted) return;786
loopStarted = true;814
loopStarted = true;787
scheduleNextRun(LOOP_DELAY_MS);815
scheduleNextRun(activeThumbnailLoopDelayMs());788
}816
}789
817
790
function stopThumbnailCacheLoop ()818
function stopThumbnailCacheLoop ()@@ -11,6 +11,7 @@ const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');
11
11
const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;
12
12
const STREAM_READY_TIMEOUT_MS = 20 * 1000;
13
13
const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;
14
const STREAM_EXPIRY_SWEEP_MS = parseSweepInterval(process.env.VIDEO_STREAM_EXPIRY_SWEEP_MS, 60 * 1000);
14
15
const ESTIMATED_SEGMENT_BYTES = 1024 * 1024;
15
16
const ESTIMATED_SEGMENT_DURATION_SECONDS = 6;
16
17
const STREAM_PRUNE_DELETE_BATCH_SIZE = parsePruneDeleteBatchSize(process.env.VIDEO_STREAM_PRUNE_DELETE_BATCH_SIZE);
@@ -59,6 +60,23 @@ if (streamRootCleanupTimer && typeof streamRootCleanupTimer.unref === 'function'
59
60
streamRootCleanupTimer.unref();
60
61
}
61
62
63
// Ensure expired leases/sessions are reclaimed even without further stream requests.
64
const streamExpirySweepTimer = setInterval(pruneExpiredSessions, STREAM_EXPIRY_SWEEP_MS);
65
if (streamExpirySweepTimer && typeof streamExpirySweepTimer.unref === 'function')
66
{
67
streamExpirySweepTimer.unref();
68
}
69
70
function parseSweepInterval (value, fallbackMs)
71
{
72
const parsed = Number.parseInt(String(value || ''), 10);
73
if (Number.isInteger(parsed) && parsed >= 1000)
74
{
75
return clampNumber(parsed, 1000, 10 * 60 * 1000);
76
}
77
return fallbackMs;
78
}
79
62
80
function parseSegmentRetentionCount (value)
63
81
{
64
82
const parsed = Number.parseInt(String(value || ''), 10);
@@ -1290,6 +1308,11 @@ function getVideoStreamDebugSnapshot ()
1290
1308
};
1291
1309
}
1292
1310
1311
function getActiveStreamCount ()
1312
{
1313
return sessions.size;
1314
}
1315
1293
1316
module.exports = {
1294
1317
STREAM_ROOT_DIR,
1295
1318
STREAM_SESSION_TTL_MS,
@@ -1298,6 +1321,7 @@ module.exports = {
1298
1321
createVideoStreamSession,
1299
1322
sendVideoStreamAsset,
1300
1323
releaseVideoStreamSession,
1324
getActiveStreamCount,
1301
1325
getVideoStreamDebugSnapshot,
1302
1326
cleanupIdleStreamArtifacts,
1303
1327
};
@@ -11,6 +11,7 @@ const STREAM_ROOT_DIR = path.join(os.tmpdir(), 'filemanager-video-streams');11
const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;11
const STREAM_SESSION_TTL_MS = 30 * 60 * 1000;12
const STREAM_READY_TIMEOUT_MS = 20 * 1000;12
const STREAM_READY_TIMEOUT_MS = 20 * 1000;13
const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;13
const STREAM_IDLE_STOP_MS = 5 * 60 * 1000;14
const STREAM_EXPIRY_SWEEP_MS = parseSweepInterval(process.env.VIDEO_STREAM_EXPIRY_SWEEP_MS, 60 * 1000);14
const ESTIMATED_SEGMENT_BYTES = 1024 * 1024;15
const ESTIMATED_SEGMENT_BYTES = 1024 * 1024;15
const ESTIMATED_SEGMENT_DURATION_SECONDS = 6;16
const ESTIMATED_SEGMENT_DURATION_SECONDS = 6;16
const STREAM_PRUNE_DELETE_BATCH_SIZE = parsePruneDeleteBatchSize(process.env.VIDEO_STREAM_PRUNE_DELETE_BATCH_SIZE);17
const STREAM_PRUNE_DELETE_BATCH_SIZE = parsePruneDeleteBatchSize(process.env.VIDEO_STREAM_PRUNE_DELETE_BATCH_SIZE);@@ -59,6 +60,23 @@ if (streamRootCleanupTimer && typeof streamRootCleanupTimer.unref === 'function'59
streamRootCleanupTimer.unref();60
streamRootCleanupTimer.unref();60
}61
}61
62
63
// Ensure expired leases/sessions are reclaimed even without further stream requests.64
const streamExpirySweepTimer = setInterval(pruneExpiredSessions, STREAM_EXPIRY_SWEEP_MS);65
if (streamExpirySweepTimer && typeof streamExpirySweepTimer.unref === 'function')66
{67
streamExpirySweepTimer.unref();68
}69
70
function parseSweepInterval (value, fallbackMs)71
{72
const parsed = Number.parseInt(String(value || ''), 10);73
if (Number.isInteger(parsed) && parsed >= 1000)74
{75
return clampNumber(parsed, 1000, 10 * 60 * 1000);76
}77
return fallbackMs;78
}79
62
function parseSegmentRetentionCount (value)80
function parseSegmentRetentionCount (value)63
{81
{64
const parsed = Number.parseInt(String(value || ''), 10);82
const parsed = Number.parseInt(String(value || ''), 10);@@ -1290,6 +1308,11 @@ function getVideoStreamDebugSnapshot ()1290
};1308
};1291
}1309
}1292
1310
1311
function getActiveStreamCount ()1312
{1313
return sessions.size;1314
}1315
1293
module.exports = {1316
module.exports = {1294
STREAM_ROOT_DIR,1317
STREAM_ROOT_DIR,1295
STREAM_SESSION_TTL_MS,1318
STREAM_SESSION_TTL_MS,@@ -1298,6 +1321,7 @@ module.exports = {1298
createVideoStreamSession,1321
createVideoStreamSession,1299
sendVideoStreamAsset,1322
sendVideoStreamAsset,1300
releaseVideoStreamSession,1323
releaseVideoStreamSession,1324
getActiveStreamCount,1301
getVideoStreamDebugSnapshot,1325
getVideoStreamDebugSnapshot,1302
cleanupIdleStreamArtifacts,1326
cleanupIdleStreamArtifacts,1303
};1327
};@@ -8,6 +8,7 @@ describe('subtitle debug log writer', () =>
8
8
{
9
9
const modulePath = require.resolve('../server/subtitle-debug-log');
10
10
const logDir = path.join(os.tmpdir(), 'filemanager-subtitle-debug-logs');
11
const originalContextLimit = process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX;
11
12
12
13
beforeEach(() =>
13
14
{
@@ -15,6 +16,12 @@ describe('subtitle debug log writer', () =>
15
16
fs.rmSync(logDir, { recursive: true, force: true });
16
17
});
17
18
19
afterEach(() =>
20
{
21
if (originalContextLimit === undefined) delete process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX;
22
else process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX = originalContextLimit;
23
});
24
18
25
test('writes timestamped per-video subtitle log files', () =>
19
26
{
20
27
const { appendSubtitleDebugEntries } = require('../server/subtitle-debug-log');
@@ -85,4 +92,36 @@ describe('subtitle debug log writer', () =>
85
92
expect(entries).toHaveLength(1);
86
93
expect(entries[0]).toContain('new-video.mkv-subtitlelog.txt');
87
94
});
95
96
test('evicts old context keys when subtitle log context map reaches cap', () =>
97
{
98
process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX = '2';
99
jest.resetModules();
100
const logModule = require('../server/subtitle-debug-log');
101
102
const firstPath = logModule.appendSubtitleDebugEntries({
103
videoName: 'ctx-video.mkv',
104
context: { source: 'app', sessionId: 'sess-1' },
105
entries: [{ loggedAtMs: 1000, eventType: 'first' }],
106
});
107
108
for (let i = 0; i < 6; i++)
109
{
110
logModule.appendSubtitleDebugEntries({
111
videoName: `ctx-video-${i}.mkv`,
112
context: { source: 'app', sessionId: `sess-${i + 2}` },
113
entries: [{ loggedAtMs: 2000 + i, eventType: 'filler' }],
114
});
115
}
116
117
const secondPath = logModule.appendSubtitleDebugEntries({
118
videoName: 'ctx-video.mkv',
119
context: { source: 'app', sessionId: 'sess-1' },
120
entries: [{ loggedAtMs: 999999, eventType: 'second' }],
121
});
122
123
expect(typeof firstPath).toBe('string');
124
expect(typeof secondPath).toBe('string');
125
expect(logModule.getSubtitleDebugLogContextCount()).toBeLessThanOrEqual(2);
126
});
88
127
});
@@ -8,6 +8,7 @@ describe('subtitle debug log writer', () =>8
{8
{9
const modulePath = require.resolve('../server/subtitle-debug-log');9
const modulePath = require.resolve('../server/subtitle-debug-log');10
const logDir = path.join(os.tmpdir(), 'filemanager-subtitle-debug-logs');10
const logDir = path.join(os.tmpdir(), 'filemanager-subtitle-debug-logs');11
const originalContextLimit = process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX;11
12
12
beforeEach(() =>13
beforeEach(() =>13
{14
{@@ -15,6 +16,12 @@ describe('subtitle debug log writer', () =>15
fs.rmSync(logDir, { recursive: true, force: true });16
fs.rmSync(logDir, { recursive: true, force: true });16
});17
});17
18
19
afterEach(() =>20
{21
if (originalContextLimit === undefined) delete process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX;22
else process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX = originalContextLimit;23
});24
18
test('writes timestamped per-video subtitle log files', () =>25
test('writes timestamped per-video subtitle log files', () =>19
{26
{20
const { appendSubtitleDebugEntries } = require('../server/subtitle-debug-log');27
const { appendSubtitleDebugEntries } = require('../server/subtitle-debug-log');@@ -85,4 +92,36 @@ describe('subtitle debug log writer', () =>85
expect(entries).toHaveLength(1);92
expect(entries).toHaveLength(1);86
expect(entries[0]).toContain('new-video.mkv-subtitlelog.txt');93
expect(entries[0]).toContain('new-video.mkv-subtitlelog.txt');87
});94
});95
96
test('evicts old context keys when subtitle log context map reaches cap', () =>97
{98
process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX = '2';99
jest.resetModules();100
const logModule = require('../server/subtitle-debug-log');101
102
const firstPath = logModule.appendSubtitleDebugEntries({103
videoName: 'ctx-video.mkv',104
context: { source: 'app', sessionId: 'sess-1' },105
entries: [{ loggedAtMs: 1000, eventType: 'first' }],106
});107
108
for (let i = 0; i < 6; i++)109
{110
logModule.appendSubtitleDebugEntries({111
videoName: `ctx-video-${i}.mkv`,112
context: { source: 'app', sessionId: `sess-${i + 2}` },113
entries: [{ loggedAtMs: 2000 + i, eventType: 'filler' }],114
});115
}116
117
const secondPath = logModule.appendSubtitleDebugEntries({118
videoName: 'ctx-video.mkv',119
context: { source: 'app', sessionId: 'sess-1' },120
entries: [{ loggedAtMs: 999999, eventType: 'second' }],121
});122
123
expect(typeof firstPath).toBe('string');124
expect(typeof secondPath).toBe('string');125
expect(logModule.getSubtitleDebugLogContextCount()).toBeLessThanOrEqual(2);126
});88
});127
});