Skip to content

Commit

Permalink
feat: add job ids
Browse files Browse the repository at this point in the history
  • Loading branch information
etnoy committed Feb 7, 2025
1 parent c5360e7 commit 1dfc0ea
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
1 change: 0 additions & 1 deletion server/src/interfaces/job.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ export interface IAssetDeleteJob extends IEntityJob {
}

export interface ILibraryFileJob extends IEntityJob {
ownerId: string;
assetPath: string;
}

Expand Down
25 changes: 22 additions & 3 deletions server/src/repositories/job.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,37 @@ export class JobRepository implements IJobRepository {

private getJobOptions(item: JobItem): JobsOptions | null {
switch (item.name) {
case JobName.LIBRARY_QUEUE_SYNC_ASSETS:
case JobName.LIBRARY_QUEUE_SYNC_FILES:
case JobName.LIBRARY_SYNC_ASSET:
case JobName.LIBRARY_DELETE:
case JobName.SIDECAR_SYNC:
case JobName.SIDECAR_DISCOVERY:
case JobName.GENERATE_THUMBNAILS:
case JobName.METADATA_EXTRACTION:
case JobName.STORAGE_TEMPLATE_MIGRATION_SINGLE: {
return { jobId: `${item.data.id}-${item.name}` };
}
case JobName.SIDECAR_DISCOVERY: {
return { jobId: `${item.data.id}-${item.data.source}` };
}
case JobName.LIBRARY_SYNC_FILE: {
return { jobId: `${item.data.id}-${item.data.assetPath}` };
}
case JobName.NOTIFY_ALBUM_UPDATE: {
return { jobId: item.data.id, delay: item.data?.delay };
}
case JobName.STORAGE_TEMPLATE_MIGRATION_SINGLE: {
return { jobId: item.data.id };
}
case JobName.GENERATE_PERSON_THUMBNAIL: {
return { priority: 1 };
}
case JobName.QUEUE_FACIAL_RECOGNITION: {
return { jobId: JobName.QUEUE_FACIAL_RECOGNITION };
}
case JobName.LIBRARY_QUEUE_SYNC_ALL:
case JobName.LIBRARY_QUEUE_CLEANUP: {
// These jobs are globally unique and should only have one instance running at a time
return { jobId: item.name };
}
default: {
return null;
}
Expand Down
13 changes: 1 addition & 12 deletions server/src/services/library.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ describe(LibraryService.name, () => {
name: JobName.LIBRARY_SYNC_FILE,
data: {
id: libraryStub.externalLibrary1.id,
ownerId: libraryStub.externalLibrary1.owner.id,
assetPath: '/data/user1/photo.jpg',
},
},
Expand Down Expand Up @@ -401,7 +400,6 @@ describe(LibraryService.name, () => {
it('should import a new asset', async () => {
const mockLibraryJob: ILibraryFileJob = {
id: libraryStub.externalLibrary1.id,
ownerId: mockUser.id,
assetPath: '/data/user1/photo.jpg',
};

Expand Down Expand Up @@ -445,7 +443,6 @@ describe(LibraryService.name, () => {
it('should import a new video', async () => {
const mockLibraryJob: ILibraryFileJob = {
id: libraryStub.externalLibrary1.id,
ownerId: mockUser.id,
assetPath: '/data/user1/video.mp4',
};

Expand Down Expand Up @@ -489,7 +486,6 @@ describe(LibraryService.name, () => {
it('should not import an asset to a soft deleted library', async () => {
const mockLibraryJob: ILibraryFileJob = {
id: libraryStub.externalLibrary1.id,
ownerId: mockUser.id,
assetPath: '/data/user1/photo.jpg',
};

Expand All @@ -504,7 +500,6 @@ describe(LibraryService.name, () => {
it('should not refresh a file whose mtime matches existing asset', async () => {
const mockLibraryJob: ILibraryFileJob = {
id: libraryStub.externalLibrary1.id,
ownerId: mockUser.id,
assetPath: assetStub.hasFileExtension.originalPath,
};

Expand All @@ -522,10 +517,9 @@ describe(LibraryService.name, () => {
expect(jobMock.queueAll).not.toHaveBeenCalled();
});

it('should skip existing asset', async () => {
it('should skip an existing asset', async () => {
const mockLibraryJob: ILibraryFileJob = {
id: libraryStub.externalLibrary1.id,
ownerId: mockUser.id,
assetPath: '/data/user1/photo.jpg',
};

Expand All @@ -537,7 +531,6 @@ describe(LibraryService.name, () => {
it('should not refresh an asset trashed by user', async () => {
const mockLibraryJob: ILibraryFileJob = {
id: libraryStub.externalLibrary1.id,
ownerId: mockUser.id,
assetPath: assetStub.hasFileExtension.originalPath,
};

Expand All @@ -554,7 +547,6 @@ describe(LibraryService.name, () => {

const mockLibraryJob: ILibraryFileJob = {
id: libraryStub.externalLibrary1.id,
ownerId: userStub.admin.id,
assetPath: '/data/user1/photo.jpg',
};

Expand All @@ -572,7 +564,6 @@ describe(LibraryService.name, () => {

const mockLibraryJob: ILibraryFileJob = {
id: libraryStub.externalLibrary1.id,
ownerId: userStub.admin.id,
assetPath: '/data/user1/photo.jpg',
};

Expand Down Expand Up @@ -923,7 +914,6 @@ describe(LibraryService.name, () => {
data: {
id: libraryStub.externalLibraryWithImportPaths1.id,
assetPath: '/foo/photo.jpg',
ownerId: libraryStub.externalLibraryWithImportPaths1.owner.id,
},
},
]);
Expand All @@ -948,7 +938,6 @@ describe(LibraryService.name, () => {
data: {
id: libraryStub.externalLibraryWithImportPaths1.id,
assetPath: '/foo/photo.jpg',
ownerId: libraryStub.externalLibraryWithImportPaths1.owner.id,
},
},
]);
Expand Down
9 changes: 6 additions & 3 deletions server/src/services/library.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,13 @@ export class LibraryService extends BaseService {
return mapLibrary(library);
}

private async syncFiles({ id, ownerId }: LibraryEntity, assetPaths: string[]) {
private async syncFiles({ id }: LibraryEntity, assetPaths: string[]) {
await this.jobRepository.queueAll(
assetPaths.map((assetPath) => ({
name: JobName.LIBRARY_SYNC_FILE,
data: {
id,
assetPath,
ownerId,
},
})),
);
Expand Down Expand Up @@ -401,7 +400,7 @@ export class LibraryService extends BaseService {
const mtime = stat.mtime;

asset = await this.assetRepository.create({
ownerId: job.ownerId,
ownerId: library.ownerId,
libraryId: job.id,
checksum: pathHash,
originalPath: assetPath,
Expand Down Expand Up @@ -433,12 +432,16 @@ export class LibraryService extends BaseService {
async queueScan(id: string) {
await this.findOrFail(id);

// We purge any existing scan jobs for this library
await this.jobRepository.removeJob(id, JobName.LIBRARY_QUEUE_SYNC_FILES);
await this.jobRepository.queue({
name: JobName.LIBRARY_QUEUE_SYNC_FILES,
data: {
id,
},
});

await this.jobRepository.removeJob(id, JobName.LIBRARY_QUEUE_SYNC_ASSETS);
await this.jobRepository.queue({ name: JobName.LIBRARY_QUEUE_SYNC_ASSETS, data: { id } });
}

Expand Down

0 comments on commit 1dfc0ea

Please sign in to comment.