Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 58 additions & 5 deletions src/lib/integrations/sabnzbd.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import axios, { AxiosInstance } from 'axios';
import fs from 'fs';
import { RMAB_USER_AGENT } from '@/lib/utils/user-agent';
import https from 'https';
import FormData from 'form-data';
Expand Down Expand Up @@ -611,8 +612,38 @@ export class SABnzbdService implements IDownloadClient {
return this.mapQueueItemToNZBInfo(queueItem);
}

// Not in queue, check history
const history = await this.getHistory(100);
// Direct NZO ID history query (bypasses 100-item history limit)
try {
const response = await this.client.get('/api', {
params: {
mode: 'history',
nzo_ids: nzbId,
output: 'json',
apikey: this.apiKey,
},
});

const slots = response?.data?.history?.slots || [];
if (slots.length > 0) {
const historyItem: HistoryItem = {
nzbId: slots[0].nzo_id,
name: slots[0].name,
category: slots[0].category || '',
status: slots[0].status,
bytes: slots[0].bytes || '0',
failMessage: slots[0].fail_message || '',
storage: slots[0].storage || '',
completedTimestamp: slots[0].completed || '0',
downloadTime: slots[0].download_time || '0',
};
return this.mapHistoryItemToNZBInfo(historyItem);
}
} catch {
// Fall through to deep history query
}

// Deep history fallback search (500 limit)
const history = await this.getHistory(500);
const historyItem = history.find(item => item.nzbId === nzbId);

if (historyItem) {
Expand Down Expand Up @@ -662,7 +693,7 @@ export class SABnzbdService implements IDownloadClient {
},
});

const slots = response.data?.history?.slots || [];
const slots = response?.data?.history?.slots || [];
return slots.map((slot: any) => ({
nzbId: slot.nzo_id,
name: slot.name,
Expand Down Expand Up @@ -955,8 +986,30 @@ export class SABnzbdService implements IDownloadClient {
* Map history item to NZBInfo
*/
private mapHistoryItemToNZBInfo(historyItem: HistoryItem): NZBInfo {
const isCompleted = historyItem.status.toLowerCase().includes('completed');
const isFailed = historyItem.status.toLowerCase().includes('failed');
const rawStatus = (historyItem.status || '').toLowerCase();
const failMsg = (historyItem.failMessage || '').toLowerCase();
const isDuplicate = failMsg.includes('duplicate') || rawStatus.includes('duplicate');

let isCompleted = rawStatus.includes('completed');
let isFailed = rawStatus.includes('failed');

// If SABnzbd marked it as Duplicate NZB, check if storage path actually exists on disk and contains files
if (isDuplicate && historyItem.storage) {
try {
if (fs.existsSync(historyItem.storage)) {
const files = fs.readdirSync(historyItem.storage);
if (files.length > 0) {
isCompleted = true;
isFailed = false;
logger.info(`SABnzbd slot "${historyItem.name}" marked as Duplicate NZB, but target storage exists with ${files.length} file(s). Mapping to completed.`, {
storage: historyItem.storage,
});
}
}
} catch {
// Fall back to original status if disk check throws
}
}

return {
nzbId: historyItem.nzbId,
Expand Down
69 changes: 41 additions & 28 deletions src/lib/processors/monitor-download.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,29 @@ export async function processMonitorDownload(payload: MonitorDownloadPayload): P

const errorMessage = `Download failed in ${client.clientType}`;
const clientErrorDetail = info.errorMessage ?? null;

// Update request to failed
await prisma.request.update({
where: { id: requestId },
data: {
status: 'failed',
errorMessage,
updatedAt: new Date(),
},
});
const isDuplicateFail = (clientErrorDetail || '').toLowerCase().includes('duplicate');

if (isDuplicateFail) {
logger.info(`Download flagged as Duplicate in ${client.clientType} for request ${requestId}. Resetting status to 'awaiting_search' to allow alternate release search.`);
await prisma.request.update({
where: { id: requestId },
data: {
status: 'awaiting_search',
errorMessage: 'Duplicate download flagged by client - reset for search retry',
updatedAt: new Date(),
},
});
} else {
// Update request to failed
await prisma.request.update({
where: { id: requestId },
data: {
status: 'failed',
errorMessage,
updatedAt: new Date(),
},
});
}

// Update download history
await prisma.downloadHistory.update({
Expand All @@ -243,25 +256,25 @@ export async function processMonitorDownload(payload: MonitorDownloadPayload): P
},
});

// Auto-block this release. The client itself reported `failed`, so this
// is a real release problem — distinct from the connection-exhausted
// path below at line ~317 which we deliberately do NOT block on.
const failedDownload = await prisma.downloadHistory.findUnique({
where: { id: downloadHistoryId },
});
if (failedDownload?.torrentName) {
await addAutoBlock({
requestId,
releaseName: failedDownload.torrentName,
releaseHash: failedDownload.torrentHash ?? failedDownload.nzbId ?? null,
indexerName: failedDownload.indexerName ?? null,
indexerId: failedDownload.indexerId ?? null,
source: 'download_fail',
reason: classifyDownloadFailure(clientErrorDetail),
reasonDetail: clientErrorDetail,
downloadHistoryId: failedDownload.id,
jobId,
// Auto-block this release ONLY if it was NOT a duplicate detection failure
if (!isDuplicateFail) {
const failedDownload = await prisma.downloadHistory.findUnique({
where: { id: downloadHistoryId },
});
if (failedDownload?.torrentName) {
await addAutoBlock({
requestId,
releaseName: failedDownload.torrentName,
releaseHash: failedDownload.torrentHash ?? failedDownload.nzbId ?? null,
indexerName: failedDownload.indexerName ?? null,
indexerId: failedDownload.indexerId ?? null,
source: 'download_fail',
reason: classifyDownloadFailure(clientErrorDetail),
reasonDetail: clientErrorDetail,
downloadHistoryId: failedDownload.id,
jobId,
});
}
}

// Send notification for request failure
Expand Down
92 changes: 92 additions & 0 deletions src/lib/processors/scan-orphaned-downloads.processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Component: Orphaned Completed Download Scanner Processor
*
* Periodically inspects completed download storage directories (/storage/downloads/nzb/complete/default/)
* and matches un-imported folders to requests stuck in 'downloading' status,
* automatically enqueuing 'organize_files' jobs to process them.
*/

import fs from 'fs';
import path from 'path';
import { prisma } from '../db';
import { RMABLogger } from '../utils/logger';
import { getJobQueueService } from '../services/job-queue.service';
import { cleanTitle } from '../utils/search-cleaner';

export interface ScanOrphanedDownloadsPayload {
jobId?: string;
downloadDir?: string;
}

export async function processScanOrphanedDownloads(payload: ScanOrphanedDownloadsPayload): Promise<any> {
const { jobId, downloadDir = '/storage/downloads/nzb/complete/default' } = payload;
const logger = RMABLogger.forJob(jobId, 'ScanOrphanedDownloads');

logger.info(`Scanning completed download storage folder: ${downloadDir}`);

if (!fs.existsSync(downloadDir)) {
logger.warn(`Download directory does not exist: ${downloadDir}`);
return { success: true, matched: 0, reason: 'Directory not found' };
}

// Find requests in downloading, awaiting_import, failed, or awaiting_search status
const requests = await prisma.request.findMany({
where: {
status: { in: ['downloading', 'awaiting_import', 'failed', 'awaiting_search'] },
deletedAt: null,
},
include: {
audiobook: true,
},
});

if (requests.length === 0) {
logger.info('No requests currently in downloading or awaiting_import status.');
return { success: true, matched: 0 };
}

const dirsToScan = [
downloadDir,
'/storage/downloads/nzb/download',
].filter(d => fs.existsSync(d));

let matchedCount = 0;
const jobQueue = getJobQueueService();

for (const dir of dirsToScan) {
const entries = fs.readdirSync(dir, { withFileTypes: true });
const folders = entries.filter(e => e.isDirectory()).map(e => e.name);

logger.info(`Found ${folders.length} folders in storage dir "${dir}" and ${requests.length} candidate request(s).`);

for (const request of requests) {
const rawTitle = request.audiobook.title;
const cleaned = cleanTitle(rawTitle).toLowerCase().replace(/[^a-z0-9]/g, '');

const matchingFolder = folders.find(folder => {
const fLower = folder.toLowerCase().replace(/[^a-z0-9]/g, '');
return fLower.includes(cleaned);
});

if (matchingFolder) {
const fullPath = path.join(dir, matchingFolder);
logger.info(`Matched orphaned download folder "${matchingFolder}" to request "${rawTitle}" (${request.id})`);

await jobQueue.addOrganizeJob(
request.id,
request.audiobook.id,
fullPath
);
matchedCount++;
}
}
}

logger.info(`Scan complete: ${matchedCount} orphaned download(s) matched and enqueued for organization.`);

return {
success: true,
matched: matchedCount,
totalRequests: requests.length,
};
}
83 changes: 83 additions & 0 deletions tests/integrations/sabnzbd-duplicate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { SABnzbdService } from '@/lib/integrations/sabnzbd.service';
import fs from 'fs';

describe('SABnzbdService Duplicate NZB Handling', () => {
let service: SABnzbdService;

beforeEach(() => {
vi.clearAllMocks();
service = new SABnzbdService('http://localhost:8080', 'test-key');
});

it('should map Duplicate NZB to completed status if storage path exists on disk with files', async () => {
const mockHistoryItem = {
nzo_id: 'sab-nzo-123',
name: 'The_Expanse_03_Abaddons_Gate',
category: 'readmeabook',
status: 'Failed',
bytes: '5000000',
fail_message: 'Duplicate NZB',
storage: '/storage/downloads/nzb/download/The_Expanse_03_Abaddons_Gate',
completed: '1785450000',
download_time: '10',
};

vi.spyOn(service['client'], 'get').mockImplementation(async (url, config: any) => {
if (config?.params?.mode === 'queue') {
return { data: { queue: { slots: [] } } };
}
return {
data: {
history: {
slots: [mockHistoryItem],
},
},
};
});

vi.spyOn(fs, 'existsSync').mockReturnValue(true);
vi.spyOn(fs, 'readdirSync').mockReturnValue(['audio.m4b'] as any);

const item = await service.getNZB('sab-nzo-123');

expect(item).toBeDefined();
expect(item?.status).toBe('completed');
expect(item?.downloadPath).toBe('/storage/downloads/nzb/download/The_Expanse_03_Abaddons_Gate');
});

it('should preserve failed status if Duplicate NZB has no existing storage files on disk', async () => {
const mockHistoryItem = {
nzo_id: 'sab-nzo-456',
name: 'Non_Existent_Book',
category: 'readmeabook',
status: 'Failed',
bytes: '0',
fail_message: 'Duplicate NZB',
storage: '/storage/downloads/nzb/download/Non_Existent_Book',
completed: '1785450000',
download_time: '0',
};

vi.spyOn(service['client'], 'get').mockImplementation(async (url, config: any) => {
if (config?.params?.mode === 'queue') {
return { data: { queue: { slots: [] } } };
}
return {
data: {
history: {
slots: [mockHistoryItem],
},
},
};
});

vi.spyOn(fs, 'existsSync').mockReturnValue(false);

const item = await service.getNZB('sab-nzo-456');

expect(item).toBeDefined();
expect(item?.status).toBe('failed');
expect(item?.errorMessage).toContain('Duplicate NZB');
});
});