1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-23 01:27:14 +02:00

Improving graceful degradation during indexing #704

This commit is contained in:
Patrik J. Braun 2023-09-02 14:17:35 +02:00
parent 930ea4f77a
commit 8a6b142229
3 changed files with 364 additions and 329 deletions

View File

@ -11,6 +11,8 @@ import {ParentDirectoryDTO} from '../../../../common/entities/DirectoryDTO';
import {Logger} from '../../../Logger'; import {Logger} from '../../../Logger';
import {FileDTO} from '../../../../common/entities/FileDTO'; import {FileDTO} from '../../../../common/entities/FileDTO';
const LOG_TAG = '[IndexingJob]';
export class IndexingJob< export class IndexingJob<
S extends { indexChangesOnly: boolean } = { indexChangesOnly: boolean } S extends { indexChangesOnly: boolean } = { indexChangesOnly: boolean }
> extends Job<S> { > extends Job<S> {
@ -48,9 +50,18 @@ export class IndexingJob<
let scanned: ParentDirectoryDTO<FileDTO>; let scanned: ParentDirectoryDTO<FileDTO>;
let dirChanged = true; let dirChanged = true;
try {
const absDirPath = path.join(ProjectPath.ImageFolder, directory);
if (!fs.existsSync(absDirPath)) {
this.Progress.log('Skipping. Directory does not exist: ' + directory);
this.Progress.Skipped++;
} else { // dir should exist now
// check if the folder got modified if only changes need to be indexed // check if the folder got modified if only changes need to be indexed
if (this.config.indexChangesOnly) { if (this.config.indexChangesOnly) {
const stat = fs.statSync(path.join(ProjectPath.ImageFolder, directory));
const stat = fs.statSync(absDirPath);
const lastModified = DiskMangerWorker.calcLastModified(stat); const lastModified = DiskMangerWorker.calcLastModified(stat);
scanned = await ObjectManagers.getInstance().GalleryManager.selectDirStructure(directory); scanned = await ObjectManagers.getInstance().GalleryManager.selectDirStructure(directory);
// If not modified and it was scanned before, dir is up-to-date // If not modified and it was scanned before, dir is up-to-date
@ -63,6 +74,7 @@ export class IndexingJob<
} }
} }
// reindex // reindex
if (dirChanged || !this.config.indexChangesOnly) { if (dirChanged || !this.config.indexChangesOnly) {
this.Progress.log('Indexing: ' + directory); this.Progress.log('Indexing: ' + directory);
@ -72,9 +84,16 @@ export class IndexingJob<
directory directory
); );
} else { } else {
this.Progress.log('Skipped: ' + directory); this.Progress.log('Skipping. No change for: ' + directory);
this.Progress.Skipped++; this.Progress.Skipped++;
Logger.silly('Skipping reindexing, no change for: ' + directory); Logger.silly(LOG_TAG, 'Skipping reindexing, no change for: ' + directory);
}
}
} catch (e) {
this.Progress.log('Skipping. Indexing failed for: ' + directory);
this.Progress.Skipped++;
Logger.warn(LOG_TAG, 'Skipping. Indexing failed for: ' + directory);
console.error(e);
} }
if (this.Progress.State !== JobProgressStates.running) { if (this.Progress.State !== JobProgressStates.running) {
return false; return false;

View File

@ -153,6 +153,7 @@ export abstract class Job<T extends Record<string, any> = Record<string, any>> i
await new Promise(setImmediate); await new Promise(setImmediate);
this.run(); this.run();
} catch (e) { } catch (e) {
Logger.error(LOG_TAG, 'Job failed with:');
Logger.error(LOG_TAG, e); Logger.error(LOG_TAG, e);
this.Progress.log('Failed with: ' + (typeof e.toString === 'function') ? e.toString() : JSON.stringify(e)); this.Progress.log('Failed with: ' + (typeof e.toString === 'function') ? e.toString() : JSON.stringify(e));
this.Progress.State = JobProgressStates.failed; this.Progress.State = JobProgressStates.failed;

View File

@ -92,21 +92,30 @@ export class MetadataLoader {
}); });
} }
private static readonly EMPTY_METADATA: PhotoMetadata = {
size: {width: 1, height: 1},
creationDate: 0,
fileSize: 0,
};
public static loadPhotoMetadata(fullPath: string): Promise<PhotoMetadata> { public static loadPhotoMetadata(fullPath: string): Promise<PhotoMetadata> {
return new Promise<PhotoMetadata>((resolve, reject) => { return new Promise<PhotoMetadata>((resolve, reject) => {
try {
const fd = fs.openSync(fullPath, 'r'); const fd = fs.openSync(fullPath, 'r');
const data = Buffer.allocUnsafe(Config.Media.photoMetadataSize); const data = Buffer.allocUnsafe(Config.Media.photoMetadataSize);
fs.read(fd, data, 0, Config.Media.photoMetadataSize, 0, (err) => { fs.read(fd, data, 0, Config.Media.photoMetadataSize, 0, (err) => {
fs.closeSync(fd); fs.closeSync(fd);
if (err) {
return reject({file: fullPath, error: err});
}
const metadata: PhotoMetadata = { const metadata: PhotoMetadata = {
size: {width: 1, height: 1}, size: {width: 1, height: 1},
creationDate: 0, creationDate: 0,
fileSize: 0, fileSize: 0,
}; };
if (err) {
Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath);
console.error(err);
return resolve(MetadataLoader.EMPTY_METADATA);
}
try { try {
try { try {
const stat = fs.statSync(fullPath); const stat = fs.statSync(fullPath);
@ -420,6 +429,12 @@ export class MetadataLoader {
return reject({file: fullPath, error: err}); return reject({file: fullPath, error: err});
} }
}); });
} catch (err) {
Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath);
console.error(err);
return resolve(MetadataLoader.EMPTY_METADATA);
}
}); });
} }
} }