74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import type { BullRootModuleOptions } from '@nestjs/bullmq';
|
|
import type { QueueOptions } from 'bullmq';
|
|
import type { SecretService } from '../global/secret/secret.service';
|
|
|
|
export type BullMqConnectionOptions = NonNullable<QueueOptions['connection']>;
|
|
|
|
function parseRedisDatabase(pathname: string): number {
|
|
const trimmedPath = pathname.replace(/^\/+/u, '');
|
|
if (!trimmedPath) return 0;
|
|
|
|
const database = Number(trimmedPath);
|
|
if (!Number.isInteger(database) || database < 0) {
|
|
throw new Error('REDIS_URL must include a non-negative integer database.');
|
|
}
|
|
return database;
|
|
}
|
|
|
|
export function resolveBullMqRedisConnection(
|
|
redisUrl: string,
|
|
): BullMqConnectionOptions {
|
|
const parsedUrl = new URL(redisUrl);
|
|
if (parsedUrl.protocol !== 'redis:' && parsedUrl.protocol !== 'rediss:') {
|
|
throw new Error('REDIS_URL must use redis:// or rediss://.');
|
|
}
|
|
|
|
if (!parsedUrl.hostname) {
|
|
throw new Error('REDIS_URL must include a hostname.');
|
|
}
|
|
|
|
const connection: BullMqConnectionOptions = {
|
|
host: parsedUrl.hostname,
|
|
port: parsedUrl.port ? Number(parsedUrl.port) : 6379,
|
|
db: parseRedisDatabase(parsedUrl.pathname),
|
|
maxRetriesPerRequest: null,
|
|
};
|
|
|
|
if (parsedUrl.username) {
|
|
connection.username = decodeURIComponent(parsedUrl.username);
|
|
}
|
|
if (parsedUrl.password) {
|
|
connection.password = decodeURIComponent(parsedUrl.password);
|
|
}
|
|
if (parsedUrl.protocol === 'rediss:') {
|
|
connection.tls = {};
|
|
}
|
|
|
|
return connection;
|
|
}
|
|
|
|
export async function createBullMqOptions(
|
|
secretService: Pick<SecretService, 'get'>,
|
|
): Promise<BullRootModuleOptions> {
|
|
const redisUrl = await secretService.get<string>('REDIS_URL');
|
|
|
|
return {
|
|
connection: resolveBullMqRedisConnection(redisUrl),
|
|
defaultJobOptions: {
|
|
attempts: 2,
|
|
backoff: {
|
|
type: 'exponential',
|
|
delay: 1_000,
|
|
},
|
|
removeOnComplete: {
|
|
age: 86_400,
|
|
count: 1_000,
|
|
},
|
|
removeOnFail: {
|
|
age: 604_800,
|
|
count: 5_000,
|
|
},
|
|
},
|
|
};
|
|
}
|