mirror of
https://github.com/bpatrik/pigallery2.git
synced 2025-01-10 04:07:35 +02:00
Improving graceful degradation during indexing #704
This commit is contained in:
parent
930ea4f77a
commit
8a6b142229
@ -11,6 +11,8 @@ import {ParentDirectoryDTO} from '../../../../common/entities/DirectoryDTO';
|
||||
import {Logger} from '../../../Logger';
|
||||
import {FileDTO} from '../../../../common/entities/FileDTO';
|
||||
|
||||
const LOG_TAG = '[IndexingJob]';
|
||||
|
||||
export class IndexingJob<
|
||||
S extends { indexChangesOnly: boolean } = { indexChangesOnly: boolean }
|
||||
> extends Job<S> {
|
||||
@ -48,9 +50,18 @@ export class IndexingJob<
|
||||
let scanned: ParentDirectoryDTO<FileDTO>;
|
||||
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
|
||||
if (this.config.indexChangesOnly) {
|
||||
const stat = fs.statSync(path.join(ProjectPath.ImageFolder, directory));
|
||||
|
||||
const stat = fs.statSync(absDirPath);
|
||||
const lastModified = DiskMangerWorker.calcLastModified(stat);
|
||||
scanned = await ObjectManagers.getInstance().GalleryManager.selectDirStructure(directory);
|
||||
// If not modified and it was scanned before, dir is up-to-date
|
||||
@ -63,6 +74,7 @@ export class IndexingJob<
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// reindex
|
||||
if (dirChanged || !this.config.indexChangesOnly) {
|
||||
this.Progress.log('Indexing: ' + directory);
|
||||
@ -72,9 +84,16 @@ export class IndexingJob<
|
||||
directory
|
||||
);
|
||||
} else {
|
||||
this.Progress.log('Skipped: ' + directory);
|
||||
this.Progress.log('Skipping. No change for: ' + directory);
|
||||
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) {
|
||||
return false;
|
||||
|
@ -153,6 +153,7 @@ export abstract class Job<T extends Record<string, any> = Record<string, any>> i
|
||||
await new Promise(setImmediate);
|
||||
this.run();
|
||||
} catch (e) {
|
||||
Logger.error(LOG_TAG, 'Job failed with:');
|
||||
Logger.error(LOG_TAG, e);
|
||||
this.Progress.log('Failed with: ' + (typeof e.toString === 'function') ? e.toString() : JSON.stringify(e));
|
||||
this.Progress.State = JobProgressStates.failed;
|
||||
|
@ -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> {
|
||||
return new Promise<PhotoMetadata>((resolve, reject) => {
|
||||
try {
|
||||
const fd = fs.openSync(fullPath, 'r');
|
||||
|
||||
const data = Buffer.allocUnsafe(Config.Media.photoMetadataSize);
|
||||
fs.read(fd, data, 0, Config.Media.photoMetadataSize, 0, (err) => {
|
||||
fs.closeSync(fd);
|
||||
if (err) {
|
||||
return reject({file: fullPath, error: err});
|
||||
}
|
||||
const metadata: PhotoMetadata = {
|
||||
size: {width: 1, height: 1},
|
||||
creationDate: 0,
|
||||
fileSize: 0,
|
||||
};
|
||||
if (err) {
|
||||
Logger.error(LOG_TAG, 'Error during reading photo: ' + fullPath);
|
||||
console.error(err);
|
||||
return resolve(MetadataLoader.EMPTY_METADATA);
|
||||
}
|
||||
try {
|
||||
try {
|
||||
const stat = fs.statSync(fullPath);
|
||||
@ -420,6 +429,12 @@ export class MetadataLoader {
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user