Add analysis pipeline
This commit is contained in:
1 parent
256d776131
commit
770c198ccd
20 files changed
+2572
-1
No files matched your search
@@ -0,0 +1,73 @@
|
||||
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,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user