Added streaming mode for better stage concurrency.

This commit is contained in:
DebaucheryLibrarian
2026-11-27 01:00:54 +01:00
parent 8561dad0ca
commit 70e7cef312
9 changed files with 533 additions and 41 deletions
+80
View File
@@ -249,6 +249,85 @@ async function scrapeReleases(baseReleases, entitiesByHostname, type) {
);
}
function deepFreeze(value) {
if (value && typeof value === 'object' && !Object.isFrozen(value)) {
Object.freeze(value);
Object.values(value).forEach((child) => deepFreeze(child));
}
return value;
}
// streaming counterpart of scrapeReleases, fetches one release at a time as stubs come in
// beforeFetchScenes data is a snapshot: it's cached per entity, recomputed when the scraper's beforeFetchScenesTtl (ms) expires or with --refresh-pre-data,
// and frozen, so scrapers can't keep state in it or rely on it being the same object for the entire run
function createReleaseFetcher(type = 'scene') {
const entitiesById = {};
const entitiesByHostname = {};
const preDataByEntityId = new Map();
async function addEntities(baseReleases) {
const fetchedEntitiesByHostname = await fetchReleaseEntities(baseReleases, { appendBySlug: false });
Object.assign(entitiesByHostname, fetchedEntitiesByHostname);
[...Object.values(fetchedEntitiesByHostname), ...baseReleases.map((baseRelease) => baseRelease.entity)]
.filter(Boolean)
.forEach((entity) => { entitiesById[entity.id] = entity; });
}
async function getPreData(entity) {
if (!entity.scraper?.beforeFetchScenes) {
return null;
}
const cached = preDataByEntityId.get(entity.id);
if (cached && !argv.refreshPreData && (!cached.expiresAt || cached.expiresAt > Date.now())) {
return cached.promise;
}
const ttl = entity.scraper.beforeFetchScenesTtl;
const promise = entity.scraper.beforeFetchScenes(entity, getRecursiveParameters(entity)).then((preData) => deepFreeze(preData));
preDataByEntityId.set(entity.id, { promise, expiresAt: ttl ? Date.now() + ttl : null });
promise.catch(() => {
// retry after a minute, rather than for every scene
preDataByEntityId.set(entity.id, { promise, expiresAt: Date.now() + 60_000 });
});
return promise;
}
async function fetchRelease(baseRelease) {
const entity = entitiesById[baseRelease.entity?.id]
|| entitiesByHostname[urlToHostname(baseRelease.url)]
|| baseRelease.entity;
if (!entity || !shouldDeepScrape(baseRelease)) {
return scrapeRelease(baseRelease, { byId: entitiesById, byHostname: entitiesByHostname }, type);
}
try {
const preData = await getPreData(entity);
const entityWithPreData = { ...entity, preData };
// the release may have been resolved by hostname, attach the entity so scrapeRelease uses the one with the pre-data
return await scrapeRelease({ ...baseRelease, entity: entityWithPreData }, {
byId: { [entity.id]: entityWithPreData },
byHostname: {},
}, type);
} catch (error) {
logger.error(`Failed to prepare deep scrape for ${baseRelease.url}: ${error.message}`);
return baseRelease;
}
}
return { addEntities, fetchRelease };
}
async function fetchReleases(baseReleasesOrUrls, type = 'scene') {
const baseReleases = toBaseReleases(baseReleasesOrUrls);
const entitiesByHostname = await fetchReleaseEntities(baseReleases, { appendBySlug: false });
@@ -269,6 +348,7 @@ async function fetchMovies(baseReleasesOrUrls) {
}
module.exports = {
createReleaseFetcher,
fetchReleases,
fetchScenes,
fetchMovies,