first commit
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { Injectable, OnApplicationShutdown } from '@nestjs/common';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
/**
|
||||
* Creates Redis connections and closes them on shutdown. Blocking commands
|
||||
* (XREADGROUP BLOCK) and pub/sub subscribers need dedicated connections.
|
||||
*/
|
||||
@Injectable()
|
||||
export class RedisConnectionFactory implements OnApplicationShutdown {
|
||||
private readonly connections: Redis[] = [];
|
||||
|
||||
constructor(private readonly url: string) {}
|
||||
|
||||
create(name: string): Redis {
|
||||
const connection = new Redis(this.url, {
|
||||
connectionName: `task-stream:${name}`,
|
||||
maxRetriesPerRequest: null,
|
||||
});
|
||||
this.connections.push(connection);
|
||||
return connection;
|
||||
}
|
||||
|
||||
async onApplicationShutdown(): Promise<void> {
|
||||
await Promise.allSettled(this.connections.map((c) => c.quit()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export const REDIS_CLIENT = Symbol('REDIS_CLIENT');
|
||||
export const GRAPHQL_PUB_SUB = Symbol('GRAPHQL_PUB_SUB');
|
||||
Reference in New Issue
Block a user