Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(server): track thumbnail jobs in job status table #11908

Merged
merged 1 commit into from
Aug 19, 2024
Merged
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
6 changes: 6 additions & 0 deletions server/src/entities/asset-job-status.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ export class AssetJobStatusEntity {

@Column({ type: 'timestamptz', nullable: true })
duplicatesDetectedAt!: Date | null;

@Column({ type: 'timestamptz', nullable: true })
previewAt!: Date | null;

@Column({ type: 'timestamptz', nullable: true })
thumbnailAt!: Date | null;
}
17 changes: 17 additions & 0 deletions server/src/migrations/1724080823160-AddThumbnailJobStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddThumbnailJobStatus1724080823160 implements MigrationInterface {
name = 'AddThumbnailJobStatus1724080823160';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "asset_job_status" ADD "previewAt" TIMESTAMP WITH TIME ZONE`);
await queryRunner.query(`ALTER TABLE "asset_job_status" ADD "thumbnailAt" TIMESTAMP WITH TIME ZONE`);
await queryRunner.query(`UPDATE "asset_job_status" SET "previewAt" = NOW() FROM "assets" WHERE "assetId" = "assets"."id" AND "assets"."previewPath" IS NOT NULL`);
await queryRunner.query(`UPDATE "asset_job_status" SET "thumbnailAt" = NOW() FROM "assets" WHERE "assetId" = "assets"."id" AND "assets"."thumbnailPath" IS NOT NULL`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "asset_job_status" DROP COLUMN "thumbnailAt"`);
await queryRunner.query(`ALTER TABLE "asset_job_status" DROP COLUMN "previewAt"`);
}
}
21 changes: 12 additions & 9 deletions server/src/repositories/asset.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,10 @@ export class AssetRepository implements IAssetRepository {

switch (property) {
case WithoutProperty.THUMBNAIL: {
relations = { jobStatus: true };
where = [
{ previewPath: IsNull(), isVisible: true },
{ previewPath: '', isVisible: true },
{ thumbnailPath: IsNull(), isVisible: true },
{ thumbnailPath: '', isVisible: true },
{ jobStatus: { previewAt: IsNull() }, isVisible: true },
{ jobStatus: { thumbnailAt: IsNull() }, isVisible: true },
{ thumbhash: IsNull(), isVisible: true },
];
break;
Expand Down Expand Up @@ -429,7 +428,7 @@ export class AssetRepository implements IAssetRepository {
};
where = {
isVisible: true,
previewPath: Not(IsNull()),
jobStatus: { previewAt: Not(IsNull()) },
smartSearch: {
embedding: IsNull(),
},
Expand All @@ -439,10 +438,10 @@ export class AssetRepository implements IAssetRepository {

case WithoutProperty.DUPLICATE: {
where = {
previewPath: Not(IsNull()),
isVisible: true,
smartSearch: true,
jobStatus: {
previewAt: Not(IsNull()),
duplicatesDetectedAt: IsNull(),
},
};
Expand All @@ -454,7 +453,9 @@ export class AssetRepository implements IAssetRepository {
smartInfo: true,
};
where = {
previewPath: Not(IsNull()),
jobStatus: {
previewAt: Not(IsNull()),
},
isVisible: true,
smartInfo: {
tags: IsNull(),
Expand All @@ -469,13 +470,13 @@ export class AssetRepository implements IAssetRepository {
jobStatus: true,
};
where = {
previewPath: Not(IsNull()),
isVisible: true,
faces: {
assetId: IsNull(),
personId: IsNull(),
},
jobStatus: {
previewAt: Not(IsNull()),
facesRecognizedAt: IsNull(),
},
};
Expand All @@ -487,7 +488,9 @@ export class AssetRepository implements IAssetRepository {
faces: true,
};
where = {
previewPath: Not(IsNull()),
jobStatus: {
previewAt: Not(IsNull()),
},
isVisible: true,
faces: {
assetId: Not(IsNull()),
Expand Down
14 changes: 14 additions & 0 deletions server/src/services/media.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,18 @@ export class MediaService {
}

const previewPath = await this.generateThumbnail(asset, AssetPathType.PREVIEW, image.previewFormat);
if (!previewPath) {
return JobStatus.SKIPPED;
}

if (asset.previewPath && asset.previewPath !== previewPath) {
this.logger.debug(`Deleting old preview for asset ${asset.id}`);
await this.storageRepository.unlink(asset.previewPath);
}

await this.assetRepository.update({ id: asset.id, previewPath });
await this.assetRepository.upsertJobStatus({ assetId: asset.id, previewAt: new Date() });

return JobStatus.SUCCESS;
}

Expand Down Expand Up @@ -257,11 +264,18 @@ export class MediaService {
}

const thumbnailPath = await this.generateThumbnail(asset, AssetPathType.THUMBNAIL, image.thumbnailFormat);
if (!thumbnailPath) {
return JobStatus.SKIPPED;
}

if (asset.thumbnailPath && asset.thumbnailPath !== thumbnailPath) {
this.logger.debug(`Deleting old thumbnail for asset ${asset.id}`);
await this.storageRepository.unlink(asset.thumbnailPath);
}

await this.assetRepository.update({ id: asset.id, thumbnailPath });
await this.assetRepository.upsertJobStatus({ assetId: asset.id, thumbnailAt: new Date() });

return JobStatus.SUCCESS;
}

Expand Down
Loading