bugfix queue
This commit is contained in:
@@ -222,6 +222,9 @@ export class UnifiedAPIServer {
|
||||
|
||||
// File download routes (under /api/file)
|
||||
this.setupFileRoutes();
|
||||
|
||||
// Mount management routes (under /api/mounts)
|
||||
this.setupMountRoutes();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -278,6 +281,338 @@ export class UnifiedAPIServer {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup Mount Management API routes
|
||||
* Manages NFS, SMB, and rclone remote storage mounts
|
||||
*/
|
||||
private setupMountRoutes(): void {
|
||||
const MOUNTS_DIR = `${config.META_CORE_PATH}/mounts`;
|
||||
const MOUNTS_FILE = `${MOUNTS_DIR}/mounts.json`;
|
||||
const ERRORS_DIR = `${MOUNTS_DIR}/errors`;
|
||||
const FILES_PATH = config.FILES_PATH;
|
||||
|
||||
// Helper: ensure directories exist
|
||||
const ensureDirs = async () => {
|
||||
const fs = await import('fs/promises');
|
||||
await fs.mkdir(MOUNTS_DIR, { recursive: true });
|
||||
await fs.mkdir(ERRORS_DIR, { recursive: true });
|
||||
};
|
||||
|
||||
// Helper: sanitize name for filesystem
|
||||
const sanitizeName = (name: string): string => {
|
||||
return name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9-_]/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.replace(/^-|-$/g, '')
|
||||
.slice(0, 64);
|
||||
};
|
||||
|
||||
// Helper: read mounts config
|
||||
const readMountsConfig = async (): Promise<{ version: number; mounts: any[] }> => {
|
||||
const fs = await import('fs/promises');
|
||||
try {
|
||||
const data = await fs.readFile(MOUNTS_FILE, 'utf-8');
|
||||
return JSON.parse(data);
|
||||
} catch (e: any) {
|
||||
if (e.code === 'ENOENT') {
|
||||
return { version: 1, mounts: [] };
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper: write mounts config
|
||||
const writeMountsConfig = async (mountsConfig: { version: number; mounts: any[] }) => {
|
||||
const fs = await import('fs/promises');
|
||||
await ensureDirs();
|
||||
await fs.writeFile(MOUNTS_FILE, JSON.stringify(mountsConfig, null, 2));
|
||||
};
|
||||
|
||||
// Helper: check if path is mounted
|
||||
const isMounted = (mountPath: string): boolean => {
|
||||
try {
|
||||
const output = execSync(`findmnt -n "${mountPath}" 2>/dev/null || true`, {
|
||||
encoding: 'utf-8',
|
||||
timeout: 5000 // 5 second timeout to prevent hanging on slow mounts
|
||||
});
|
||||
return output.trim().length > 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Helper: read error for mount
|
||||
const readError = async (id: string): Promise<string | undefined> => {
|
||||
const fs = await import('fs/promises');
|
||||
try {
|
||||
const errorFile = `${ERRORS_DIR}/${id}.error`;
|
||||
const content = await fs.readFile(errorFile, 'utf-8');
|
||||
const lines = content.trim().split('\n');
|
||||
return lines.slice(1).join('\n'); // Skip timestamp line
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
// GET /api/mounts - List all mounts with status
|
||||
this.app.get('/api/mounts', async (request, reply) => {
|
||||
try {
|
||||
await ensureDirs();
|
||||
const mountsConfig = await readMountsConfig();
|
||||
|
||||
const mounts = await Promise.all(mountsConfig.mounts.map(async (mount: any) => {
|
||||
const mounted = isMounted(mount.mountPath);
|
||||
const error = await readError(mount.id);
|
||||
return {
|
||||
...mount,
|
||||
mounted,
|
||||
error,
|
||||
lastChecked: Date.now()
|
||||
};
|
||||
}));
|
||||
|
||||
return { mounts };
|
||||
} catch (error: any) {
|
||||
return reply.status(500).send({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/mounts/rclone/remotes - List available rclone remotes
|
||||
this.app.get('/api/mounts/rclone/remotes', async (request, reply) => {
|
||||
try {
|
||||
const response = await fetch('http://127.0.0.1:5572/config/listremotes', {
|
||||
headers: {
|
||||
'Authorization': 'Basic ' + Buffer.from('admin:admin').toString('base64')
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return { remotes: [] };
|
||||
}
|
||||
|
||||
const data = await response.json() as { remotes?: string[] };
|
||||
const remoteNames = data.remotes || [];
|
||||
|
||||
// Get type for each remote
|
||||
const remotes = await Promise.all(remoteNames.map(async (name: string) => {
|
||||
try {
|
||||
const typeRes = await fetch('http://127.0.0.1:5572/config/get', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Basic ' + Buffer.from('admin:admin').toString('base64')
|
||||
},
|
||||
body: JSON.stringify({ name: name.replace(/:$/, '') })
|
||||
});
|
||||
const typeData = await typeRes.json() as { type?: string };
|
||||
return {
|
||||
name: name.replace(/:$/, ''),
|
||||
type: typeData.type || 'unknown'
|
||||
};
|
||||
} catch {
|
||||
return { name: name.replace(/:$/, ''), type: 'unknown' };
|
||||
}
|
||||
}));
|
||||
|
||||
return { remotes };
|
||||
} catch (error: any) {
|
||||
return { remotes: [] };
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/mounts - Create new mount
|
||||
interface CreateMountBody {
|
||||
name?: string;
|
||||
type?: 'nfs' | 'smb' | 'rclone';
|
||||
enabled?: boolean;
|
||||
nfsServer?: string;
|
||||
nfsPath?: string;
|
||||
smbServer?: string;
|
||||
smbShare?: string;
|
||||
smbUsername?: string;
|
||||
smbPassword?: string;
|
||||
smbDomain?: string;
|
||||
rcloneRemote?: string;
|
||||
rclonePath?: string;
|
||||
}
|
||||
|
||||
this.app.post<{ Body: CreateMountBody }>('/api/mounts', async (request, reply) => {
|
||||
const { randomUUID } = await import('crypto');
|
||||
const fs = await import('fs/promises');
|
||||
|
||||
try {
|
||||
await ensureDirs();
|
||||
const body = request.body;
|
||||
|
||||
if (!body.name) {
|
||||
return reply.status(400).send({ error: 'Mount name is required' });
|
||||
}
|
||||
|
||||
if (!body.type || !['nfs', 'smb', 'rclone'].includes(body.type)) {
|
||||
return reply.status(400).send({ error: 'Valid mount type (nfs, smb, rclone) is required' });
|
||||
}
|
||||
|
||||
const id = randomUUID();
|
||||
const safeName = sanitizeName(body.name);
|
||||
const mountPath = `${FILES_PATH}/${safeName}`;
|
||||
|
||||
// Check if mount path already exists in config
|
||||
const existingConfig = await readMountsConfig();
|
||||
const pathExists = existingConfig.mounts.some((m: any) => m.mountPath === mountPath);
|
||||
if (pathExists) {
|
||||
return reply.status(400).send({ error: `Mount path ${mountPath} already configured` });
|
||||
}
|
||||
|
||||
const mount: Record<string, any> = {
|
||||
id,
|
||||
name: body.name,
|
||||
type: body.type,
|
||||
enabled: body.enabled !== false,
|
||||
desiredMounted: body.enabled !== false, // Auto-mount if enabled
|
||||
mountPath
|
||||
};
|
||||
|
||||
// Type-specific fields
|
||||
if (body.type === 'nfs') {
|
||||
if (!body.nfsServer || !body.nfsPath) {
|
||||
return reply.status(400).send({ error: 'NFS server and path are required' });
|
||||
}
|
||||
mount.nfsServer = body.nfsServer;
|
||||
mount.nfsPath = body.nfsPath;
|
||||
} else if (body.type === 'smb') {
|
||||
if (!body.smbServer || !body.smbShare) {
|
||||
return reply.status(400).send({ error: 'SMB server and share are required' });
|
||||
}
|
||||
mount.smbServer = body.smbServer;
|
||||
mount.smbShare = body.smbShare;
|
||||
|
||||
// Store credentials - password is obscured using rclone
|
||||
if (body.smbUsername) {
|
||||
mount.smbUsername = body.smbUsername;
|
||||
}
|
||||
if (body.smbPassword) {
|
||||
try {
|
||||
// Obscure password using rclone (prevents plaintext storage)
|
||||
const obscured = execSync(`rclone obscure "${body.smbPassword.replace(/"/g, '\\"')}"`, {
|
||||
encoding: 'utf-8',
|
||||
timeout: 5000
|
||||
}).trim();
|
||||
mount.smbPasswordObscured = obscured;
|
||||
} catch (err) {
|
||||
console.error('[Mounts] Failed to obscure password:', err);
|
||||
return reply.status(500).send({ error: 'Failed to secure password' });
|
||||
}
|
||||
}
|
||||
if (body.smbDomain) {
|
||||
mount.smbDomain = body.smbDomain;
|
||||
}
|
||||
} else if (body.type === 'rclone') {
|
||||
if (!body.rcloneRemote) {
|
||||
return reply.status(400).send({ error: 'rclone remote is required' });
|
||||
}
|
||||
mount.rcloneRemote = body.rcloneRemote;
|
||||
mount.rclonePath = body.rclonePath || '';
|
||||
}
|
||||
|
||||
existingConfig.mounts.push(mount);
|
||||
await writeMountsConfig(existingConfig);
|
||||
|
||||
console.log(`[Mounts] Created mount config: ${mount.name} (${mount.type}) -> ${mount.mountPath}`);
|
||||
return { mount };
|
||||
} catch (error: any) {
|
||||
console.error('[Mounts] Error creating mount:', error);
|
||||
return reply.status(500).send({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/mounts/:id/mount - Request mount
|
||||
this.app.post<{ Params: { id: string } }>('/api/mounts/:id/mount', async (request, reply) => {
|
||||
try {
|
||||
const mountsConfig = await readMountsConfig();
|
||||
const mount = mountsConfig.mounts.find((m: any) => m.id === request.params.id);
|
||||
|
||||
if (!mount) {
|
||||
return reply.status(404).send({ error: 'Mount not found' });
|
||||
}
|
||||
|
||||
mount.desiredMounted = true;
|
||||
await writeMountsConfig(mountsConfig);
|
||||
|
||||
console.log(`[Mounts] Mount requested: ${mount.name}`);
|
||||
return { status: 'ok', message: 'Mount requested' };
|
||||
} catch (error: any) {
|
||||
return reply.status(500).send({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/mounts/:id/unmount - Request unmount
|
||||
this.app.post<{ Params: { id: string } }>('/api/mounts/:id/unmount', async (request, reply) => {
|
||||
try {
|
||||
const mountsConfig = await readMountsConfig();
|
||||
const mount = mountsConfig.mounts.find((m: any) => m.id === request.params.id);
|
||||
|
||||
if (!mount) {
|
||||
return reply.status(404).send({ error: 'Mount not found' });
|
||||
}
|
||||
|
||||
mount.desiredMounted = false;
|
||||
await writeMountsConfig(mountsConfig);
|
||||
|
||||
console.log(`[Mounts] Unmount requested: ${mount.name}`);
|
||||
return { status: 'ok', message: 'Unmount requested' };
|
||||
} catch (error: any) {
|
||||
return reply.status(500).send({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE /api/mounts/:id - Remove mount config
|
||||
this.app.delete<{ Params: { id: string } }>('/api/mounts/:id', async (request, reply) => {
|
||||
const fs = await import('fs/promises');
|
||||
|
||||
try {
|
||||
const mountsConfig = await readMountsConfig();
|
||||
const index = mountsConfig.mounts.findIndex((m: any) => m.id === request.params.id);
|
||||
|
||||
if (index === -1) {
|
||||
return reply.status(404).send({ error: 'Mount not found' });
|
||||
}
|
||||
|
||||
const mount = mountsConfig.mounts[index];
|
||||
|
||||
// Request unmount first
|
||||
mount.desiredMounted = false;
|
||||
await writeMountsConfig(mountsConfig);
|
||||
|
||||
// Wait for unmount (poll for up to 15 seconds)
|
||||
for (let i = 0; i < 30; i++) {
|
||||
if (!isMounted(mount.mountPath)) break;
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
}
|
||||
|
||||
// Remove from config
|
||||
mountsConfig.mounts.splice(index, 1);
|
||||
await writeMountsConfig(mountsConfig);
|
||||
|
||||
// Clean up files
|
||||
try {
|
||||
await fs.unlink(`${ERRORS_DIR}/${mount.id}.error`);
|
||||
} catch { /* ignore */ }
|
||||
try {
|
||||
await fs.rmdir(mount.mountPath);
|
||||
} catch { /* ignore */ }
|
||||
|
||||
console.log(`[Mounts] Deleted mount: ${mount.name}`);
|
||||
return { status: 'ok' };
|
||||
} catch (error: any) {
|
||||
console.error('[Mounts] Error deleting mount:', error);
|
||||
return reply.status(500).send({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
console.log('[UnifiedAPIServer] Mount management routes registered at /api/mounts/*');
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup FUSE API routes
|
||||
*/
|
||||
@@ -1108,26 +1443,47 @@ export class UnifiedAPIServer {
|
||||
|
||||
// Get active processing queue (files currently being processed)
|
||||
this.app.get('/api/processing/queue', async (request, reply) => {
|
||||
const items: Array<{ path: string; phase: 'light' | 'hash'; startTime?: number }> = [];
|
||||
const items: Array<{ path: string; phase: 'light' | 'hash'; startTime?: number; plugin?: string }> = [];
|
||||
|
||||
// Get files in light processing
|
||||
const lightProcessing = this.unifiedStateManager!.getLightProcessingFiles();
|
||||
for (const [filePath, info] of lightProcessing) {
|
||||
// Get running tasks from TaskScheduler (container plugin tasks)
|
||||
const taskScheduler = this.getTaskScheduler?.();
|
||||
const runningTasks = taskScheduler?.getRunningTasks() || [];
|
||||
|
||||
// Add running plugin tasks - map queue type to phase
|
||||
// 'fast' queue = 'light' phase, 'background' queue = 'hash' phase
|
||||
for (const task of runningTasks) {
|
||||
items.push({
|
||||
path: filePath,
|
||||
phase: 'light',
|
||||
startTime: info.lightProcessingStartedAt
|
||||
path: task.filePath,
|
||||
phase: task.queue === 'background' ? 'hash' : 'light',
|
||||
startTime: task.startTime,
|
||||
plugin: task.pluginId
|
||||
});
|
||||
}
|
||||
|
||||
// Get files in hash processing
|
||||
// Get files in light processing (midhash256 computation - no plugin yet)
|
||||
const lightProcessing = this.unifiedStateManager!.getLightProcessingFiles();
|
||||
for (const [filePath, info] of lightProcessing) {
|
||||
// Only add if not already in plugin tasks
|
||||
if (!runningTasks.some(t => t.filePath === filePath)) {
|
||||
items.push({
|
||||
path: filePath,
|
||||
phase: 'light',
|
||||
startTime: info.lightProcessingStartedAt
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Get files in hash processing (waiting for or running background tasks)
|
||||
const hashProcessing = this.unifiedStateManager!.getHashProcessingFiles();
|
||||
for (const [filePath, info] of hashProcessing) {
|
||||
items.push({
|
||||
path: filePath,
|
||||
phase: 'hash',
|
||||
startTime: info.hashProcessingStartedAt
|
||||
});
|
||||
// Only add if not already in plugin tasks
|
||||
if (!runningTasks.some(t => t.filePath === filePath)) {
|
||||
items.push({
|
||||
path: filePath,
|
||||
phase: 'hash',
|
||||
startTime: info.hashProcessingStartedAt
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { items };
|
||||
@@ -1135,24 +1491,10 @@ export class UnifiedAPIServer {
|
||||
|
||||
// Get unified processing status snapshot (4 tabs: pending, lightProcessing, hashProcessing, done)
|
||||
this.app.get('/api/processing/status', async (request, reply) => {
|
||||
// Get ALL file paths currently in VFS (most accurate count)
|
||||
const allVfsFiles = this.vfs.getAllFiles();
|
||||
|
||||
// Count only actual media files (not virtual metadata files like .meta, .nfo)
|
||||
const mediaFiles = allVfsFiles.filter(f => {
|
||||
return !f.endsWith('.meta') && !f.endsWith('.nfo') && !f.endsWith('.xml');
|
||||
});
|
||||
const actualMediaFileCount = mediaFiles.length;
|
||||
|
||||
// Count files with completed hash computation
|
||||
const filesWithHash = this.vfs.countFilesWithHash();
|
||||
|
||||
// Get snapshot with actual VFS counts
|
||||
const snapshot = this.unifiedStateManager!.getSnapshot(actualMediaFileCount, this.fastQueueConcurrency, this.backgroundQueueConcurrency);
|
||||
|
||||
// Override totalDone with actual count of files with hashes in VFS
|
||||
// (not just current session, but ALL files in VFS with completed hash)
|
||||
snapshot.totalDone = filesWithHash;
|
||||
// Get snapshot from state manager
|
||||
// Note: VFS-related features have been moved to meta-fuse
|
||||
// totalDone now uses state manager's count of fully processed files
|
||||
const snapshot = this.unifiedStateManager!.getSnapshot(0, this.fastQueueConcurrency, this.backgroundQueueConcurrency);
|
||||
|
||||
// Add queue status if available
|
||||
if (this.getQueueStatus) {
|
||||
@@ -1168,6 +1510,15 @@ export class UnifiedAPIServer {
|
||||
const backgroundQueueRunning = queueStatus.backgroundQueue?.running || 0;
|
||||
const backgroundQueuePending = queueStatus.backgroundQueue?.pending || 0;
|
||||
|
||||
// "Discovered" now represents files waiting for fast queue (validated but not yet processing)
|
||||
// This merges the old "discovered" and "awaitingFastQueue" concepts
|
||||
snapshot.totalDiscovered = fastQueuePending;
|
||||
snapshot.awaitingFastQueue = fastQueuePending; // Keep for backward compatibility
|
||||
|
||||
// Fix awaitingBackground: subtract files currently being processed by background workers
|
||||
// hashProcessing.size includes both waiting AND running files
|
||||
snapshot.awaitingBackground = Math.max(0, snapshot.awaitingBackground - backgroundQueueRunning);
|
||||
|
||||
(snapshot as any).computed = {
|
||||
// Pre-process queue (validation - quick extension checks)
|
||||
preProcessRunning,
|
||||
@@ -1195,8 +1546,64 @@ export class UnifiedAPIServer {
|
||||
};
|
||||
}
|
||||
|
||||
// Add failed files count from metrics
|
||||
const metrics = performanceMetrics.getMetrics();
|
||||
(snapshot as any).totalFailed = metrics.totalFailedFiles || 0;
|
||||
|
||||
return snapshot;
|
||||
});
|
||||
|
||||
// Get failed files list
|
||||
this.app.get('/api/processing/failed', async (request, reply) => {
|
||||
const metrics = performanceMetrics.getMetrics();
|
||||
return {
|
||||
failedFiles: metrics.failedFiles || [],
|
||||
totalFailed: metrics.totalFailedFiles || 0
|
||||
};
|
||||
});
|
||||
|
||||
// Retry a failed file
|
||||
this.app.post<{ Body: { filePath: string } }>('/api/processing/retry', async (request, reply) => {
|
||||
const { filePath } = request.body;
|
||||
|
||||
if (!filePath) {
|
||||
return reply.status(400).send({ error: 'filePath is required' });
|
||||
}
|
||||
|
||||
try {
|
||||
// Add file back to discovered state for reprocessing
|
||||
this.unifiedStateManager!.addDiscovered(filePath);
|
||||
|
||||
// Trigger scan callback if available to pick up the file
|
||||
if (this.triggerScanCallback) {
|
||||
// Don't await - just trigger async
|
||||
this.triggerScanCallback().catch(() => {});
|
||||
}
|
||||
|
||||
return { status: 'ok', message: `File ${filePath} queued for retry` };
|
||||
} catch (error: any) {
|
||||
return reply.status(500).send({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Retry all failed files
|
||||
this.app.post('/api/processing/retry-all', async (request, reply) => {
|
||||
const metrics = performanceMetrics.getMetrics();
|
||||
const failedFiles = metrics.failedFiles || [];
|
||||
|
||||
let queued = 0;
|
||||
for (const file of failedFiles) {
|
||||
this.unifiedStateManager!.addDiscovered(file.filePath);
|
||||
queued++;
|
||||
}
|
||||
|
||||
// Trigger scan callback if available
|
||||
if (this.triggerScanCallback) {
|
||||
this.triggerScanCallback().catch(() => {});
|
||||
}
|
||||
|
||||
return { status: 'ok', message: `${queued} files queued for retry` };
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -577,6 +577,43 @@ export class ContainerPluginScheduler extends EventEmitter {
|
||||
/**
|
||||
* Pause queues
|
||||
*/
|
||||
/**
|
||||
* Get currently running tasks (status === 'dispatched')
|
||||
* Returns array of { taskId, pluginId, fileHash, filePath, queue, startTime }
|
||||
*/
|
||||
getRunningTasks(): Array<{
|
||||
taskId: string;
|
||||
pluginId: string;
|
||||
fileHash: string;
|
||||
filePath: string;
|
||||
queue: TaskQueueType;
|
||||
startTime?: number;
|
||||
}> {
|
||||
const running: Array<{
|
||||
taskId: string;
|
||||
pluginId: string;
|
||||
fileHash: string;
|
||||
filePath: string;
|
||||
queue: TaskQueueType;
|
||||
startTime?: number;
|
||||
}> = [];
|
||||
|
||||
for (const [taskId, task] of this.pendingTasks) {
|
||||
if (task.status === 'dispatched') {
|
||||
running.push({
|
||||
taskId,
|
||||
pluginId: task.pluginId,
|
||||
fileHash: task.fileHash,
|
||||
filePath: task.filePath,
|
||||
queue: task.queue,
|
||||
startTime: task.dispatchedAt,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return running;
|
||||
}
|
||||
|
||||
pause(): void {
|
||||
this.fastQueue.pause();
|
||||
this.backgroundQueue.pause();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* UnifiedProcessingStateManager
|
||||
*
|
||||
* Tracks files through the complete processing pipeline with 4 states:
|
||||
* 1. Pending: Files discovered in filesystem (not yet processed)
|
||||
* 1. Discovered: Files discovered in filesystem (not yet processed)
|
||||
* 2. Light Processing: Quick metadata extraction + midhash256 computation (~20ms)
|
||||
* 3. Hash Processing: SHA-256 computation for compatibility (slow, background)
|
||||
* 4. Done: Fully processed files
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
import { performanceMetrics } from '../metrics/PerformanceMetrics.js';
|
||||
|
||||
export type FileState = 'pending' | 'lightProcessing' | 'hashProcessing' | 'done';
|
||||
export type FileState = 'discovered' | 'lightProcessing' | 'hashProcessing' | 'done';
|
||||
|
||||
export interface UnifiedFileState {
|
||||
filePath: string;
|
||||
@@ -42,24 +42,29 @@ export interface UnifiedFileState {
|
||||
}
|
||||
|
||||
export interface UnifiedProcessingSnapshot {
|
||||
pending: UnifiedFileState[];
|
||||
discovered: UnifiedFileState[];
|
||||
lightProcessing: UnifiedFileState[];
|
||||
hashProcessing: UnifiedFileState[];
|
||||
done: UnifiedFileState[];
|
||||
|
||||
totalPending: number;
|
||||
totalDiscovered: number;
|
||||
totalLightProcessing: number;
|
||||
totalHashProcessing: number;
|
||||
totalDone: number;
|
||||
totalFilesInVFS: number; // Total number of files accessible in the Virtual File System
|
||||
|
||||
/** Files that completed validation but are waiting for fast queue */
|
||||
awaitingFastQueue: number;
|
||||
/** Files that completed fast queue but are waiting for background queue */
|
||||
awaitingBackground: number;
|
||||
|
||||
// Configuration
|
||||
fastQueueConcurrency?: number; // Number of fast queue workers
|
||||
backgroundQueueConcurrency?: number; // Number of background queue workers
|
||||
}
|
||||
|
||||
export class UnifiedProcessingStateManager {
|
||||
private pending: Map<string, UnifiedFileState> = new Map();
|
||||
private discovered: Map<string, UnifiedFileState> = new Map();
|
||||
private lightProcessing: Map<string, UnifiedFileState> = new Map();
|
||||
private hashProcessing: Map<string, UnifiedFileState> = new Map();
|
||||
private done: UnifiedFileState[] = [];
|
||||
@@ -67,28 +72,35 @@ export class UnifiedProcessingStateManager {
|
||||
private totalProcessedCount = 0; // Track total number of completed files (not limited)
|
||||
|
||||
/**
|
||||
* Add a file to pending state (file discovered)
|
||||
* Add a file to discovered state (file discovered)
|
||||
*/
|
||||
addPending(filePath: string): void {
|
||||
this.pending.set(filePath, {
|
||||
addDiscovered(filePath: string): void {
|
||||
this.discovered.set(filePath, {
|
||||
filePath,
|
||||
state: 'pending',
|
||||
state: 'discovered',
|
||||
discoveredAt: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Move file from pending to light processing
|
||||
* @deprecated Use addDiscovered instead
|
||||
*/
|
||||
addPending(filePath: string): void {
|
||||
this.addDiscovered(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move file from discovered to light processing
|
||||
*/
|
||||
startLightProcessing(filePath: string): void {
|
||||
const pendingState = this.pending.get(filePath);
|
||||
const discoveredState = this.discovered.get(filePath);
|
||||
const existingState = this.lightProcessing.get(filePath);
|
||||
this.pending.delete(filePath);
|
||||
this.discovered.delete(filePath);
|
||||
|
||||
this.lightProcessing.set(filePath, {
|
||||
filePath,
|
||||
state: 'lightProcessing',
|
||||
discoveredAt: pendingState?.discoveredAt || existingState?.discoveredAt,
|
||||
discoveredAt: discoveredState?.discoveredAt || existingState?.discoveredAt,
|
||||
lightProcessingStartedAt: Date.now(),
|
||||
retryCount: existingState?.retryCount || 0
|
||||
});
|
||||
@@ -204,7 +216,7 @@ export class UnifiedProcessingStateManager {
|
||||
* Remove a file from all states (e.g., when deleted)
|
||||
*/
|
||||
removeFile(filePath: string): void {
|
||||
this.pending.delete(filePath);
|
||||
this.discovered.delete(filePath);
|
||||
this.lightProcessing.delete(filePath);
|
||||
this.hashProcessing.delete(filePath);
|
||||
this.done = this.done.filter(f => f.filePath !== filePath);
|
||||
@@ -221,10 +233,10 @@ export class UnifiedProcessingStateManager {
|
||||
const MAX_ITEMS_PER_STATE = 100;
|
||||
|
||||
// Efficiently extract only the needed items without creating full arrays
|
||||
const getPendingSample = (): UnifiedFileState[] => {
|
||||
const getDiscoveredSample = (): UnifiedFileState[] => {
|
||||
const result: UnifiedFileState[] = [];
|
||||
let count = 0;
|
||||
for (const item of this.pending.values()) {
|
||||
for (const item of this.discovered.values()) {
|
||||
if (count >= MAX_ITEMS_PER_STATE) break;
|
||||
result.push(item);
|
||||
count++;
|
||||
@@ -255,17 +267,22 @@ export class UnifiedProcessingStateManager {
|
||||
};
|
||||
|
||||
return {
|
||||
pending: getPendingSample(),
|
||||
discovered: getDiscoveredSample(),
|
||||
lightProcessing: getLightProcessingSample(),
|
||||
hashProcessing: getHashProcessingSample(),
|
||||
done: this.done.slice(0, 50), // Return only last 50 for API
|
||||
|
||||
totalPending: this.pending.size,
|
||||
totalDiscovered: this.discovered.size,
|
||||
totalLightProcessing: this.lightProcessing.size,
|
||||
totalHashProcessing: this.hashProcessing.size,
|
||||
totalDone: this.totalProcessedCount, // Total count of all processed files (not limited to history)
|
||||
totalFilesInVFS: vfsFileCount, // Total number of files accessible in the Virtual File System
|
||||
|
||||
// Files that completed validation but are waiting for fast queue (set by API layer from queue status)
|
||||
awaitingFastQueue: 0,
|
||||
// Files that completed fast queue but are waiting for/in background queue
|
||||
awaitingBackground: this.hashProcessing.size,
|
||||
|
||||
fastQueueConcurrency,
|
||||
backgroundQueueConcurrency
|
||||
};
|
||||
@@ -275,7 +292,7 @@ export class UnifiedProcessingStateManager {
|
||||
* Get the state of a file in any processing queue
|
||||
*/
|
||||
getFileState(filePath: string): UnifiedFileState | undefined {
|
||||
return this.pending.get(filePath) ||
|
||||
return this.discovered.get(filePath) ||
|
||||
this.lightProcessing.get(filePath) ||
|
||||
this.hashProcessing.get(filePath);
|
||||
}
|
||||
@@ -284,7 +301,7 @@ export class UnifiedProcessingStateManager {
|
||||
* Clear all states (useful for testing or reset)
|
||||
*/
|
||||
clear(): void {
|
||||
this.pending.clear();
|
||||
this.discovered.clear();
|
||||
this.lightProcessing.clear();
|
||||
this.hashProcessing.clear();
|
||||
this.done = [];
|
||||
@@ -295,11 +312,11 @@ export class UnifiedProcessingStateManager {
|
||||
* Get the total number of files in all states
|
||||
*/
|
||||
getTotalFileCount(): number {
|
||||
return this.pending.size + this.lightProcessing.size + this.hashProcessing.size + this.done.length;
|
||||
return this.discovered.size + this.lightProcessing.size + this.hashProcessing.size + this.done.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retry processing for a file (move back to pending with incremented retry count)
|
||||
* Retry processing for a file (move back to discovered with incremented retry count)
|
||||
*/
|
||||
retryProcessing(filePath: string): boolean {
|
||||
const lightState = this.lightProcessing.get(filePath);
|
||||
@@ -315,9 +332,9 @@ export class UnifiedProcessingStateManager {
|
||||
this.lightProcessing.delete(filePath);
|
||||
this.hashProcessing.delete(filePath);
|
||||
|
||||
this.pending.set(filePath, {
|
||||
this.discovered.set(filePath, {
|
||||
filePath,
|
||||
state: 'pending',
|
||||
state: 'discovered',
|
||||
discoveredAt: state.discoveredAt,
|
||||
retryCount,
|
||||
lastRetryAt: Date.now()
|
||||
@@ -332,8 +349,16 @@ export class UnifiedProcessingStateManager {
|
||||
getRetryCount(filePath: string): number {
|
||||
const lightState = this.lightProcessing.get(filePath);
|
||||
const hashState = this.hashProcessing.get(filePath);
|
||||
const pendingState = this.pending.get(filePath);
|
||||
return lightState?.retryCount || hashState?.retryCount || pendingState?.retryCount || 0;
|
||||
const discoveredState = this.discovered.get(filePath);
|
||||
return lightState?.retryCount || hashState?.retryCount || discoveredState?.retryCount || 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all files currently discovered (not yet processing)
|
||||
* Returns the internal Map for direct iteration
|
||||
*/
|
||||
getDiscoveredFiles(): Map<string, UnifiedFileState> {
|
||||
return this.discovered;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -285,9 +285,16 @@ export class WatchedFileProcessor implements FileProcessorInterface {
|
||||
}
|
||||
}
|
||||
|
||||
markDiscovered(filePath: string): void {
|
||||
// STATE 1: Mark as discovered when file is found (before queue)
|
||||
this.unifiedStateManager.addDiscovered(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use markDiscovered instead
|
||||
*/
|
||||
markPending(filePath: string): void {
|
||||
// STATE 1: Mark as pending when file is discovered (before queue)
|
||||
this.unifiedStateManager.addPending(filePath);
|
||||
this.markDiscovered(filePath);
|
||||
}
|
||||
|
||||
async queueFile(filePath: string): Promise<void> {
|
||||
|
||||
@@ -157,6 +157,9 @@ export class StreamingPipeline {
|
||||
for await (const filePath of discoveryStream) {
|
||||
this.discoveredCount++;
|
||||
|
||||
// Mark as discovered immediately when file is found
|
||||
this.config.stateManager.addDiscovered(filePath);
|
||||
|
||||
// Stage 1: Validation (fire and forget)
|
||||
this.validationQueue.add(() => this.validateFile(filePath))
|
||||
.catch(err => console.error(`[Pipeline] Validation error for ${filePath}:`, err.message));
|
||||
@@ -181,14 +184,16 @@ export class StreamingPipeline {
|
||||
* Stage 1: Validation
|
||||
* - Quick extension check (no file I/O)
|
||||
* - Optional MIME validation (reads file header)
|
||||
* - Mark file as pending
|
||||
* - Files already marked as discovered in start()
|
||||
* - After validation passes, file is removed from discovered (tracked by PQueue pending)
|
||||
*/
|
||||
private async validateFile(filePath: string): Promise<void> {
|
||||
try {
|
||||
// Extension check (instant, no I/O)
|
||||
const ext = path.extname(filePath).toLowerCase();
|
||||
if (!this.config.supportedExtensions.has(ext)) {
|
||||
// Skip unsupported file types
|
||||
// Skip unsupported file types - remove from discovered
|
||||
this.config.stateManager.removeFile(filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -196,15 +201,19 @@ export class StreamingPipeline {
|
||||
// Note: Skipped for performance - extension check is sufficient
|
||||
// If needed, could add MIME validation via external library
|
||||
|
||||
// Mark as pending
|
||||
this.config.stateManager.addPending(filePath);
|
||||
this.validatedCount++;
|
||||
|
||||
// Remove from discovered - file will be tracked by PQueue pending
|
||||
// until a fast queue worker picks it up and calls startLightProcessing()
|
||||
this.config.stateManager.removeFile(filePath);
|
||||
|
||||
// Stage 2: Fast Queue - metadata + midhash256 (fire and forget)
|
||||
this.fastQueue.add(() => this.processLightPhase(filePath))
|
||||
.catch(err => console.error(`[Pipeline] Fast queue error for ${filePath}:`, err.message));
|
||||
} catch (error: any) {
|
||||
console.error(`[Pipeline] Validation failed for ${filePath}:`, error.message);
|
||||
// Remove failed file from discovered
|
||||
this.config.stateManager.removeFile(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -574,6 +574,24 @@ export class TaskScheduler extends EventEmitter {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get currently running tasks with plugin info
|
||||
* Delegates to container plugin scheduler
|
||||
*/
|
||||
getRunningTasks(): Array<{
|
||||
taskId: string;
|
||||
pluginId: string;
|
||||
fileHash: string;
|
||||
filePath: string;
|
||||
queue: TaskQueueType;
|
||||
startTime?: number;
|
||||
}> {
|
||||
if (this.containerPluginScheduler) {
|
||||
return this.containerPluginScheduler.getRunningTasks();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Shutdown
|
||||
// =========================================================================
|
||||
|
||||
Reference in New Issue
Block a user