Skip to content

High performance self-hosted photo and video management solution.

refactor(server): app init (#2638)

Jason Rasmussen800f010383f2
diff --git a/server/apps/immich/src/app.cron-jobs.ts b/server/apps/immich/src/app.cron-jobs.ts
deleted file mode 100644
index 7473c90b5b..0000000000
--- a/server/apps/immich/src/app.cron-jobs.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-import { JobService } from '@app/domain';
-import { Injectable } from '@nestjs/common';
-import { Cron, CronExpression } from '@nestjs/schedule';
-
-@Injectable()
-export class AppCronJobs {
- constructor(private jobService: JobService) {}
-
- @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
- async onNightlyJob() {
- await this.jobService.handleNightlyJobs();
- }
-}
diff --git a/server/apps/immich/src/app.module.ts b/server/apps/immich/src/app.module.ts
index 51f5e32076..f38f0f4d28 100644
--- a/server/apps/immich/src/app.module.ts
+++ b/server/apps/immich/src/app.module.ts
@@ -1,29 +1,29 @@
-import { Module, OnModuleInit } from '@nestjs/common';
-import { AssetModule } from './api-v1/asset/asset.module';
-import { AlbumModule } from './api-v1/album/album.module';
-import { AppController } from './app.controller';
-import { ScheduleModule } from '@nestjs/schedule';
-import { DomainModule, SearchService } from '@app/domain';
+import { DomainModule } from '@app/domain';
import { InfraModule } from '@app/infra';
+import { Module } from '@nestjs/common';
+import { APP_GUARD } from '@nestjs/core';
+import { ScheduleModule } from '@nestjs/schedule';
+import { AlbumModule } from './api-v1/album/album.module';
+import { AssetModule } from './api-v1/asset/asset.module';
+import { AppService } from './app.service';
import {
AlbumController,
APIKeyController,
+ AppController,
AssetController,
AuthController,
- PersonController,
JobController,
OAuthController,
PartnerController,
+ PersonController,
SearchController,
ServerInfoController,
SharedLinkController,
SystemConfigController,
- UserController,
TagController,
+ UserController,
} from './controllers';
-import { APP_GUARD } from '@nestjs/core';
import { AuthGuard } from './middlewares/auth.guard';
-import { AppCronJobs } from './app.cron-jobs';
@Module({
imports: [
@@ -54,12 +54,7 @@ import { AppCronJobs } from './app.cron-jobs';
//
{ provide: APP_GUARD, useExisting: AuthGuard },
AuthGuard,
- AppCronJobs,
+ AppService,
],
})
-export class AppModule implements OnModuleInit {
- constructor(private searchService: SearchService) {}
- async onModuleInit() {
- await this.searchService.bootstrap();
- }
-}
+export class AppModule {}
diff --git a/server/apps/immich/src/app.service.ts b/server/apps/immich/src/app.service.ts
new file mode 100644
index 0000000000..c366d88512
--- /dev/null
+++ b/server/apps/immich/src/app.service.ts
@@ -0,0 +1,22 @@
+import { JobService, SearchService, StorageService } from '@app/domain';
+import { Injectable } from '@nestjs/common';
+import { Cron, CronExpression } from '@nestjs/schedule';
+
+@Injectable()
+export class AppService {
+ constructor(
+ private jobService: JobService,
+ private searchService: SearchService,
+ private storageService: StorageService,
+ ) {}
+
+ @Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
+ async onNightlyJob() {
+ await this.jobService.handleNightlyJobs();
+ }
+
+ async init() {
+ this.storageService.init();
+ await this.searchService.init();
+ }
+}
diff --git a/server/apps/immich/src/app.controller.ts b/server/apps/immich/src/controllers/app.controller.ts
similarity index 100%
rename from server/apps/immich/src/app.controller.ts
rename to server/apps/immich/src/controllers/app.controller.ts
diff --git a/server/apps/immich/src/controllers/index.ts b/server/apps/immich/src/controllers/index.ts
index 5917f0f79a..e257c5a9ad 100644
--- a/server/apps/immich/src/controllers/index.ts
+++ b/server/apps/immich/src/controllers/index.ts
@@ -1,5 +1,6 @@
export * from './album.controller';
export * from './api-key.controller';
+export * from './app.controller';
export * from './asset.controller';
export * from './auth.controller';
export * from './job.controller';
diff --git a/server/apps/immich/src/main.ts b/server/apps/immich/src/main.ts
index ccb97c4d9e..72d8991dcd 100644
--- a/server/apps/immich/src/main.ts
+++ b/server/apps/immich/src/main.ts
@@ -6,7 +6,6 @@ import {
MACHINE_LEARNING_ENABLED,
SearchService,
SERVER_VERSION,
- StorageService,
} from '@app/domain';
import { RedisIoAdapter } from '@app/infra';
import { Logger } from '@nestjs/common';
@@ -18,6 +17,7 @@ import cookieParser from 'cookie-parser';
import { writeFileSync } from 'fs';
import path from 'path';
import { AppModule } from './app.module';
+import { AppService } from './app.service';
import { patchOpenAPI } from './utils/patch-open-api.util';
const logger = new Logger('ImmichServer');
@@ -73,7 +73,7 @@ async function bootstrap() {
customSiteTitle: 'Immich API Documentation',
});
- app.get(StorageService).init();
+ await app.get(AppService).init();
await app.listen(serverPort, () => {
if (process.env.NODE_ENV == 'development') {
diff --git a/server/libs/domain/src/search/search.service.spec.ts b/server/libs/domain/src/search/search.service.spec.ts
index ddb842ea48..ebd614e5b1 100644
--- a/server/libs/domain/src/search/search.service.spec.ts
+++ b/server/libs/domain/src/search/search.service.spec.ts
@@ -110,11 +110,11 @@ describe(SearchService.name, () => {
});
});
- describe(`bootstrap`, () => {
+ describe(`init`, () => {
it('should skip when search is disabled', async () => {
const sut = makeSut('false');
- await sut.bootstrap();
+ await sut.init();
expect(searchMock.setup).not.toHaveBeenCalled();
expect(searchMock.checkMigrationStatus).not.toHaveBeenCalled();
@@ -125,7 +125,7 @@ describe(SearchService.name, () => {
it('should skip schema migration if not needed', async () => {
searchMock.checkMigrationStatus.mockResolvedValue({ assets: false, albums: false, faces: false });
- await sut.bootstrap();
+ await sut.init();
expect(searchMock.setup).toHaveBeenCalled();
expect(jobMock.queue).not.toHaveBeenCalled();
@@ -133,7 +133,7 @@ describe(SearchService.name, () => {
it('should do schema migration if needed', async () => {
searchMock.checkMigrationStatus.mockResolvedValue({ assets: true, albums: true, faces: true });
- await sut.bootstrap();
+ await sut.init();
expect(searchMock.setup).toHaveBeenCalled();
expect(jobMock.queue.mock.calls).toEqual([
diff --git a/server/libs/domain/src/search/search.service.ts b/server/libs/domain/src/search/search.service.ts
index 08b78127f0..7a69ad8c21 100644
--- a/server/libs/domain/src/search/search.service.ts
+++ b/server/libs/domain/src/search/search.service.ts
@@ -80,7 +80,7 @@ export class SearchService {
};
}
- async bootstrap() {
+ async init() {
if (!this.enabled) {
return;
}