mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-06-05 19:24:39 +00:00
fix: add timeout lock to ensure completion of vecotrisation
This commit is contained in:
@@ -19,6 +19,8 @@ export class VectorWorkerManager {
|
||||
private initializationMutex = false;
|
||||
private idleTimer: NodeJS.Timeout | null = null;
|
||||
private unloadTimer: NodeJS.Timeout | null = null;
|
||||
/** Non-streaming `process` jobs must not hit the idle shutdown mid-flight. */
|
||||
private vectorizationLockCount = 0;
|
||||
|
||||
private streamingSession: {
|
||||
isActive: boolean;
|
||||
@@ -92,6 +94,12 @@ export class VectorWorkerManager {
|
||||
break;
|
||||
|
||||
case "progress":
|
||||
if (
|
||||
data.status === "processing" ||
|
||||
data.status === "started"
|
||||
) {
|
||||
this.bumpActivityDuringVectorization();
|
||||
}
|
||||
if (this.progressCallback) {
|
||||
this.progressCallback(data);
|
||||
|
||||
@@ -120,6 +128,7 @@ export class VectorWorkerManager {
|
||||
break;
|
||||
|
||||
case "streamingProgress":
|
||||
this.bumpActivityDuringVectorization();
|
||||
if (this.progressCallback && this.streamingSession?.isActive) {
|
||||
const { processed } = data;
|
||||
this.progressCallback({
|
||||
@@ -150,6 +159,7 @@ export class VectorWorkerManager {
|
||||
this.readyPromise = null;
|
||||
this.progressCallback = null;
|
||||
this.initializationMutex = false;
|
||||
this.vectorizationLockCount = 0;
|
||||
this.clearIdleTimer();
|
||||
this.clearUnloadTimer();
|
||||
if (this.streamingSession?.isActive) {
|
||||
@@ -158,15 +168,27 @@ export class VectorWorkerManager {
|
||||
}
|
||||
|
||||
private startIdleTimer() {
|
||||
if (this.vectorizationLockCount > 0 || this.streamingSession?.isActive) {
|
||||
return;
|
||||
}
|
||||
this.clearIdleTimer();
|
||||
this.idleTimer = setTimeout(() => {
|
||||
if (!this.streamingSession?.isActive && this.isInitialized) {
|
||||
if (this.vectorizationLockCount > 0) return;
|
||||
if (this.streamingSession?.isActive) return;
|
||||
if (!this.isInitialized) return;
|
||||
console.debug("[VectorWorker] Auto-shutting down due to 2 minutes of inactivity");
|
||||
this.resetWorkerState();
|
||||
}
|
||||
}, 120000); // 2 minutes
|
||||
}
|
||||
|
||||
/** Extends idle deadline while embeddings run; cheap if no idle timer is scheduled. */
|
||||
private bumpActivityDuringVectorization() {
|
||||
if (this.vectorizationLockCount > 0 || this.streamingSession?.isActive) {
|
||||
this.clearIdleTimer();
|
||||
}
|
||||
this.updateActivity();
|
||||
}
|
||||
|
||||
private clearIdleTimer() {
|
||||
if (this.idleTimer) {
|
||||
clearTimeout(this.idleTimer);
|
||||
@@ -184,6 +206,7 @@ export class VectorWorkerManager {
|
||||
private scheduleUnload(delay: number = 10000) {
|
||||
this.clearUnloadTimer();
|
||||
this.unloadTimer = setTimeout(() => {
|
||||
if (this.vectorizationLockCount > 0) return;
|
||||
if (!this.streamingSession?.isActive && this.isInitialized) {
|
||||
console.debug("[VectorWorker] Auto-unloading after processing complete");
|
||||
this.resetWorkerState();
|
||||
@@ -193,6 +216,9 @@ export class VectorWorkerManager {
|
||||
|
||||
private updateActivity() {
|
||||
this.clearUnloadTimer();
|
||||
if (this.vectorizationLockCount > 0 || this.streamingSession?.isActive) {
|
||||
return;
|
||||
}
|
||||
this.startIdleTimer();
|
||||
}
|
||||
|
||||
@@ -303,6 +329,11 @@ export class VectorWorkerManager {
|
||||
// stopHeartbeat/loadAll/loadDynamicItems on the main thread while
|
||||
// vectorization was still running — blocking indexing-progress handlers
|
||||
// and freezing the chip on “Vectorization in progress”.
|
||||
this.vectorizationLockCount++;
|
||||
this.clearIdleTimer();
|
||||
this.clearUnloadTimer();
|
||||
|
||||
try {
|
||||
await new Promise<void>((resolve) => {
|
||||
let settled = false;
|
||||
const wrap: ProgressCallback = (data) => {
|
||||
@@ -318,7 +349,6 @@ export class VectorWorkerManager {
|
||||
}
|
||||
};
|
||||
this.progressCallback = wrap;
|
||||
this.updateActivity();
|
||||
|
||||
console.debug(
|
||||
`Sending ${uniqueItems.length} unique items to worker for processing.`,
|
||||
@@ -329,6 +359,15 @@ export class VectorWorkerManager {
|
||||
data: { items: uniqueItems },
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
this.vectorizationLockCount = Math.max(0, this.vectorizationLockCount - 1);
|
||||
if (
|
||||
this.vectorizationLockCount === 0 &&
|
||||
!this.streamingSession?.isActive
|
||||
) {
|
||||
this.startIdleTimer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async startStreamingSession(
|
||||
|
||||
Reference in New Issue
Block a user