1
0
forked from Cutlery/immich
This commit is contained in:
Jonathan Jogenfors 2024-03-19 22:39:35 +01:00
parent 6f3401343f
commit e6c761894c
2 changed files with 30 additions and 25 deletions

View File

@ -169,13 +169,15 @@ describe(LibraryService.name, () => {
}); });
describe('handleQueueAssetRefresh', () => { describe('handleQueueAssetRefresh', () => {
it('should queue new assets', async () => { it('should queue refresh of a new asset', async () => {
const mockLibraryJob: ILibraryRefreshJob = { const mockLibraryJob: ILibraryRefreshJob = {
id: libraryStub.externalLibrary1.id, id: libraryStub.externalLibrary1.id,
refreshModifiedFiles: false, refreshModifiedFiles: false,
refreshAllFiles: false, refreshAllFiles: false,
}; };
assetMock.getWith.mockResolvedValue({ items: [], hasNextPage: false });
libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1); libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1);
// eslint-disable-next-line @typescript-eslint/require-await // eslint-disable-next-line @typescript-eslint/require-await
storageMock.walk.mockImplementation(async function* generator() { storageMock.walk.mockImplementation(async function* generator() {
@ -205,6 +207,7 @@ describe(LibraryService.name, () => {
refreshAllFiles: true, refreshAllFiles: true,
}; };
assetMock.getWith.mockResolvedValue({ items: [], hasNextPage: false });
libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1); libraryMock.get.mockResolvedValue(libraryStub.externalLibrary1);
// eslint-disable-next-line @typescript-eslint/require-await // eslint-disable-next-line @typescript-eslint/require-await
storageMock.walk.mockImplementation(async function* generator() { storageMock.walk.mockImplementation(async function* generator() {

View File

@ -643,40 +643,40 @@ export class LibraryService extends EventEmitter {
this.assetRepository.getWith(pagination, WithProperty.IS_ONLINE, job.id), this.assetRepository.getWith(pagination, WithProperty.IS_ONLINE, job.id),
); );
let crawledAssetPaths: string[] = [];
let crawlCounter = 0;
let crawlDone = false; let crawlDone = false;
let existingAssetsDone = false; let existingAssetsDone = false;
let crawlCounter = 0;
let existingAssetCounter = 0; let existingAssetCounter = 0;
const scanNextAssetPage = async () => { const checkNextAssetPageForOffline = async () => {
if (!existingAssetsDone) { const existingAssetPage = await existingAssets.next();
const existingAssetPage = await existingAssets.next(); existingAssetsDone = existingAssetPage.done ?? true;
existingAssetsDone = existingAssetPage.done ?? true;
if (existingAssetPage.value) { if (existingAssetPage.value) {
existingAssetCounter += existingAssetPage.value.length; existingAssetCounter += existingAssetPage.value.length;
this.logger.log( this.logger.log(
`Queuing online check of ${existingAssetPage.value.length} asset(s) in library ${library.id}...`, `Queuing online check of ${existingAssetPage.value.length} asset(s) in library ${library.id}...`,
); );
await this.jobRepository.queueAll( await this.jobRepository.queueAll(
existingAssetPage.value.map((asset) => ({ existingAssetPage.value.map((asset) => ({
name: JobName.LIBRARY_CHECK_OFFLINE, name: JobName.LIBRARY_CHECK_OFFLINE,
data: { id: asset.id }, data: { id: asset.id },
})), })),
); );
}
} }
}; };
let crawledAssetPaths: string[] = [];
while (!crawlDone) { while (!crawlDone) {
const crawlResult = await crawledAssets.next(); const crawlResult = await crawledAssets.next();
crawlDone = crawlResult.done ?? true; crawlDone = crawlResult.done ?? true;
crawledAssetPaths.push(crawlResult.value); if (!crawlDone) {
crawlCounter++; crawledAssetPaths.push(crawlResult.value);
crawlCounter++;
}
if (crawledAssetPaths.length % LIBRARY_SCAN_BATCH_SIZE === 0 || crawlDone) { if (crawledAssetPaths.length % LIBRARY_SCAN_BATCH_SIZE === 0 || crawlDone) {
this.logger.log(`Queueing scan of ${crawledAssetPaths.length} asset path(s) in library ${library.id}...`); this.logger.log(`Queueing scan of ${crawledAssetPaths.length} asset path(s) in library ${library.id}...`);
@ -684,14 +684,16 @@ export class LibraryService extends EventEmitter {
await this.scanAssets(job.id, crawledAssetPaths, library.ownerId, job.refreshAllFiles ?? false); await this.scanAssets(job.id, crawledAssetPaths, library.ownerId, job.refreshAllFiles ?? false);
crawledAssetPaths = []; crawledAssetPaths = [];
// Interweave the queuing of offline checks with the asset scanning (if any) if (!existingAssetsDone) {
await scanNextAssetPage(); // Interweave the queuing of offline checks with the asset scanning (if any)
await checkNextAssetPageForOffline();
}
} }
} }
// If there are any remaining assets to check for offline status, do so // If there are any remaining assets to check for offline status, do so
while (!existingAssetsDone) { while (!existingAssetsDone) {
await scanNextAssetPage(); await checkNextAssetPageForOffline();
} }
this.logger.log( this.logger.log(