Skip to content

Commit

Permalink
feat: start microservices in worker thread and add internal microserv…
Browse files Browse the repository at this point in the history
…ices for the server
  • Loading branch information
zackpollard committed May 14, 2024
1 parent 06402aa commit 1f84c9a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
1 change: 1 addition & 0 deletions server/src/interfaces/logger.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LogLevel } from 'src/entities/system-config.entity';
export const ILoggerRepository = 'ILoggerRepository';

export interface ILoggerRepository {
setAppName(name: string): void;
setContext(message: string): void;
setLogLevel(level: LogLevel): void;

Expand Down
34 changes: 17 additions & 17 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { json } from 'body-parser';
import cookieParser from 'cookie-parser';
import { CommandFactory } from 'nest-commander';
import { existsSync } from 'node:fs';
import { Worker } from 'node:worker_threads';
import sirv from 'sirv';
import { ApiModule, ImmichAdminModule, MicroservicesModule } from 'src/app.module';
import { ApiModule, ImmichAdminModule } from 'src/app.module';
import { WEB_ROOT, envName, excludePaths, isDev, serverVersion } from 'src/constants';
import { LogLevel } from 'src/entities/system-config.entity';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
Expand All @@ -16,28 +17,14 @@ import { useSwagger } from 'src/utils/misc';

const host = process.env.HOST;

async function bootstrapMicroservices() {
otelSDK.start();

const port = Number(process.env.MICROSERVICES_PORT) || 3002;
const app = await NestFactory.create(MicroservicesModule, { bufferLogs: true });
const logger = await app.resolve(ILoggerRepository);
logger.setContext('ImmichMicroservice');
app.useLogger(logger);
app.useWebSocketAdapter(new WebSocketAdapter(app));

await (host ? app.listen(port, host) : app.listen(port));

logger.log(`Immich Microservices is listening on ${await app.getUrl()} [v${serverVersion}] [${envName}] `);
}

async function bootstrapApi() {
otelSDK.start();

const port = Number(process.env.SERVER_PORT) || 3001;
const app = await NestFactory.create<NestExpressApplication>(ApiModule, { bufferLogs: true });
const logger = await app.resolve(ILoggerRepository);

logger.setAppName('ImmichServer');
logger.setContext('ImmichServer');
app.useLogger(logger);
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
Expand Down Expand Up @@ -86,15 +73,28 @@ async function bootstrapImmichAdmin() {
await CommandFactory.run(ImmichAdminModule);
}

function bootstrapMicroservicesWorker() {
const worker = new Worker('./dist/workers/microservices.js');
worker.on('exit', (exitCode) => {
if (exitCode !== 0) {
console.error(`Microservices worker exited with code ${exitCode}`);
process.exit(exitCode);
}
});
}

function bootstrap() {
switch (immichApp) {
case 'immich': {
process.title = 'immich_server';
if (process.env.INTERNAL_MICROSERVICES === 'true') {
bootstrapMicroservicesWorker();
}
return bootstrapApi();
}
case 'microservices': {
process.title = 'immich_microservices';
return bootstrapMicroservices();
return bootstrapMicroservicesWorker();
}
case 'immich-admin': {
process.title = 'immich_admin_cli';
Expand Down
10 changes: 10 additions & 0 deletions server/src/repositories/logger.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export class LoggerRepository extends ImmichLogger implements ILoggerRepository
super(LoggerRepository.name);
}

static appName?: string = undefined;

setAppName(name: string): void {
LoggerRepository.appName = name;
}

protected formatContext(context: string): string {
let formattedContext = super.formatContext(context);

Expand All @@ -18,6 +24,10 @@ export class LoggerRepository extends ImmichLogger implements ILoggerRepository
formattedContext += `[${correlationId}] `;
}

if (LoggerRepository.appName) {
formattedContext = `\u001B[34m[${LoggerRepository.appName}] ` + formattedContext;
}

return formattedContext;
}

Expand Down
32 changes: 32 additions & 0 deletions server/src/workers/microservices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { NestFactory } from '@nestjs/core';
import { isMainThread } from 'node:worker_threads';
import { MicroservicesModule } from 'src/app.module';
import { envName, serverVersion } from 'src/constants';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { WebSocketAdapter } from 'src/middleware/websocket.adapter';
import { otelSDK } from 'src/utils/instrumentation';

const host = process.env.HOST;

export async function bootstrapMicroservices() {
otelSDK.start();

const port = Number(process.env.MICROSERVICES_PORT) || 3002;
const app = await NestFactory.create(MicroservicesModule, { bufferLogs: true });
const logger = await app.resolve(ILoggerRepository);
logger.setAppName('ImmichMicroservices');
logger.setContext('ImmichMicroservices');
app.useLogger(logger);
app.useWebSocketAdapter(new WebSocketAdapter(app));

await (host ? app.listen(port, host) : app.listen(port));

logger.log(`Immich Microservices is listening on ${await app.getUrl()} [v${serverVersion}] [${envName}] `);
}

if (!isMainThread) {
bootstrapMicroservices().catch((error) => {
console.error(error);
process.exit(1);
});
}
1 change: 1 addition & 0 deletions server/test/repositories/logger.repository.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const newLoggerRepositoryMock = (): Mocked<ILoggerRepository> => {
return {
setLogLevel: vitest.fn(),
setContext: vitest.fn(),
setAppName: vitest.fn(),

verbose: vitest.fn(),
debug: vitest.fn(),
Expand Down

0 comments on commit 1f84c9a

Please sign in to comment.