Hot reload proactif post-cron pour ratings + mappings + chunks (evite la latence du 1er appel)

This commit is contained in:
unfr
2026-04-24 07:58:22 +02:00
parent fbf99a9ccf
commit 2e84e212ea
3 changed files with 89 additions and 24 deletions

View File

@@ -3,10 +3,10 @@
// stay loaded in memory (replaces the per-request `php searchmultithreads.php`
// fork from the PHP version).
//
// A filesystem watcher detects when the cron rewrites the chunks and recycles
// the worker pool transparently — no server restart needed.
// Hot reload of chunks after a cron rewrite is handled by lib/dataReload.js
// which calls reloadAllPools() exported below.
import { existsSync, watch } from 'node:fs';
import { existsSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Worker } from 'node:worker_threads';
@@ -16,9 +16,6 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
const WORKER_PATH = join(__dirname, 'searchWorker.js');
const pools = new Map();
let watcher = null;
let reloadTimer = null;
const RELOAD_DEBOUNCE_MS = 5000;
class WorkerPool {
constructor(type) {
@@ -67,8 +64,9 @@ class WorkerPool {
}
}
async function reloadAllPools() {
export async function reloadAllPools() {
const types = [...pools.keys()];
if (!types.length) return;
console.log(`Reloading search pools: ${types.join(', ')}`);
for (const type of types) {
const old = pools.get(type);
@@ -79,25 +77,9 @@ async function reloadAllPools() {
}
}
function ensureWatcher() {
if (watcher) return;
try {
watcher = watch(TMDBINTEGRAL_DIR, (_event, filename) => {
if (!filename) return;
if (!/^search(movie|tv)\d+\.json$/.test(filename)) return;
clearTimeout(reloadTimer);
reloadTimer = setTimeout(reloadAllPools, RELOAD_DEBOUNCE_MS);
});
watcher.unref();
} catch (err) {
console.warn(`Cannot watch ${TMDBINTEGRAL_DIR} for chunk reload:`, err.message);
}
}
export function getPool(type) {
if (!pools.has(type)) {
pools.set(type, new WorkerPool(type));
ensureWatcher();
}
return pools.get(type);
}