2023-03-20 11:55:28 -04:00
|
|
|
import { BadRequestException, Inject, Injectable, Logger } from '@nestjs/common';
|
2023-03-24 00:55:15 -04:00
|
|
|
import { assertMachineLearningEnabled } from '../domain.constant';
|
2023-03-20 11:55:28 -04:00
|
|
|
import { JobCommandDto } from './dto';
|
|
|
|
|
import { JobCommand, JobName, QueueName } from './job.constants';
|
|
|
|
|
import { IJobRepository } from './job.repository';
|
2023-04-01 22:46:07 +02:00
|
|
|
import { AllJobStatusResponseDto, JobStatusDto } from './response-dto';
|
2023-03-20 11:55:28 -04:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class JobService {
|
|
|
|
|
private logger = new Logger(JobService.name);
|
|
|
|
|
|
|
|
|
|
constructor(@Inject(IJobRepository) private jobRepository: IJobRepository) {}
|
|
|
|
|
|
2023-05-17 13:07:17 -04:00
|
|
|
async handleNightlyJobs() {
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.USER_DELETE_CHECK });
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.PERSON_CLEANUP });
|
2023-05-21 22:24:21 -04:00
|
|
|
await this.jobRepository.queue({ name: JobName.QUEUE_GENERATE_THUMBNAILS, data: { force: false } });
|
2023-05-17 13:07:17 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-20 11:55:28 -04:00
|
|
|
handleCommand(queueName: QueueName, dto: JobCommandDto): Promise<void> {
|
|
|
|
|
this.logger.debug(`Handling command: queue=${queueName},force=${dto.force}`);
|
|
|
|
|
|
|
|
|
|
switch (dto.command) {
|
|
|
|
|
case JobCommand.START:
|
|
|
|
|
return this.start(queueName, dto);
|
|
|
|
|
|
|
|
|
|
case JobCommand.PAUSE:
|
|
|
|
|
return this.jobRepository.pause(queueName);
|
|
|
|
|
|
2023-03-28 14:25:22 -04:00
|
|
|
case JobCommand.RESUME:
|
|
|
|
|
return this.jobRepository.resume(queueName);
|
|
|
|
|
|
2023-03-20 11:55:28 -04:00
|
|
|
case JobCommand.EMPTY:
|
|
|
|
|
return this.jobRepository.empty(queueName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-01 22:46:07 +02:00
|
|
|
async getJobStatus(queueName: QueueName): Promise<JobStatusDto> {
|
|
|
|
|
const [jobCounts, queueStatus] = await Promise.all([
|
|
|
|
|
this.jobRepository.getJobCounts(queueName),
|
|
|
|
|
this.jobRepository.getQueueStatus(queueName),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return { jobCounts, queueStatus };
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 11:55:28 -04:00
|
|
|
async getAllJobsStatus(): Promise<AllJobStatusResponseDto> {
|
|
|
|
|
const response = new AllJobStatusResponseDto();
|
|
|
|
|
for (const queueName of Object.values(QueueName)) {
|
2023-04-01 22:46:07 +02:00
|
|
|
response[queueName] = await this.getJobStatus(queueName);
|
2023-03-20 11:55:28 -04:00
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async start(name: QueueName, { force }: JobCommandDto): Promise<void> {
|
2023-04-01 22:46:07 +02:00
|
|
|
const { isActive } = await this.jobRepository.getQueueStatus(name);
|
2023-03-20 11:55:28 -04:00
|
|
|
if (isActive) {
|
|
|
|
|
throw new BadRequestException(`Job is already running`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (name) {
|
|
|
|
|
case QueueName.VIDEO_CONVERSION:
|
|
|
|
|
return this.jobRepository.queue({ name: JobName.QUEUE_VIDEO_CONVERSION, data: { force } });
|
|
|
|
|
|
|
|
|
|
case QueueName.STORAGE_TEMPLATE_MIGRATION:
|
|
|
|
|
return this.jobRepository.queue({ name: JobName.STORAGE_TEMPLATE_MIGRATION });
|
|
|
|
|
|
|
|
|
|
case QueueName.OBJECT_TAGGING:
|
|
|
|
|
assertMachineLearningEnabled();
|
|
|
|
|
return this.jobRepository.queue({ name: JobName.QUEUE_OBJECT_TAGGING, data: { force } });
|
|
|
|
|
|
|
|
|
|
case QueueName.CLIP_ENCODING:
|
|
|
|
|
assertMachineLearningEnabled();
|
|
|
|
|
return this.jobRepository.queue({ name: JobName.QUEUE_ENCODE_CLIP, data: { force } });
|
|
|
|
|
|
|
|
|
|
case QueueName.METADATA_EXTRACTION:
|
|
|
|
|
return this.jobRepository.queue({ name: JobName.QUEUE_METADATA_EXTRACTION, data: { force } });
|
|
|
|
|
|
feat(server): xmp sidecar metadata (#2466)
* initial commit for XMP sidecar support
* Added support for 'missing' metadata files to include those without sidecar files, now detects sidecar files in the filesystem for media already ingested but the sidecar was created afterwards
* didn't mean to commit default log level during testing
* new sidecar logic for video metadata as well
* Added xml mimetype for sidecars only
* don't need capture group for this regex
* wrong default value reverted
* simplified the move here - keep it in the same try catch since the outcome is to move the media back anyway
* simplified setter logic
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
* simplified logic per suggestions
* sidecar is now its own queue with a discover and sync, updated UI for the new job queueing
* queue a sidecar job for every asset based on discovery or sync, though the logic is almost identical aside from linking the sidecar
* now queue sidecar jobs for each assset, though logic is mostly the same between discovery and sync
* simplified logic of filename extraction and asset instantiation
* not sure how that got deleted..
* updated code per suggestions and comments in the PR
* stat was not being used, removed the variable set
* better type checking, using in-scope variables for exif getter instead of passing in every time
* removed commented out test
* ran and resolved all lints, formats, checks, and tests
* resolved suggested change in PR
* made getExifProperty more dynamic with multiple possible args for fallbacks, fixed typo, used generic in function for better type checking
* better error handling and moving files back to positions on move or save failure
* regenerated api
* format fixes
* Added XMP documentation
* documentation typo
* Merged in main
* missed merge conflict
* more changes due to a merge
* Resolving conflicts
* added icon for sidecar jobs
---------
Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-05-24 21:59:30 -04:00
|
|
|
case QueueName.SIDECAR:
|
|
|
|
|
return this.jobRepository.queue({ name: JobName.QUEUE_SIDECAR, data: { force } });
|
|
|
|
|
|
2023-03-20 11:55:28 -04:00
|
|
|
case QueueName.THUMBNAIL_GENERATION:
|
|
|
|
|
return this.jobRepository.queue({ name: JobName.QUEUE_GENERATE_THUMBNAILS, data: { force } });
|
|
|
|
|
|
2023-05-17 13:07:17 -04:00
|
|
|
case QueueName.RECOGNIZE_FACES:
|
|
|
|
|
return this.jobRepository.queue({ name: JobName.QUEUE_RECOGNIZE_FACES, data: { force } });
|
|
|
|
|
|
2023-03-20 11:55:28 -04:00
|
|
|
default:
|
|
|
|
|
throw new BadRequestException(`Invalid job name: ${name}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|