Sharkey/packages/backend/src/GlobalModule.ts

118 lines
3.2 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { setTimeout } from 'node:timers/promises';
2022-09-17 21:27:08 +03:00
import { Global, Inject, Module } from '@nestjs/common';
2023-04-14 07:50:05 +03:00
import * as Redis from 'ioredis';
2022-09-17 21:27:08 +03:00
import { DataSource } from 'typeorm';
import { MeiliSearch } from 'meilisearch';
2022-09-17 21:27:08 +03:00
import { DI } from './di-symbols.js';
2023-05-24 03:59:30 +03:00
import { Config, loadConfig } from './config.js';
2023-02-07 12:50:38 +02:00
import { createPostgresDataSource } from './postgres.js';
import { RepositoryModule } from './models/RepositoryModule.js';
2022-09-17 21:27:08 +03:00
import type { Provider, OnApplicationShutdown } from '@nestjs/common';
const $config: Provider = {
provide: DI.config,
useValue: loadConfig(),
2022-09-17 21:27:08 +03:00
};
const $db: Provider = {
provide: DI.db,
2022-09-21 23:11:26 +03:00
useFactory: async (config) => {
2023-02-07 12:50:38 +02:00
const db = createPostgresDataSource(config);
2022-09-17 21:27:08 +03:00
return await db.initialize();
},
2022-09-21 23:11:26 +03:00
inject: [DI.config],
2022-09-17 21:27:08 +03:00
};
const $meilisearch: Provider = {
provide: DI.meilisearch,
2023-05-24 03:59:30 +03:00
useFactory: (config: Config) => {
if (config.meilisearch) {
return new MeiliSearch({
host: `${config.meilisearch.ssl ? 'https' : 'http' }://${config.meilisearch.host}:${config.meilisearch.port}`,
apiKey: config.meilisearch.apiKey,
});
} else {
return null;
}
},
inject: [DI.config],
};
2022-09-17 21:27:08 +03:00
const $redis: Provider = {
provide: DI.redis,
2023-05-24 03:59:30 +03:00
useFactory: (config: Config) => {
return new Redis.Redis(config.redis);
2022-09-17 21:27:08 +03:00
},
2022-09-21 23:11:26 +03:00
inject: [DI.config],
2022-09-17 21:27:08 +03:00
};
const $redisForPub: Provider = {
provide: DI.redisForPub,
2023-05-24 03:59:30 +03:00
useFactory: (config: Config) => {
const redis = new Redis.Redis(config.redisForPubsub);
return redis;
},
inject: [DI.config],
};
const $redisForSub: Provider = {
provide: DI.redisForSub,
2023-05-24 03:59:30 +03:00
useFactory: (config: Config) => {
const redis = new Redis.Redis(config.redisForPubsub);
redis.subscribe(config.host);
return redis;
2022-09-17 21:27:08 +03:00
},
2022-09-21 23:11:26 +03:00
inject: [DI.config],
2022-09-17 21:27:08 +03:00
};
2023-10-03 14:26:11 +03:00
const $redisForTimelines: Provider = {
provide: DI.redisForTimelines,
useFactory: (config: Config) => {
return new Redis.Redis(config.redisForTimelines);
},
inject: [DI.config],
};
2022-09-17 21:27:08 +03:00
@Global()
@Module({
imports: [RepositoryModule],
2023-10-03 14:26:11 +03:00
providers: [$config, $db, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines],
exports: [$config, $db, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, RepositoryModule],
2022-09-17 21:27:08 +03:00
})
export class GlobalModule implements OnApplicationShutdown {
constructor(
@Inject(DI.db) private db: DataSource,
2022-09-20 22:54:21 +03:00
@Inject(DI.redis) private redisClient: Redis.Redis,
@Inject(DI.redisForPub) private redisForPub: Redis.Redis,
@Inject(DI.redisForSub) private redisForSub: Redis.Redis,
2023-10-03 14:26:11 +03:00
@Inject(DI.redisForTimelines) private redisForTimelines: Redis.Redis,
2022-09-17 21:27:08 +03:00
) {}
2023-05-29 07:21:26 +03:00
public async dispose(): Promise<void> {
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 21:27:08 +03:00
await Promise.all([
this.db.destroy(),
this.redisClient.disconnect(),
this.redisForPub.disconnect(),
this.redisForSub.disconnect(),
2023-10-03 14:26:11 +03:00
this.redisForTimelines.disconnect(),
2022-09-17 21:27:08 +03:00
]);
}
2023-05-29 07:21:26 +03:00
async onApplicationShutdown(signal: string): Promise<void> {
await this.dispose();
}
2022-09-17 21:27:08 +03:00
}