2023-06-01 12:32:51 +02:00
|
|
|
import { RegisterQueueOptions } from '@nestjs/bullmq';
|
2024-03-20 21:04:03 +01:00
|
|
|
import { ConfigModuleOptions } from '@nestjs/config';
|
2023-06-01 12:32:51 +02:00
|
|
|
import { QueueOptions } from 'bullmq';
|
2024-04-16 01:39:06 +02:00
|
|
|
import { Request, Response } from 'express';
|
2023-03-31 16:36:08 +02:00
|
|
|
import { RedisOptions } from 'ioredis';
|
2024-03-20 21:04:03 +01:00
|
|
|
import Joi from 'joi';
|
2024-04-16 01:39:06 +02:00
|
|
|
import { CLS_ID, ClsModuleOptions } from 'nestjs-cls';
|
2024-03-20 22:02:51 +01:00
|
|
|
import { LogLevel } from 'src/entities/system-config.entity';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { QueueName } from 'src/interfaces/job.interface';
|
2024-03-20 21:04:03 +01:00
|
|
|
|
|
|
|
const WHEN_DB_URL_SET = Joi.when('DB_URL', {
|
|
|
|
is: Joi.exist(),
|
|
|
|
then: Joi.string().optional(),
|
|
|
|
otherwise: Joi.string().required(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export const immichAppConfig: ConfigModuleOptions = {
|
|
|
|
envFilePath: '.env',
|
|
|
|
isGlobal: true,
|
|
|
|
validationSchema: Joi.object({
|
|
|
|
NODE_ENV: Joi.string().optional().valid('development', 'production', 'staging').default('development'),
|
|
|
|
LOG_LEVEL: Joi.string()
|
|
|
|
.optional()
|
|
|
|
.valid(...Object.values(LogLevel)),
|
|
|
|
|
|
|
|
DB_USERNAME: WHEN_DB_URL_SET,
|
|
|
|
DB_PASSWORD: WHEN_DB_URL_SET,
|
|
|
|
DB_DATABASE_NAME: WHEN_DB_URL_SET,
|
|
|
|
DB_URL: Joi.string().optional(),
|
|
|
|
DB_VECTOR_EXTENSION: Joi.string().optional().valid('pgvector', 'pgvecto.rs').default('pgvecto.rs'),
|
2024-04-25 04:52:38 +02:00
|
|
|
DB_SKIP_MIGRATIONS: Joi.boolean().optional().default(false),
|
2024-03-20 21:04:03 +01:00
|
|
|
|
|
|
|
MACHINE_LEARNING_PORT: Joi.number().optional(),
|
|
|
|
MICROSERVICES_PORT: Joi.number().optional(),
|
|
|
|
IMMICH_METRICS_PORT: Joi.number().optional(),
|
|
|
|
|
|
|
|
IMMICH_METRICS: Joi.boolean().optional().default(false),
|
|
|
|
IMMICH_HOST_METRICS: Joi.boolean().optional().default(false),
|
|
|
|
IMMICH_API_METRICS: Joi.boolean().optional().default(false),
|
|
|
|
IMMICH_IO_METRICS: Joi.boolean().optional().default(false),
|
|
|
|
}),
|
|
|
|
};
|
2023-03-31 16:36:08 +02:00
|
|
|
|
2023-03-31 22:33:21 +02:00
|
|
|
function parseRedisConfig(): RedisOptions {
|
|
|
|
const redisUrl = process.env.REDIS_URL;
|
|
|
|
if (redisUrl && redisUrl.startsWith('ioredis://')) {
|
|
|
|
try {
|
|
|
|
const decodedString = Buffer.from(redisUrl.slice(10), 'base64').toString();
|
|
|
|
return JSON.parse(decodedString);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Failed to decode redis options: ${error}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
2024-04-27 17:04:23 +02:00
|
|
|
host: process.env.REDIS_HOSTNAME || 'redis',
|
2024-02-02 04:18:00 +01:00
|
|
|
port: Number.parseInt(process.env.REDIS_PORT || '6379'),
|
|
|
|
db: Number.parseInt(process.env.REDIS_DBINDEX || '0'),
|
2023-03-31 22:33:21 +02:00
|
|
|
username: process.env.REDIS_USERNAME || undefined,
|
|
|
|
password: process.env.REDIS_PASSWORD || undefined,
|
|
|
|
path: process.env.REDIS_SOCKET || undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-01 12:32:51 +02:00
|
|
|
export const bullConfig: QueueOptions = {
|
2023-03-30 21:38:55 +02:00
|
|
|
prefix: 'immich_bull',
|
2024-01-09 23:07:01 +01:00
|
|
|
connection: parseRedisConfig(),
|
2023-03-30 21:38:55 +02:00
|
|
|
defaultJobOptions: {
|
|
|
|
attempts: 3,
|
|
|
|
removeOnComplete: true,
|
|
|
|
removeOnFail: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-06-01 12:32:51 +02:00
|
|
|
export const bullQueues: RegisterQueueOptions[] = Object.values(QueueName).map((name) => ({ name }));
|
2024-04-16 01:39:06 +02:00
|
|
|
|
|
|
|
export const clsConfig: ClsModuleOptions = {
|
|
|
|
middleware: {
|
|
|
|
mount: true,
|
|
|
|
generateId: true,
|
|
|
|
setup: (cls, req: Request, res: Response) => {
|
|
|
|
const headerValues = req.headers['x-immich-cid'];
|
|
|
|
const headerValue = Array.isArray(headerValues) ? headerValues[0] : headerValues;
|
|
|
|
const cid = headerValue || cls.get(CLS_ID);
|
2024-04-16 23:31:49 +02:00
|
|
|
cls.set(CLS_ID, cid);
|
2024-04-16 01:39:06 +02:00
|
|
|
res.header('x-immich-cid', cid);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|