Use find -printf instead of per-file stat/basename in transcode_cleanup.sh

Same fix as the arr cleanup scripts today: find -printf gets file size
directly from find's own stat() during the walk (mtime already handled
by -mmin), and the dry-run log line's basename call is replaced with
parameter expansion. Header notes this can matter under real load --
"thousands of HLS segment files" per the existing lsof design principle
this mirrors, even though the ramdisk is empty right now (no active
transcode session to benchmark against directly).
This commit is contained in:
Gmer4Lfe
2026-07-17 02:13:07 -04:00
parent 587fae1539
commit 6dc5c91f60
+15 -7
View File
@@ -24,6 +24,14 @@
# lsof is called once per location to build a complete open-file map. All subsequent
# checks are O(1) lookups against that map — thousands of files, one lsof call.
#
# find -printf Instead of Per-File stat/basename (2026-07-17)
# Same fork-elimination principle as above, applied to the file-processing loop. find
# already has to stat() every entry to know it's -type f, so -printf '%s %p' gets the
# size for free during the walk instead of a separate stat fork per file. basename was
# also forking per file in the dry-run log line -- replaced with parameter expansion
# (${file##*/}). Same class of bug found and measured (~28-185x per call) in the arr
# cleanup scripts' classification loops the same day.
#
# No Session-Aware Cleanup
# ffmpeg generates folder names independently of the media server API session IDs.
# There is no reliable correlation between API session IDs and transcoding-temp
@@ -200,8 +208,11 @@ cleanup_location() {
log "$files_streaming files currently open in $label"
fi
# Process aged files
while IFS= read -r file; do
# Process aged files. -printf gets size directly from find's own stat() during the walk
# instead of a separate stat fork per file (2026-07-17, same fix as the arr cleanup
# scripts) — mtime isn't needed here since -mmin above already did the age filtering.
local file_size file
while read -r file_size file; do
[[ -z "$file" ]] && continue
# O(1) open file check — in-memory map
@@ -211,11 +222,8 @@ cleanup_location() {
continue
fi
local file_size
file_size=$(stat -c%s "$file" 2>/dev/null || echo 0)
if [[ "$DRY_RUN" == true ]]; then
log "DRY RUN — would delete: $(basename "$file")"
log "DRY RUN — would delete: ${file##*/}"
(( files_skipped++ ))
else
if rm -f "$file" 2>/dev/null; then
@@ -228,7 +236,7 @@ cleanup_location() {
fi
fi
done < <(find "$location" -type f -mmin +"$max_age" 2>/dev/null)
done < <(find "$location" -type f -mmin +"$max_age" -printf '%s %p\n' 2>/dev/null)
# Remove empty directories older than TRANSCODE_ORPHAN_AGE — but NEVER remove
# transcoding-temp. Age gate avoids deleting a session folder ffmpeg just