* feat(thumbnails): thumbnail format, quality and size as server settings
Thumbnail format (webp default, jxl, jpeg), quality (1-100, default 80)
and size limit (128-2048 px, default 512) are server settings in the
config.json thumbnails section, saved with the settings revision. The
never-read gallery thumbnail_size is gone: older saves wrote its unused
default 256 into config.json.
One module names previews (<stem>_thumb.webp/.jxl/.jpg) and finds the
preview a source owns, preferring the configured format; legacy names
are still found. The encoder module writes every preview atomically and
verifies it decodes. JPEG XL needs no new dependency: Pillow (natively
or with pillow_jxl), ffmpeg with libjxl, or cjxl; without one the
settings refuse jxl (400 jxl-unavailable) and GET says why. Encoder
processes are tracked, bounded and killed at shutdown.
* feat(thumbnails): every preview writer and lookup follows the thumbnail settings
Automatic previews (capture and first view), the thumbnail job, the
legacy gallery routes and the history route write <stem>_thumb.webp
(or .jxl/.jpg) with the configured quality and size limit, for images
and videos alike: video frames, ffmpeg's included (a PNG on stdout,
never upscaled), and the placeholder are encoded like images.
Listings, duplicates, enrichment, the autotag scan and the history
route show any preview the file owns, the current format first, so
older _thumb.png previews keep working until converted. A request for
an older name answers the current preview. Previews are served with
an explicit image/webp or image/jxl type and the same cache headers.
The plan and status count missing, outdated (another format) and
ready previews; the missing job never writes beside an outdated one.
Removing a file removes its previews in every format; the clear
removes .webp and .jxl too. Encoder processes are killed at shutdown.
A preview is only a placeholder: inspection refuses paths under
thumbnails/ and always reads the original, and the inspector cover
accepts the listing's preview in any format the file owns.
* feat(thumbnails): Convert outdated thumbnails as an Activity job
POST /jobs/thumbnails {mode: "convert"} starts "Convert outdated
thumbnails" under the thumbnail job's kind, guarded admission, worker
threads and cancel at a file boundary. For each file whose preview is
in another format, it writes the new preview from the source (atomic,
verified; a video with no decodable frame fails instead of getting the
placeholder), then removes that file's old-format previews in its
thumbnails folder. A failure keeps the old preview; orphan previews
and links are never touched. The final line reports converted, failed
and bytes before and after. The plan counts it with mode=convert; the
legacy gallery stream refuses to attach to a running conversion.
* feat(thumbnails): thumbnail settings in Appearance and Convert outdated in the workspace
System > Appearance gains a Thumbnails surface: format (WebP, JPEG XL,
JPEG), quality and size limit, saved to the server with the settings
revision. JPEG XL is offered only when the server has an encoder, else
the option is disabled with the server's reason (D130); a note says
when this browser cannot show JPEG XL. Saving a new format converts
nothing: the row then says previews keep their old format.
Thumbnail maintenance gains the Convert outdated scope and names the
server's settings instead of its own quality and size fields (D131).
Overview's thumbnail row reads N missing, M outdated, K failed and
offers Convert outdated while previews are outdated.
The workspace decodes a tiny JPEG XL once: where it cannot, cards, the
Library and the inspector cover load the original for a .jxl preview,
and any preview that fails to load falls back to the original. The
viewer and downloads keep using the original (regression test).
README explains enabling JPEG XL; TESTING.md records D130 and D131.
* test(thumbnails): coverage floors for the thumbnail modules and WebP in page workflows
The two new frontend modules enter the coverage baseline (98% lines);
other floors only rise. The visual fixture's preview is WebP and
Appearance has its Thumbnails surface in the System shell check.
* fix(thumbnails): say when the thumbnail settings could not be read
Like Appearance's other server rows, the Thumbnails rows show 'Server
settings could not be read.' while the read failed, instead of only
staying disabled. Firefox exposed the gap: its JPEG XL note arrived
first and changed the row notes the page workflow counted.
* test(thumbnails): accept the legacy preview's spelling a case-insensitive disk finds
On Windows and macOS the '.jpg' lookup finds an older release's
b_thumb.JPG first, so the listed URL spells '.jpg'; it names the same
file there. The test now accepts either spelling and checks the URL
serves that preview.
* fix(thumbnails): address the final review of the thumbnail format work
- ffmpeg writes JPEG XL with -update 1, so a % in a file name is never an
image-sequence pattern, and reads videos as file:<path>. The clear and a
startup sweep remove temp files that interrupted writes left behind.
- Convert outdated converts each group of same-stem files once, from the
first source in the R2 order. An old preview that is already gone counts
as removed, and each file's bytes count once. A current preview is only
kept if it decodes.
- An encoder that fails makes the write detect encoders again once. Another
encoder is used if one is found; with none, the file is written as WebP
and the availability Appearance shows is refreshed.
- Changing the thumbnail format, quality or size while Convert outdated runs
returns 409 busy. If the format still changes underneath, the job stops
without removing anything.
- Legacy preview spellings are matched case-insensitively, so X_thumb.JPG
counts as outdated.
- History's cover uses the same lookup as Images. It serves .jxl only when
Accept lists image/jxl, and otherwise the original or the placeholder.
- A video whose thumbnail the browser cannot show keeps its own element
(Images) or its label (Library).
* fix(thumbnails): keep the new preview, probe encoders only when gone, sweep reused PIDs
- Convert never removes a path that is the preview it just wrote. It
compares the device and inode, or the resolved and normcased paths when
the disk has no inode numbers. This covers x_thumb.JPG on a
case-insensitive disk.
- A JPEG XL encode failure re-runs encoder detection only when the
encoder's binary or module is gone. A "no encoder" result is checked
again on the settings read, on a jxl save, and after 60 s, so one slow
probe no longer turns JPEG XL off until a restart.
- The startup sweep now also removes temp files that carry this process's
PID but predate its start. Containers reuse PIDs.
* fix(history): let the cover ask for JPEG XL when this browser decodes it
Firefox can decode JPEG XL without listing image/jxl in Accept. When the
workspace's probe decodes JPEG XL, it adds jxl=1 to the History cover URL.
The server serves a .jxl preview when that flag is set or when Accept
lists image/jxl. In every other case it falls back as before. The flag
accepts only 0 or 1, and the response stays no-store.
* fix(thumbnails): keep going past the per-batch cap until every preview is made
The missing, rebuild and Convert outdated jobs stopped after
MAX_THUMBNAIL_FILES (10,000) files and said "N more on the next run", but
nothing ever started that next run. The cap now bounds one batch. After
each batch the job plans again, leaving out every file it already worked
on, and continues until nothing is left, the job is cancelled, or a batch
makes no progress. That last case stops on files that fail every time
instead of looping over them.
Activity progress counts against the overall total, and the final line and
the legacy stream's complete frame report every batch. Admission, worker
threads, cancellation and the #316 rules are unchanged. The Thumbnail
maintenance dialog now counts all the work instead of saying "more on the
next run".
520 lines
21 KiB
Python
520 lines
21 KiB
Python
"""Thumbnail generation as an Activity job (#249 System S5, Task 12; #317).
|
|
|
|
- ``GET /prompt_manager/images/thumbnails/plan?mode=`` counts what a run
|
|
would do over every gallery root and writes nothing: ``{success, targets,
|
|
fresh, outdated, remaining, roots, mode, format, quality, size}``.
|
|
- ``POST /prompt_manager/jobs/thumbnails {mode}`` starts the ``thumbnails``
|
|
job (202 ``{job}``). Its admission class is ``guarded``: it never runs
|
|
beside a record-changing job, and one thumbnail job runs at a time. It
|
|
stops at a file boundary when cancelled, keeping the previews it wrote.
|
|
Its outcome groups are ``generated``, ``skipped`` (already fresh) and
|
|
``errors``.
|
|
|
|
``mode`` is ``missing`` (previews that are absent or older than their file),
|
|
``rebuild`` (every preview again, fresh ones too) or ``convert`` (Convert
|
|
outdated: previews in another format, see ``thumbnail_convert.py``). Format, quality and
|
|
size limit are the server's thumbnail settings (``thumbnail_settings``,
|
|
#317), read once when the job starts and shown in its scope; a ``size`` or
|
|
``quality`` an older client sends is ignored. Every preview is written next
|
|
to the old one, verified and renamed over it, so nothing is cleared first and
|
|
a failure keeps the old preview; originals are only read.
|
|
|
|
The legacy gallery stream ``POST /images/generate-thumbnails/progress``
|
|
starts this job (missing previews, the server's settings; its ``quality``
|
|
preset is ignored, #317 R3) or attaches to the running one. Its first frame,
|
|
``job {id, started}``, names that job and whether this stream started it (the
|
|
gallery cancels by that id, and only a job it started); then come the job's
|
|
legacy ``status``/``start``/``progress``/``file_error``/``complete`` frames. A
|
|
client that goes away no longer stops the work: the job outlives the request,
|
|
as a scan does.
|
|
"""
|
|
|
|
import asyncio
|
|
import functools
|
|
import json
|
|
import time
|
|
from dataclasses import dataclass
|
|
|
|
from aiohttp import web
|
|
|
|
from .admin import _plural, _scan_location, _scan_roots, _scan_status_line
|
|
from .images import (
|
|
_BodyTooLarge,
|
|
_new_thumbnail_stats,
|
|
_read_json_body,
|
|
_record_thumbnail_result,
|
|
_thumbnail_batch_size,
|
|
_thumbnail_complete_payload,
|
|
_thumbnail_progress_payload,
|
|
_worker_threads,
|
|
)
|
|
from .job_routes import busy_response
|
|
from .jobs import THUMBNAIL_GROUP_NAMES, THUMBNAILS_JOB_KIND, JobBusy
|
|
from .thumbnail_convert import CONVERT_JOB_TITLE
|
|
from .thumbnail_encode import writer_settings
|
|
from .thumbnail_settings import ThumbnailSettings
|
|
|
|
THUMBNAIL_JOB_TITLE = "Thumbnail generation"
|
|
THUMBNAIL_MODES = ("missing", "rebuild", "convert")
|
|
_OPTIONS_ERROR = 'mode must be "missing", "rebuild" or "convert"'
|
|
CANCELLED_ERROR = "Thumbnail generation was cancelled"
|
|
CANCELLED_BEFORE_WRITING = "Cancelled before any preview was written"
|
|
NOTHING_TO_PREVIEW = "No images or videos to preview"
|
|
_MODE_LOG = {"missing": "missing previews", "rebuild": "every preview"}
|
|
# Outcome rows: what happened to each file's preview.
|
|
_DETAILS = {
|
|
"generated": "Preview written",
|
|
"skipped": "Preview already up to date",
|
|
}
|
|
_LOG_PREFIXES = {"generated": "+", "errors": "!"}
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ThumbnailOptions:
|
|
"""One run's choices: the mode and the thumbnail settings it writes with."""
|
|
|
|
mode: str
|
|
settings: ThumbnailSettings
|
|
|
|
@property
|
|
def rebuild(self):
|
|
return self.mode == "rebuild"
|
|
|
|
def scope(self):
|
|
return {
|
|
"mode": self.mode,
|
|
"format": self.settings.format,
|
|
"quality": self.settings.quality,
|
|
"size": self.settings.edge,
|
|
}
|
|
|
|
|
|
def thumbnail_options(raw, *, from_query=False):
|
|
"""``(ThumbnailOptions, None)`` from a body or query, or ``(None, message)``.
|
|
|
|
An absent mode is ``missing``; the settings are the server's now.
|
|
"""
|
|
if not isinstance(raw, dict) and not hasattr(raw, "get"):
|
|
return None, _OPTIONS_ERROR
|
|
mode = raw.get("mode", "missing")
|
|
if mode not in THUMBNAIL_MODES:
|
|
return None, _OPTIONS_ERROR
|
|
return ThumbnailOptions(mode, writer_settings()), None
|
|
|
|
|
|
def _invalid(message):
|
|
return web.json_response(
|
|
{"success": False, "code": "invalid-request", "error": message}, status=400
|
|
)
|
|
|
|
|
|
def _frame(event, data):
|
|
"""A legacy stream frame as the job publishes it."""
|
|
return {"event": event, "data": data}
|
|
|
|
|
|
def _finished_line(plan, stats):
|
|
"""A finished job's status line: what it wrote, or that it had nothing to do."""
|
|
if not plan["targets"]:
|
|
if plan["skipped"]:
|
|
return f"No previews to generate · {plan['skipped']:,} already up to date"
|
|
return NOTHING_TO_PREVIEW
|
|
parts = [f"{_plural(stats['generated'], 'preview')} written"]
|
|
if stats["errors"]:
|
|
parts.append(_plural(len(stats["errors"]), "error"))
|
|
if plan["remaining"]:
|
|
parts.append(f"{plan['remaining']:,} more on the next run")
|
|
return " · ".join(parts)
|
|
|
|
|
|
class ThumbnailJobsMixin:
|
|
"""The thumbnail plan, the thumbnail job and the legacy stream on it."""
|
|
|
|
async def get_thumbnail_plan(self, request):
|
|
"""What a thumbnail run would do (read-only; capped like a run)."""
|
|
options, error = thumbnail_options(request.query, from_query=True)
|
|
if error:
|
|
return _invalid(error)
|
|
try:
|
|
output_dirs = await self._run_in_executor(self._get_all_output_dirs)
|
|
plan = await self._run_in_executor(
|
|
self._plan_gallery_thumbnails,
|
|
output_dirs,
|
|
options.mode,
|
|
settings=options.settings,
|
|
)
|
|
except Exception:
|
|
self.logger.exception("Thumbnail plan failed")
|
|
return web.json_response(
|
|
{
|
|
"success": False,
|
|
"code": None,
|
|
"error": "The thumbnail plan could not be read",
|
|
},
|
|
status=500,
|
|
)
|
|
return web.json_response(
|
|
{
|
|
"success": True,
|
|
"targets": len(plan["targets"]),
|
|
"fresh": plan["fresh"],
|
|
"outdated": plan["outdated"],
|
|
"remaining": plan["remaining"],
|
|
"roots": [self._public_path(root) for root in output_dirs],
|
|
**options.scope(),
|
|
},
|
|
headers={"Cache-Control": "no-store"},
|
|
)
|
|
|
|
async def start_thumbnail_job(self, request):
|
|
"""Start the thumbnail job (202), or 409 naming the job in the way."""
|
|
body = {}
|
|
if request.can_read_body:
|
|
try:
|
|
body = await _read_json_body(request)
|
|
except _BodyTooLarge:
|
|
return web.json_response(
|
|
{"success": False, "code": "too-large", "error": "Body too large"},
|
|
status=413,
|
|
)
|
|
except (ValueError, UnicodeDecodeError):
|
|
return _invalid(_OPTIONS_ERROR)
|
|
options, error = thumbnail_options(body)
|
|
if error:
|
|
return _invalid(error)
|
|
try:
|
|
job = self._start_thumbnail_job(options)
|
|
except JobBusy as busy:
|
|
return busy_response(busy)
|
|
return web.json_response({"success": True, "job": job.summary()}, status=202)
|
|
|
|
def _start_thumbnail_job(self, options):
|
|
"""Register the thumbnail job synchronously; raises JobBusy.
|
|
|
|
``convert`` runs Convert outdated (``thumbnail_convert.py``) under the
|
|
same kind, admission, cancel and groups.
|
|
"""
|
|
convert = options.mode == "convert"
|
|
runner = self._run_convert_job if convert else self._run_thumbnail_job
|
|
return self._jobs.start(
|
|
THUMBNAILS_JOB_KIND,
|
|
CONVERT_JOB_TITLE if convert else THUMBNAIL_JOB_TITLE,
|
|
admission="guarded",
|
|
cancellable=True,
|
|
scope=options.scope(),
|
|
runner=functools.partial(runner, options=options),
|
|
groups=THUMBNAIL_GROUP_NAMES,
|
|
)
|
|
|
|
# ── the job ───────────────────────────────────────────────────────
|
|
|
|
async def _run_thumbnail_job(self, job, options):
|
|
try:
|
|
state = await self._thumbnail_job_body(job, options)
|
|
except Exception:
|
|
self.logger.exception("Thumbnail generation failed")
|
|
job.update(status_line="Thumbnail generation stopped on an internal error.")
|
|
job.log_line("Thumbnails failed · internal error")
|
|
job.publish(
|
|
_frame(
|
|
"error",
|
|
{
|
|
"error": "An internal error occurred",
|
|
"message": "Thumbnail generation failed. Check server logs.",
|
|
},
|
|
)
|
|
)
|
|
state = "failed"
|
|
job.finish(state)
|
|
|
|
async def _thumbnail_job_body(self, job, options):
|
|
"""Plan, then generate in worker batches; returns the final state.
|
|
|
|
Every exit sets the job's final status line (I3): what was written,
|
|
that there was nothing to do, or what a cancel kept.
|
|
"""
|
|
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"Thumbnails failed · {message}")
|
|
job.publish(_frame("error", {"error": message}))
|
|
return "failed"
|
|
folders = _plural(len(output_dirs), "output folder")
|
|
job.scope["output_folders"] = len(output_dirs)
|
|
job.update(status_line=f"Looking for media files in {folders}")
|
|
job.log_line(
|
|
f"Thumbnails started · {folders} · {_MODE_LOG[options.mode]} · "
|
|
f"{options.settings.describe()}"
|
|
)
|
|
job.publish(
|
|
_frame(
|
|
"status",
|
|
{"phase": "scanning", "message": "Scanning for images and videos..."},
|
|
)
|
|
)
|
|
plan = await self._plan_thumbnail_batch(job, options, output_dirs)
|
|
if job.cancelled: # M2: stopped during the walk, before any start frame
|
|
return self._end_cancelled(job, 0, CANCELLED_BEFORE_WRITING)
|
|
roots = _scan_roots(output_dirs)
|
|
self._record_fresh(job, plan, roots)
|
|
stats, start, left, attempted = await self._generate_in_batches(
|
|
job, options, plan, roots, output_dirs
|
|
)
|
|
self.invalidate_gallery_cache()
|
|
generated = _plural(stats["generated"], "preview")
|
|
if job.cancelled:
|
|
return self._end_cancelled(
|
|
job, stats["generated"], f"Cancelled · {generated} written"
|
|
)
|
|
overall = {"targets": attempted, "skipped": plan["skipped"], "remaining": left}
|
|
payload = _thumbnail_complete_payload(
|
|
len(attempted) + plan["skipped"] + left,
|
|
stats,
|
|
start,
|
|
remaining=left,
|
|
processed=len(attempted),
|
|
)
|
|
job.update(progress=100, status_line=_finished_line(overall, stats))
|
|
if left:
|
|
job.log_line(f"{left:,} more previews are left for the next run")
|
|
errors = _plural(len(stats["errors"]), "error")
|
|
job.log_line(f"Thumbnails finished · {generated} written · {errors}")
|
|
self.logger.info(payload["message"])
|
|
job.publish(_frame("complete", payload))
|
|
return "finished"
|
|
|
|
async def _plan_thumbnail_batch(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,
|
|
options.mode,
|
|
stop=lambda: job.cancelled,
|
|
settings=options.settings,
|
|
exclude=exclude,
|
|
)
|
|
|
|
async def _generate_in_batches(self, job, options, plan, roots, output_dirs):
|
|
"""Work through ``plan``, then plan again after each batch until
|
|
nothing is left, the job is cancelled, or a batch made no progress
|
|
(every file failed: they are not planned again, and the rest waits
|
|
for the next run). Files worked on are never planned twice. Returns
|
|
``(stats, start, files left, files worked on)``; progress counts
|
|
against the overall total."""
|
|
run = {
|
|
"index": 0,
|
|
"batches": 0,
|
|
"total": len(plan["targets"]) + plan["remaining"],
|
|
}
|
|
stats = _new_thumbnail_stats()
|
|
stats["skipped"] = plan["skipped"]
|
|
start = time.monotonic()
|
|
self._publish_thumbnail_start(job, plan, run["total"])
|
|
attempted = set()
|
|
while True:
|
|
before = stats["generated"] + stats["skipped"]
|
|
await self._generate_thumbnail_targets(
|
|
job, options, plan, roots, stats, run, start
|
|
)
|
|
attempted.update(src for src, _dst, _video in plan["targets"])
|
|
progressed = stats["generated"] + stats["skipped"] > before
|
|
if job.cancelled or not plan["remaining"] or not progressed:
|
|
return stats, start, plan["remaining"], attempted
|
|
plan = await self._plan_thumbnail_batch(
|
|
job, options, output_dirs, exclude=attempted
|
|
)
|
|
if job.cancelled:
|
|
return stats, start, 0, attempted
|
|
run["total"] = run["index"] + len(plan["targets"]) + plan["remaining"]
|
|
job.update(total=run["total"])
|
|
if not plan["targets"]:
|
|
return stats, start, plan["remaining"], attempted
|
|
|
|
@staticmethod
|
|
def _end_cancelled(job, generated, status_line):
|
|
"""Close a cancelled job: its status line, log line and error frame."""
|
|
job.update(status_line=status_line)
|
|
job.log_line(f"Thumbnails cancelled · {_plural(generated, 'preview')} written")
|
|
job.publish(
|
|
_frame("error", {"error": CANCELLED_ERROR, "message": CANCELLED_ERROR})
|
|
)
|
|
return "cancelled"
|
|
|
|
def _record_fresh(self, job, plan, roots):
|
|
"""List the already fresh previews under Skipped (no log line each)."""
|
|
rows = []
|
|
for src in plan["fresh_files"]:
|
|
path, label, index = _scan_location(src, roots)
|
|
rows.append(
|
|
{
|
|
"path": path,
|
|
"root": label,
|
|
"root_index": index,
|
|
"detail": _DETAILS["skipped"],
|
|
}
|
|
)
|
|
job.record_counted("skipped", plan["skipped"], rows)
|
|
if plan["skipped"]:
|
|
job.log_line(f"= {plan['skipped']:,} previews already up to date")
|
|
|
|
@staticmethod
|
|
def _publish_thumbnail_start(job, plan, total):
|
|
"""The legacy stream's start frame, with the job's overall total."""
|
|
targets = plan["targets"]
|
|
video_count = sum(1 for _, _, is_video in targets if is_video)
|
|
message = (
|
|
f"Found {total - video_count} images and {video_count} videos to process"
|
|
)
|
|
if plan["skipped"]:
|
|
message += f" ({plan['skipped']} already have thumbnails)"
|
|
job.update(total=total)
|
|
if total: # never "Processing file 0 of 0" (I3)
|
|
job.update(status_line=_scan_status_line(0, total, 0, 0))
|
|
job.publish(
|
|
_frame(
|
|
"start",
|
|
{
|
|
"total_images": total,
|
|
"skipped": plan["skipped"],
|
|
"remaining": 0,
|
|
"phase": "processing",
|
|
"image_count": total - video_count,
|
|
"video_count": video_count,
|
|
"message": message,
|
|
},
|
|
)
|
|
)
|
|
|
|
async def _generate_thumbnail_targets(
|
|
self, job, options, plan, roots, stats, run, start
|
|
):
|
|
"""Generate one batch (``plan``'s targets) in worker batches,
|
|
publishing each file against the job's overall ``run`` counters."""
|
|
targets = plan["targets"]
|
|
batch_size = _thumbnail_batch_size(_worker_threads())
|
|
generate = {
|
|
"rebuild": options.rebuild,
|
|
"stop": lambda: job.cancelled,
|
|
}
|
|
for batch_start in range(0, len(targets), batch_size):
|
|
if job.cancelled:
|
|
break
|
|
batch = targets[batch_start : batch_start + batch_size]
|
|
results = await self._run_in_executor(
|
|
self._generate_batch, batch, options.settings, **generate
|
|
)
|
|
for (src, _dst, _video), result in zip(batch, results):
|
|
if result["action"] == "cancelled":
|
|
continue
|
|
run["index"] += 1
|
|
index, total = run["index"], max(run["total"], run["index"])
|
|
_record_thumbnail_result(stats, result)
|
|
self._record_thumbnail_outcome(job, src, result, roots)
|
|
job.update(processed=index, progress=index * 100 // total)
|
|
if result["action"] == "error":
|
|
job.publish(
|
|
_frame(
|
|
"file_error",
|
|
{"file": result["file"], "error": result["error"]},
|
|
)
|
|
)
|
|
job.publish(
|
|
_frame(
|
|
"progress",
|
|
_thumbnail_progress_payload(index, total, stats, result, start),
|
|
)
|
|
)
|
|
run["batches"] += 1
|
|
job.update(
|
|
status_line=_scan_status_line(
|
|
run["index"], run["total"], run["batches"], time.monotonic() - start
|
|
)
|
|
)
|
|
await asyncio.sleep(0) # let other requests run between batches
|
|
|
|
@staticmethod
|
|
def _record_thumbnail_outcome(job, src, result, roots):
|
|
path, label, index = _scan_location(src, roots)
|
|
where = f"{label}/{path}" if label else path
|
|
if result["action"] == "error":
|
|
group, detail = "errors", result.get("error") or "failed"
|
|
elif result["action"] == "skipped":
|
|
group, detail = "skipped", _DETAILS["skipped"]
|
|
else:
|
|
group, detail = "generated", _DETAILS["generated"]
|
|
row = {"path": path, "root": label, "root_index": index, "detail": detail}
|
|
if group == "skipped":
|
|
job.record_counted(group, 1, [row])
|
|
return
|
|
job.record(group, row, f"{_LOG_PREFIXES[group]} {where} · {detail}")
|
|
|
|
# ── the legacy gallery stream ─────────────────────────────────────
|
|
|
|
async def generate_thumbnails_with_progress(self, request):
|
|
"""The gallery's thumbnail stream (POST, server-sent events).
|
|
|
|
Starts the thumbnail job (missing previews with the server's
|
|
thumbnail settings; the gallery's ``quality`` preset is ignored, R3),
|
|
or attaches to the running thumbnail job, and streams its frames. A
|
|
legacy hold (the blocking generate or clear) or a record-changing
|
|
job refuses it with the stream's error frame. The job goes on when
|
|
the client goes away.
|
|
"""
|
|
response = web.StreamResponse(
|
|
status=200,
|
|
headers={
|
|
"Content-Type": "text/event-stream",
|
|
"Cache-Control": "no-cache",
|
|
"Connection": "keep-alive",
|
|
},
|
|
)
|
|
await response.prepare(request)
|
|
# M3: read the registry after the last await, so a job that started
|
|
# while the headers went out is attached to, not refused.
|
|
job, started, busy = self._legacy_stream_job()
|
|
if busy is not None:
|
|
message = f"{busy.title} already running"
|
|
await self._emit_sse(
|
|
response, "error", {"error": message, "message": message}
|
|
)
|
|
return response
|
|
# No await between the start and the subscription: the lead frame is
|
|
# sent after it, so the stream misses none of the runner's frames.
|
|
events = job.events(lead=_frame("job", {"id": job.id, "started": started}))
|
|
try:
|
|
async for payload in events:
|
|
frame = (
|
|
f"event: {payload['event']}\ndata: {json.dumps(payload['data'])}"
|
|
)
|
|
await response.write(f"{frame}\n\n".encode("utf-8"))
|
|
except Exception as exc: # a write failed: the client went away
|
|
self.logger.info(
|
|
f"Thumbnail progress client disconnected ({exc}); "
|
|
"the thumbnails continue in the background"
|
|
)
|
|
finally:
|
|
# Also when aiohttp cancels the handler on disconnect: the job is
|
|
# its own task and goes on.
|
|
await events.aclose()
|
|
return response
|
|
|
|
def _legacy_stream_job(self):
|
|
"""``(job, started, busy)`` for the legacy stream, synchronously.
|
|
|
|
Attaches to the running cancellable thumbnail job, or starts one for
|
|
the missing previews with the server's settings, or names the job in
|
|
the way.
|
|
"""
|
|
running = self._jobs.active(THUMBNAILS_JOB_KIND)
|
|
if running is not None and running.scope.get("mode") == "convert":
|
|
return None, False, running # no legacy frames to follow (#317)
|
|
if running is not None and running.cancellable:
|
|
return running, False, None
|
|
busy = self._jobs.blocker(THUMBNAILS_JOB_KIND, admission="guarded")
|
|
if busy is not None:
|
|
return None, False, busy
|
|
# blocker() said free and nothing awaited since, so the start is admitted.
|
|
options = ThumbnailOptions("missing", writer_settings())
|
|
return self._start_thumbnail_job(options), True, None
|