feat(server): job system (#10134)

This commit is contained in:
forehalo
2025-02-18 05:41:56 +00:00
parent f6a86c10fe
commit cb895d4cb0
26 changed files with 1045 additions and 131 deletions
@@ -2,13 +2,18 @@ import './config';
import { Global, Module } from '@nestjs/common';
import { CacheRedis, SessionRedis, SocketIoRedis } from './instances';
import {
CacheRedis,
QueueRedis,
SessionRedis,
SocketIoRedis,
} from './instances';
@Global()
@Module({
providers: [CacheRedis, SessionRedis, SocketIoRedis],
exports: [CacheRedis, SessionRedis, SocketIoRedis],
providers: [CacheRedis, SessionRedis, SocketIoRedis, QueueRedis],
exports: [CacheRedis, SessionRedis, SocketIoRedis, QueueRedis],
})
export class RedisModule {}
export { CacheRedis, SessionRedis, SocketIoRedis };
export { CacheRedis, QueueRedis, SessionRedis, SocketIoRedis };
@@ -31,6 +31,16 @@ class Redis extends IORedis implements OnModuleInit, OnModuleDestroy {
client.on('error', this.errorHandler);
return client;
}
assertValidDBIndex(db: number) {
if (db && db > 15) {
throw new Error(
// Redis allows [0..16) by default
// we separate the db for different usages by `this.options.db + [0..4]`
`Invalid database index: ${db}, must be between 0 and 11`
);
}
}
}
@Injectable()
@@ -53,3 +63,15 @@ export class SocketIoRedis extends Redis {
super({ ...config.redis, db: (config.redis.db ?? 0) + 3 });
}
}
@Injectable()
export class QueueRedis extends Redis {
constructor(config: Config) {
super({
...config.redis,
db: (config.redis.db ?? 0) + 4,
// required explicitly set to `null` by bullmq
maxRetriesPerRequest: null,
});
}
}