import { BadRequestException } from '@nestjs/common'; import { defaults, SystemConfig } from 'src/config'; import { ImmichWorker, JobName, QueueCommand, QueueName } from 'src/enum'; import { QueueService } from 'src/services/queue.service'; import { newTestService, ServiceMocks } from 'test/utils'; describe(QueueService.name, () => { let sut: QueueService; let mocks: ServiceMocks; beforeEach(() => { ({ sut, mocks } = newTestService(QueueService)); mocks.config.getWorker.mockReturnValue(ImmichWorker.Microservices); }); it('should work', () => { expect(sut).toBeDefined(); }); describe('onConfigUpdate', () => { it('should update concurrency', () => { sut.onConfigUpdate({ newConfig: defaults, oldConfig: {} as SystemConfig }); expect(mocks.job.setConcurrency).toHaveBeenCalledTimes(17); expect(mocks.job.setConcurrency).toHaveBeenNthCalledWith(5, QueueName.FacialRecognition, 1); expect(mocks.job.setConcurrency).toHaveBeenNthCalledWith(7, QueueName.DuplicateDetection, 1); expect(mocks.job.setConcurrency).toHaveBeenNthCalledWith(8, QueueName.BackgroundTask, 5); expect(mocks.job.setConcurrency).toHaveBeenNthCalledWith(9, QueueName.StorageTemplateMigration, 1); }); }); describe('handleNightlyJobs', () => { it('should run the scheduled jobs', async () => { await sut.handleNightlyJobs(); expect(mocks.job.queueAll).toHaveBeenCalledWith([ { name: JobName.AssetDeleteCheck }, { name: JobName.UserDeleteCheck }, { name: JobName.PersonCleanup }, { name: JobName.MemoryCleanup }, { name: JobName.SessionCleanup }, { name: JobName.AuditTableCleanup }, { name: JobName.AuditLogCleanup }, { name: JobName.MemoryGenerate }, { name: JobName.UserSyncUsage }, { name: JobName.AssetGenerateThumbnailsQueueAll, data: { force: false } }, { name: JobName.FacialRecognitionQueueAll, data: { force: false, nightly: true } }, ]); }); }); describe('getAllJobStatus', () => { it('should get all job statuses', async () => { mocks.job.getJobCounts.mockResolvedValue({ active: 1, completed: 1, failed: 1, delayed: 1, waiting: 1, paused: 1, }); mocks.job.getQueueStatus.mockResolvedValue({ isActive: true, isPaused: true, }); const expectedJobStatus = { jobCounts: { active: 1, completed: 1, delayed: 1, failed: 1, waiting: 1, paused: 1, }, queueStatus: { isActive: true, isPaused: true, }, }; await expect(sut.getAll()).resolves.toEqual({ [QueueName.BackgroundTask]: expectedJobStatus, [QueueName.DuplicateDetection]: expectedJobStatus, [QueueName.SmartSearch]: expectedJobStatus, [QueueName.MetadataExtraction]: expectedJobStatus, [QueueName.Search]: expectedJobStatus, [QueueName.StorageTemplateMigration]: expectedJobStatus, [QueueName.Migration]: expectedJobStatus, [QueueName.ThumbnailGeneration]: expectedJobStatus, [QueueName.VideoConversion]: expectedJobStatus, [QueueName.FaceDetection]: expectedJobStatus, [QueueName.FacialRecognition]: expectedJobStatus, [QueueName.Sidecar]: expectedJobStatus, [QueueName.Library]: expectedJobStatus, [QueueName.Notification]: expectedJobStatus, [QueueName.BackupDatabase]: expectedJobStatus, [QueueName.Ocr]: expectedJobStatus, [QueueName.Workflow]: expectedJobStatus, }); }); }); describe('handleCommand', () => { it('should handle a pause command', async () => { await sut.runCommand(QueueName.MetadataExtraction, { command: QueueCommand.Pause, force: false }); expect(mocks.job.pause).toHaveBeenCalledWith(QueueName.MetadataExtraction); }); it('should handle a resume command', async () => { await sut.runCommand(QueueName.MetadataExtraction, { command: QueueCommand.Resume, force: false }); expect(mocks.job.resume).toHaveBeenCalledWith(QueueName.MetadataExtraction); }); it('should handle an empty command', async () => { await sut.runCommand(QueueName.MetadataExtraction, { command: QueueCommand.Empty, force: false }); expect(mocks.job.empty).toHaveBeenCalledWith(QueueName.MetadataExtraction); }); it('should not start a job that is already running', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: true, isPaused: false }); await expect( sut.runCommand(QueueName.VideoConversion, { command: QueueCommand.Start, force: false }), ).rejects.toBeInstanceOf(BadRequestException); expect(mocks.job.queue).not.toHaveBeenCalled(); expect(mocks.job.queueAll).not.toHaveBeenCalled(); }); it('should handle a start video conversion command', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await sut.runCommand(QueueName.VideoConversion, { command: QueueCommand.Start, force: false }); expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.AssetEncodeVideoQueueAll, data: { force: false } }); }); it('should handle a start storage template migration command', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await sut.runCommand(QueueName.StorageTemplateMigration, { command: QueueCommand.Start, force: false }); expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.StorageTemplateMigration }); }); it('should handle a start smart search command', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await sut.runCommand(QueueName.SmartSearch, { command: QueueCommand.Start, force: false }); expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.SmartSearchQueueAll, data: { force: false } }); }); it('should handle a start metadata extraction command', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await sut.runCommand(QueueName.MetadataExtraction, { command: QueueCommand.Start, force: false }); expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.AssetExtractMetadataQueueAll, data: { force: false }, }); }); it('should handle a start sidecar command', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await sut.runCommand(QueueName.Sidecar, { command: QueueCommand.Start, force: false }); expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.SidecarQueueAll, data: { force: false } }); }); it('should handle a start thumbnail generation command', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await sut.runCommand(QueueName.ThumbnailGeneration, { command: QueueCommand.Start, force: false }); expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.AssetGenerateThumbnailsQueueAll, data: { force: false }, }); }); it('should handle a start face detection command', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await sut.runCommand(QueueName.FaceDetection, { command: QueueCommand.Start, force: false }); expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.AssetDetectFacesQueueAll, data: { force: false } }); }); it('should handle a start facial recognition command', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await sut.runCommand(QueueName.FacialRecognition, { command: QueueCommand.Start, force: false }); expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.FacialRecognitionQueueAll, data: { force: false } }); }); it('should handle a start backup database command', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await sut.runCommand(QueueName.BackupDatabase, { command: QueueCommand.Start, force: false }); expect(mocks.job.queue).toHaveBeenCalledWith({ name: JobName.DatabaseBackup, data: { force: false } }); }); it('should throw a bad request when an invalid queue is used', async () => { mocks.job.getQueueStatus.mockResolvedValue({ isActive: false, isPaused: false }); await expect( sut.runCommand(QueueName.BackgroundTask, { command: QueueCommand.Start, force: false }), ).rejects.toBeInstanceOf(BadRequestException); expect(mocks.job.queue).not.toHaveBeenCalled(); expect(mocks.job.queueAll).not.toHaveBeenCalled(); }); }); });