2023-03-03 03:13:12 +01:00
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Global, Inject, Module } from '@nestjs/common';
|
2022-09-20 21:54:21 +02:00
|
|
|
import Redis from 'ioredis';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DataSource } from 'typeorm';
|
|
|
|
import { DI } from './di-symbols.js';
|
|
|
|
import { loadConfig } from './config.js';
|
2023-02-07 11:50:38 +01:00
|
|
|
import { createPostgresDataSource } from './postgres.js';
|
2022-12-08 09:19:37 +01:00
|
|
|
import { RepositoryModule } from './models/RepositoryModule.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import type { Provider, OnApplicationShutdown } from '@nestjs/common';
|
|
|
|
|
|
|
|
const $config: Provider = {
|
|
|
|
provide: DI.config,
|
2023-04-07 04:20:14 +02:00
|
|
|
useValue: loadConfig(),
|
2022-09-17 20:27:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const $db: Provider = {
|
|
|
|
provide: DI.db,
|
2022-09-21 22:11:26 +02:00
|
|
|
useFactory: async (config) => {
|
2023-02-07 11:50:38 +01:00
|
|
|
const db = createPostgresDataSource(config);
|
2022-09-17 20:27:08 +02:00
|
|
|
return await db.initialize();
|
|
|
|
},
|
2022-09-21 22:11:26 +02:00
|
|
|
inject: [DI.config],
|
2022-09-17 20:27:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const $redis: Provider = {
|
|
|
|
provide: DI.redis,
|
2022-09-21 22:11:26 +02:00
|
|
|
useFactory: (config) => {
|
2023-04-07 04:20:14 +02:00
|
|
|
return new Redis({
|
|
|
|
port: config.redis.port,
|
|
|
|
host: config.redis.host,
|
|
|
|
family: config.redis.family == null ? 0 : config.redis.family,
|
|
|
|
password: config.redis.pass,
|
|
|
|
keyPrefix: `${config.redis.prefix}:`,
|
|
|
|
db: config.redis.db ?? 0,
|
|
|
|
});
|
2022-09-17 20:27:08 +02:00
|
|
|
},
|
2022-09-21 22:11:26 +02:00
|
|
|
inject: [DI.config],
|
2022-09-17 20:27:08 +02:00
|
|
|
};
|
|
|
|
|
2023-04-07 04:20:14 +02:00
|
|
|
const $redisForPubsub: Provider = {
|
|
|
|
provide: DI.redisForPubsub,
|
2022-09-21 22:11:26 +02:00
|
|
|
useFactory: (config) => {
|
2023-04-07 04:20:14 +02:00
|
|
|
const redis = new Redis({
|
|
|
|
port: config.redisForPubsub.port,
|
|
|
|
host: config.redisForPubsub.host,
|
|
|
|
family: config.redisForPubsub.family == null ? 0 : config.redisForPubsub.family,
|
|
|
|
password: config.redisForPubsub.pass,
|
|
|
|
keyPrefix: `${config.redisForPubsub.prefix}:`,
|
|
|
|
db: config.redisForPubsub.db ?? 0,
|
|
|
|
});
|
|
|
|
redis.subscribe(config.host);
|
|
|
|
return redis;
|
2022-09-17 20:27:08 +02:00
|
|
|
},
|
2022-09-21 22:11:26 +02:00
|
|
|
inject: [DI.config],
|
2022-09-17 20:27:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
@Global()
|
|
|
|
@Module({
|
|
|
|
imports: [RepositoryModule],
|
2023-04-07 04:20:14 +02:00
|
|
|
providers: [$config, $db, $redis, $redisForPubsub],
|
|
|
|
exports: [$config, $db, $redis, $redisForPubsub, RepositoryModule],
|
2022-09-17 20:27:08 +02:00
|
|
|
})
|
|
|
|
export class GlobalModule implements OnApplicationShutdown {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.db) private db: DataSource,
|
2022-09-20 21:54:21 +02:00
|
|
|
@Inject(DI.redis) private redisClient: Redis.Redis,
|
2023-04-07 04:20:14 +02:00
|
|
|
@Inject(DI.redisForPubsub) private redisForPubsub: Redis.Redis,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
async onApplicationShutdown(signal: string): Promise<void> {
|
2023-03-03 03:13:12 +01:00
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
// XXX:
|
|
|
|
// Shutting down the existing connections causes errors on Jest as
|
|
|
|
// Misskey has asynchronous postgres/redis connections that are not
|
|
|
|
// awaited.
|
|
|
|
// Let's wait for some random time for them to finish.
|
|
|
|
await setTimeout(5000);
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
await Promise.all([
|
|
|
|
this.db.destroy(),
|
|
|
|
this.redisClient.disconnect(),
|
2023-04-07 04:20:14 +02:00
|
|
|
this.redisForPubsub.disconnect(),
|
2022-09-17 20:27:08 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|