* perf(images): share and warm one listing per output root (#348) The Images page, its sidebar counts, the subfolder list and the legacy gallery each rebuilt the output listing when the 30 s cache expired, all at once, and the subfolder list computed relative paths on the event loop. Thumbnail writes also threw the listing away. Keep one RootListing per output root (py/api/gallery_listing.py): - one walk per root at a time; concurrent readers wait for it (shielded) - a listing older than 30 s is served at once and refreshed in the background - the walk, the comparison with the previous listing and both folder sets run in the executor; an unchanged walk keeps the same list object - os.scandir probe: a missing root fails the walk and keeps the previous listing (one warning per failure streak) - Rescan waits for a walk started after the click and is a 500 when it fails, even with a previous listing - deletes keep the blocking invalidation; a generation counter that survives drops discards walks that raced it - thumbnail jobs, conversion and the legacy generate/clear routes no longer drop the listing - warm-up on the host's on_startup, cancelled on on_cleanup Adds tools/vendor/perf/images_load_probe.mjs (maintainer tool, not CI) and the TESTING.md section "Images listing cache (#348)". * fix(images): drop listings on the loop when invalidated from a worker thread (#348) A settings save (gallery roots change) and the browser-test seeds call invalidate_gallery_cache() from the executor. Listing records are only touched on the event loop, so an off-loop call now hands the drop to the loop and waits for it, keeping the call synchronous for its callers. The shared-Rescan test waits for the later Rescans to take their walk number instead of sleeping. * test(images): bound the shared-listing test waits (#348) A macOS py3.12 run stopped inside the shared-listing tests and sat until the 6 h job limit with no traceback. It does not reproduce locally (about 300 runs on 3.12 and 3.14, pinned to one core). Requests and background settles in TestSharedListing now give up after 30 s, so a recurrence fails with the stuck await instead of hanging the run. * test(images): let the settle loops yield so a finished walk leaves the set (#348) On CI (ubuntu and macOS py3.12) the runner's watchdog dumped test_a_root_that_recovers_warns_again_on_its_next_failure spinning in _settle: asyncio.gather over tasks that are already done returns without yielding, so the set's discard callback, queued when the walk finished, never ran and the loop never ended (wait_for could not fire either). Delaying discard by one loop turn reproduces it locally; both settle loops now yield one turn after each gather. * test(browser): poll the import review's rows instead of reading them once gridOf reads the dialog's rows once; right after the click the review may not have rendered them yet (firefox CI: received undefined). * perf(images): walk the output with scandir entries and build folders from strings (#348) The live test showed a read during a background refresh taking 2.2 s instead of 0.07 s: the walk held the GIL for about 2 s in the server. py-spy split that walk into two halves: Path.relative_to per file in folder_sets, and Path.stat + Path.is_symlink + Path joins per file in the scan. - _walk_media visits folders in os.walk's order with os.scandir entries: the file type comes from the listing (no lstat per file) and the mtime from entry.stat (one call, none on Windows). _iter_media_files is now a thin wrapper, so its other caller is unchanged. - folder_sets slices the path strings (in the path flavour's separator) instead of calling relative_to, falling back to it for a path that does not start with the root string. On the maintainer's 19,935-file output folder: scan 0.386 s -> 0.112 s, folder sets 0.452 s -> 0.007 s, both results identical to the previous code. A reference copy of the os.walk version checks order, filters, the depth cap, symlinks and the file cap. * perf(images): build the workspace caches behind the warm-up and changed walks (#348) The live test after the faster walk still took 2.9-5.2 s for the first Images open after a restart. py-spy showed the walk was now small; the time went to the workspace's own caches, cold after a restart and built by three parallel first requests at once: the favorites listing (Path.relative_to per file), the per-file values and newest order, and the prompt-link read. - Once the host's startup warm-up has run, a stored walk that changed a listing schedules one executor job that builds what a first page reads, through the request path itself (_select_workspace_page with the default filters, then _workspace_favorite_paths). A walk stored during a build asks for one more build; an unchanged walk (same list kept) builds nothing; a failed build only warns; on_cleanup stops it. Servers that never ran the warm-up (the test servers) start no build. - _favorite_listing slices the path strings as _listed does, falling back to relative_to for a path outside the root string. * perf(images): start a stale listing's refresh walk a second after the read (#348) After the faster walk, a read that found the listing stale still took 0.5-1.0 s against 0.07 s: it was served from the old listing but ran beside the refresh walk it had just started, sharing the GIL. The refresh now starts REFRESH_DELAY (1 s) after that read through a per-root rewalk_handle, so the page's own requests finish first. One pending refresh per root and none while a walk runs; any walk start (a cold read, Rescan, the warm-up) cancels a pending one; on_cleanup cancels them before its first await. * test(browser): accept KB or MB for the picked safety backup's size The restore preview showed '2.5 MB' for the test server's database copy in a CI coverage shard: its size depends on what earlier tests on that server wrote, and the formatter switches to MB at 1 MiB. * fix(images): keep the 3.10-3.13 suffix rule in the scandir walk and harden the warm-up (#348) Review of the walk speedup: - os.path.splitext drops names like ..png that Path.suffix listed on Python 3.10-3.13 (3.14 changed Path.suffix, which hid it from the parity test on a 3.14 venv). _name_suffix spells out the 3.10-3.13 rule; the test now asserts ..png and ...webp are listed and g. is not. - _warm_workspace reads the roots inside its try, so a failing _get_all_output_dirs warns instead of leaving an unretrieved task error. - The folder_sets fallback test now takes the fallback (a Windows case mismatch); the coalescing test pins the walk count.
405 lines
17 KiB
Python
405 lines
17 KiB
Python
"""Convert outdated thumbnails (#317): an explicit, cancellable Activity job.
|
|
|
|
``POST /prompt_manager/jobs/thumbnails {mode: "convert"}`` (and
|
|
``GET /prompt_manager/images/thumbnails/plan?mode=convert`` for its count)
|
|
starts "Convert outdated thumbnails": the thumbnail job's kind, ``guarded``
|
|
admission (never beside a record-changing job such as a scan, a restore or
|
|
a duplicate removal; one thumbnail job at a time), its worker threads and
|
|
its outcome groups. Nothing converts on its own: changing the format in
|
|
Appearance only makes previews "outdated".
|
|
|
|
A preview is outdated when the file owns one in another format than the one
|
|
written now (``writer_settings``: a legacy ``_thumb.png``, or a format
|
|
switched away from). For each such file, at a file boundary (Cancel keeps
|
|
what was converted):
|
|
|
|
1. the new preview is written from the SOURCE media in the configured
|
|
format (``_generate_one``: atomic, verified to decode; a video whose frame
|
|
cannot be decoded fails rather than getting the placeholder), unless a
|
|
current preview at least as new as the source is already there and
|
|
decodes;
|
|
2. only then are that file's old-format previews removed: regular files (no
|
|
links) in its ``thumbnails/`` folder, owned by that source
|
|
(``thumbnail_settings.owns``), with the source still present. One already
|
|
gone counts as removed.
|
|
|
|
Files sharing a stem in one folder (``x.png`` and ``x.mp4``) share one
|
|
preview (ruling R2), so they are converted once, as a group: the preview is
|
|
made from the first source in ``thumbnail_settings.source_rank`` order
|
|
(``.png``, ``.jpg``, ``.jpeg``, ``.webp``, ``.gif``, then videos), the old
|
|
previews of every source in the group are removed, and each removed or
|
|
written file's bytes count once.
|
|
|
|
A failure keeps the old preview. Previews whose source is gone (orphans) are
|
|
never planned, so never touched. The format the job converts to is fixed
|
|
when it starts; Appearance refuses a thumbnail settings change while it runs
|
|
(409), and should the format still change underneath, the job stops at the
|
|
next file without removing anything in another format. The final status line
|
|
reads "N converted · F failed · <bytes before> → <bytes after>".
|
|
"""
|
|
|
|
import asyncio
|
|
import functools
|
|
import os
|
|
import time
|
|
from pathlib import Path
|
|
|
|
from . import thumbnail_settings
|
|
from .thumbnail_encode import ThumbnailVerifyError, verify_thumbnail, writer_settings
|
|
from .admin import _plural, _scan_location, _scan_roots, _scan_status_line
|
|
from .images import _thumbnail_is_fresh, _worker_threads, _thumbnail_batch_size
|
|
from .prompts import safe_error_message
|
|
|
|
try:
|
|
from ...utils.parallel import map_parallel
|
|
except ImportError: # tests import the package top-level
|
|
from utils.parallel import map_parallel
|
|
|
|
CONVERT_JOB_TITLE = "Convert outdated thumbnails"
|
|
NOTHING_OUTDATED = "No outdated previews to convert"
|
|
CANCELLED_BEFORE_CONVERTING = "Cancelled before any preview was converted"
|
|
SOURCE_GONE = "The file is gone; its old preview was kept"
|
|
OLD_NOT_REMOVED = "Converted; the old preview could not be removed"
|
|
FORMAT_CHANGED = (
|
|
"Stopped: the thumbnail format changed while converting · "
|
|
"{converted} converted. Run Convert outdated again."
|
|
)
|
|
_UNITS = ("B", "KB", "MB", "GB", "TB")
|
|
|
|
|
|
def readable_bytes(count):
|
|
"""``1.5 KB``-style size (whole bytes below 1 KB)."""
|
|
size = float(count)
|
|
for unit in _UNITS:
|
|
if size < 1024 or unit == _UNITS[-1]:
|
|
return f"{int(size)} B" if unit == "B" else f"{size:.1f} {unit}"
|
|
size /= 1024
|
|
return f"{size:.1f} {_UNITS[-1]}" # pragma: no cover - loop always returns
|
|
|
|
|
|
def _old_previews(src, dst, settings):
|
|
"""``[(path, size)]``: *src*'s previews beside *dst* in another format."""
|
|
stem = dst.name[: -len(dst.suffix)] if dst.suffix else dst.name
|
|
old = []
|
|
for path in thumbnail_settings.owned_in_folder(
|
|
dst.parent, stem, src.suffix, settings.extension
|
|
):
|
|
if path.suffix == settings.extension:
|
|
continue
|
|
try:
|
|
old.append((path, path.stat().st_size))
|
|
except OSError:
|
|
continue
|
|
return old
|
|
|
|
|
|
def _group_targets(targets):
|
|
"""``[[(src, dst, is_video)]]``: targets sharing a preview name (one
|
|
folder, one stem) in one group, its first source first (R2)."""
|
|
groups = {}
|
|
for target in targets:
|
|
dst = Path(target[1])
|
|
key = os.path.normcase(str(dst.with_suffix("")))
|
|
groups.setdefault(key, []).append(target)
|
|
ordered = []
|
|
for group in groups.values():
|
|
group.sort(
|
|
key=lambda t: (thumbnail_settings.source_rank(Path(t[0]).suffix), str(t[0]))
|
|
)
|
|
ordered.append(group)
|
|
return ordered
|
|
|
|
|
|
def _same_file(a, b):
|
|
"""Whether *a* and *b* name one file (re-review N1: on a case-insensitive
|
|
disk ``x_thumb.JPG`` is the ``x_thumb.jpg`` just written). Without inode
|
|
numbers (st_ino 0 on some SMB/FAT mounts) the resolved, normcased paths
|
|
are compared."""
|
|
try:
|
|
first, second = os.stat(a), os.stat(b)
|
|
except OSError:
|
|
return False
|
|
if first.st_ino and second.st_ino:
|
|
return (first.st_dev, first.st_ino) == (second.st_dev, second.st_ino)
|
|
return os.path.normcase(os.path.realpath(a)) == os.path.normcase(
|
|
os.path.realpath(b)
|
|
)
|
|
|
|
|
|
def _format_changed(settings):
|
|
"""Whether previews are written in another format than *settings* now."""
|
|
return writer_settings().extension != settings.extension
|
|
|
|
|
|
def _finished_line(totals, remaining):
|
|
line = (
|
|
f"{totals['converted']:,} converted · {totals['failed']:,} failed · "
|
|
f"{readable_bytes(totals['before'])} → {readable_bytes(totals['after'])}"
|
|
)
|
|
if remaining:
|
|
line += f" · {remaining:,} more on the next run"
|
|
return line
|
|
|
|
|
|
class ThumbnailConvertMixin:
|
|
"""The Convert outdated job (started through ``_start_thumbnail_job``)."""
|
|
|
|
async def _run_convert_job(self, job, options):
|
|
try:
|
|
state = await self._convert_job_body(job, options)
|
|
except Exception:
|
|
self.logger.exception("Thumbnail conversion failed")
|
|
job.update(status_line="Thumbnail conversion stopped on an internal error.")
|
|
job.log_line("Conversion failed · internal error")
|
|
state = "failed"
|
|
job.finish(state)
|
|
|
|
async def _convert_job_body(self, job, options):
|
|
output_dirs = await self._run_in_executor(self._get_all_output_dirs)
|
|
if not output_dirs:
|
|
message = "ComfyUI output directory not found"
|
|
job.update(status_line=message)
|
|
job.log_line(f"Conversion failed · {message}")
|
|
return "failed"
|
|
folders = _plural(len(output_dirs), "output folder")
|
|
job.scope["output_folders"] = len(output_dirs)
|
|
job.update(status_line=f"Looking for outdated previews in {folders}")
|
|
job.log_line(
|
|
f"Conversion started · {folders} · to {options.settings.describe()}"
|
|
)
|
|
plan = await self._plan_conversion(job, options, output_dirs)
|
|
if job.cancelled:
|
|
return self._end_conversion(job, 0, CANCELLED_BEFORE_CONVERTING)
|
|
if not plan["targets"]:
|
|
job.update(progress=100, status_line=NOTHING_OUTDATED)
|
|
job.log_line(f"Conversion finished · {NOTHING_OUTDATED.lower()}")
|
|
return "finished"
|
|
totals, left = await self._convert_in_batches(job, options, plan, output_dirs)
|
|
if totals["stopped"]:
|
|
job.update(status_line=FORMAT_CHANGED.format(converted=totals["converted"]))
|
|
job.log_line(
|
|
"Conversion stopped · the thumbnail format changed · "
|
|
f"{_plural(totals['converted'], 'preview')} converted"
|
|
)
|
|
return "cancelled"
|
|
if job.cancelled:
|
|
return self._end_conversion(
|
|
job,
|
|
totals["converted"],
|
|
f"Cancelled · {_plural(totals['converted'], 'preview')} converted",
|
|
)
|
|
line = _finished_line(totals, left)
|
|
job.update(progress=100, status_line=line)
|
|
job.log_line(f"Conversion finished · {line}")
|
|
self.logger.info(f"Thumbnail conversion: {line}")
|
|
return "finished"
|
|
|
|
@staticmethod
|
|
def _end_conversion(job, converted, status_line):
|
|
job.update(status_line=status_line)
|
|
job.log_line(
|
|
f"Conversion cancelled · {_plural(converted, 'preview')} converted"
|
|
)
|
|
return "cancelled"
|
|
|
|
async def _plan_conversion(self, job, options, output_dirs, exclude=None):
|
|
"""One batch's plan (at most MAX_THUMBNAIL_FILES targets), off-loop."""
|
|
return await self._run_in_executor(
|
|
self._plan_gallery_thumbnails,
|
|
output_dirs,
|
|
"convert",
|
|
stop=lambda: job.cancelled,
|
|
settings=options.settings,
|
|
exclude=exclude,
|
|
)
|
|
|
|
async def _convert_in_batches(self, job, options, plan, output_dirs):
|
|
"""Convert ``plan``, then plan again after each batch until nothing
|
|
is left, the job is cancelled or stopped, or a batch converted
|
|
nothing (files that fail are never planned again). Returns
|
|
``(totals across batches, files left)``."""
|
|
totals = {"converted": 0, "failed": 0, "before": 0, "after": 0}
|
|
totals["stopped"] = False
|
|
run = {
|
|
"index": 0,
|
|
"batches": 0,
|
|
"total": len(plan["targets"]) + plan["remaining"],
|
|
}
|
|
run["start"] = time.monotonic()
|
|
job.update(
|
|
total=run["total"], status_line=_scan_status_line(0, run["total"], 0, 0)
|
|
)
|
|
attempted = set()
|
|
while True:
|
|
before = totals["converted"]
|
|
await self._convert_targets(
|
|
job, options, plan["targets"], output_dirs, totals, run
|
|
)
|
|
attempted.update(src for src, _dst, _video in plan["targets"])
|
|
stop = job.cancelled or totals["stopped"]
|
|
if stop or not plan["remaining"] or totals["converted"] == before:
|
|
return totals, plan["remaining"]
|
|
plan = await self._plan_conversion(
|
|
job, options, output_dirs, exclude=attempted
|
|
)
|
|
if job.cancelled:
|
|
return totals, 0
|
|
run["total"] = run["index"] + len(plan["targets"]) + plan["remaining"]
|
|
job.update(total=run["total"])
|
|
if not plan["targets"]:
|
|
return totals, plan["remaining"]
|
|
|
|
async def _convert_targets(self, job, options, targets, output_dirs, totals, run):
|
|
"""Convert one batch's *targets* in worker batches of preview groups,
|
|
recording each file against the job's overall ``run`` counters."""
|
|
roots = _scan_roots(output_dirs)
|
|
groups = _group_targets(targets)
|
|
batch_size = _thumbnail_batch_size(_worker_threads())
|
|
for batch_start in range(0, len(groups), batch_size):
|
|
if job.cancelled or totals["stopped"]:
|
|
break
|
|
batch = groups[batch_start : batch_start + batch_size]
|
|
results = await self._run_in_executor(
|
|
self._convert_batch, batch, options.settings, lambda: job.cancelled
|
|
)
|
|
for group, group_results in zip(batch, results):
|
|
for (src, _dst, _video), result in zip(group, group_results):
|
|
if result["action"] == "stopped":
|
|
totals["stopped"] = True
|
|
if result["action"] in ("cancelled", "stopped"):
|
|
continue
|
|
run["index"] += 1
|
|
index, total = run["index"], max(run["total"], run["index"])
|
|
self._record_conversion(job, src, result, roots, totals)
|
|
job.update(processed=index, progress=index * 100 // total)
|
|
run["batches"] += 1
|
|
job.update(
|
|
status_line=_scan_status_line(
|
|
run["index"],
|
|
run["total"],
|
|
run["batches"],
|
|
time.monotonic() - run["start"],
|
|
)
|
|
)
|
|
await asyncio.sleep(0) # let other requests run between batches
|
|
|
|
@staticmethod
|
|
def _record_conversion(job, src, result, roots, totals):
|
|
path, label, index = _scan_location(src, roots)
|
|
where = f"{label}/{path}" if label else path
|
|
row = {"path": path, "root": label, "root_index": index}
|
|
if result["action"] == "converted":
|
|
totals["converted"] += 1
|
|
totals["before"] += result["before"]
|
|
totals["after"] += result["after"]
|
|
detail = result["detail"]
|
|
job.record("generated", {**row, "detail": detail}, f"+ {where} · {detail}")
|
|
return
|
|
totals["failed"] += 1
|
|
detail = result.get("error") or "failed"
|
|
job.record("errors", {**row, "detail": detail}, f"! {where} · {detail}")
|
|
|
|
def _convert_batch(self, groups, settings, stop):
|
|
"""Convert preview ``groups`` on the configured worker threads
|
|
(blocking); one result list per group."""
|
|
convert = functools.partial(self._convert_group, settings=settings, stop=stop)
|
|
return map_parallel(convert, groups, _worker_threads())
|
|
|
|
def _convert_group(self, group, settings, stop):
|
|
"""Convert the preview a group of same-stem files shares, once.
|
|
|
|
The first source writes it; the others are recorded with the same
|
|
outcome and no bytes of their own."""
|
|
src, dst, is_video = group[0]
|
|
siblings = [Path(target[0]) for target in group[1:]]
|
|
result = self._convert_one(
|
|
src, dst, is_video, settings, stop=stop, siblings=siblings
|
|
)
|
|
if result["action"] != "converted":
|
|
return [result] * len(group)
|
|
shared = {**result, "before": 0, "after": 0}
|
|
shared["detail"] = f"{result['detail']} (shares {Path(src).name}'s preview)"
|
|
return [result] + [shared] * len(siblings)
|
|
|
|
def _convert_one(self, src, dst, is_video, settings, stop=None, siblings=()):
|
|
"""Convert one file's preview (blocking; never raises).
|
|
|
|
``converted`` with the bytes removed and written, ``error`` (the old
|
|
preview kept), ``cancelled`` (a file boundary) or ``stopped`` (the
|
|
format changed underneath, review I4). *siblings* share the preview
|
|
(R2): their old previews go too.
|
|
"""
|
|
if stop is not None and stop():
|
|
return {"action": "cancelled"}
|
|
if _format_changed(settings):
|
|
return {"action": "stopped"}
|
|
dst = Path(dst)
|
|
try:
|
|
old = self._old_group_previews([Path(src), *siblings], dst, settings)
|
|
if not self._current_preview_usable(src, dst, settings):
|
|
made = self._generate_one(
|
|
src, dst, settings, is_video, rebuild=True, placeholder=False
|
|
)
|
|
if made["action"] != "generated":
|
|
return {"action": "error", "error": made.get("error")}
|
|
dst = Path(made["path"])
|
|
if dst.suffix != settings.extension or not dst.is_file():
|
|
return {"action": "error", "error": "The new preview was not written"}
|
|
if not os.path.isfile(src):
|
|
return {"action": "error", "error": SOURCE_GONE}
|
|
if _format_changed(settings):
|
|
return {"action": "stopped"}
|
|
old = [(path, size) for path, size in old if not _same_file(path, dst)]
|
|
removed, kept = self._remove_old_previews(old)
|
|
if kept:
|
|
return {"action": "error", "error": OLD_NOT_REMOVED}
|
|
label = thumbnail_settings.FORMAT_LABELS[settings.format]
|
|
return {
|
|
"action": "converted",
|
|
"before": removed,
|
|
"after": dst.stat().st_size,
|
|
"detail": f"Converted to {label}",
|
|
}
|
|
except Exception as exc:
|
|
self.logger.warning(f"Conversion failed for {src.name}", exc_info=True)
|
|
return {"action": "error", "error": safe_error_message(exc)}
|
|
|
|
@staticmethod
|
|
def _old_group_previews(sources, dst, settings):
|
|
"""``[(path, size)]`` of every source's old previews, each path once."""
|
|
seen, old = set(), []
|
|
for source in sources:
|
|
for path, size in _old_previews(source, dst, settings):
|
|
key = os.path.normcase(str(path))
|
|
if key not in seen:
|
|
seen.add(key)
|
|
old.append((path, size))
|
|
return old
|
|
|
|
@staticmethod
|
|
def _current_preview_usable(src, dst, settings):
|
|
"""A current preview at least as new as *src* that decodes (M4)."""
|
|
if not _thumbnail_is_fresh(src, dst):
|
|
return False
|
|
try:
|
|
verify_thumbnail(dst, settings.format)
|
|
except ThumbnailVerifyError:
|
|
return False
|
|
return True
|
|
|
|
def _remove_old_previews(self, old):
|
|
"""``(bytes removed, [kept paths])``; a file already gone counts as
|
|
removed, with no bytes (another run or a sibling removed it)."""
|
|
removed, kept = 0, []
|
|
for path, size in old:
|
|
try:
|
|
os.remove(path)
|
|
except FileNotFoundError:
|
|
continue
|
|
except OSError as exc:
|
|
self.logger.warning(f"Could not remove old preview {path}: {exc}")
|
|
kept.append(path)
|
|
continue
|
|
removed += size
|
|
return removed, kept
|