2023-07-26 22:31:52 -07:00
|
|
|
/*
|
2024-02-13 07:59:27 -08:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-26 22:31:52 -07:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 11:27:08 -07:00
|
|
|
import { Global, Inject, Module } from '@nestjs/common';
|
2023-04-13 21:50:05 -07:00
|
|
|
import * as Redis from 'ioredis';
|
2022-09-17 11:27:08 -07:00
|
|
|
import { DataSource } from 'typeorm';
|
2023-05-04 16:52:14 -07:00
|
|
|
import { MeiliSearch } from 'meilisearch';
|
2022-09-17 11:27:08 -07:00
|
|
|
import { DI } from './di-symbols.js';
|
2023-05-23 17:59:30 -07:00
|
|
|
import { Config, loadConfig } from './config.js';
|
2023-02-07 02:50:38 -08:00
|
|
|
import { createPostgresDataSource } from './postgres.js';
|
2022-12-08 00:19:37 -08:00
|
|
|
import { RepositoryModule } from './models/RepositoryModule.js';
|
2024-01-07 19:28:13 -08:00
|
|
|
import { allSettled } from './misc/promise-tracker.js';
|
2022-09-17 11:27:08 -07:00
|
|
|
import type { Provider, OnApplicationShutdown } from '@nestjs/common';
|
|
|
|
|
|
|
|
const $config: Provider = {
|
|
|
|
provide: DI.config,
|
2023-04-06 19:20:14 -07:00
|
|
|
useValue: loadConfig(),
|
2022-09-17 11:27:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const $db: Provider = {
|
|
|
|
provide: DI.db,
|
2022-09-21 13:11:26 -07:00
|
|
|
useFactory: async (config) => {
|
2023-02-07 02:50:38 -08:00
|
|
|
const db = createPostgresDataSource(config);
|
2022-09-17 11:27:08 -07:00
|
|
|
return await db.initialize();
|
|
|
|
},
|
2022-09-21 13:11:26 -07:00
|
|
|
inject: [DI.config],
|
2022-09-17 11:27:08 -07:00
|
|
|
};
|
|
|
|
|
2023-05-04 16:52:14 -07:00
|
|
|
const $meilisearch: Provider = {
|
|
|
|
provide: DI.meilisearch,
|
2023-05-23 17:59:30 -07:00
|
|
|
useFactory: (config: Config) => {
|
2023-05-04 16:52:14 -07:00
|
|
|
if (config.meilisearch) {
|
|
|
|
return new MeiliSearch({
|
2024-01-07 19:28:13 -08:00
|
|
|
host: `${config.meilisearch.ssl ? 'https' : 'http'}://${config.meilisearch.host}:${config.meilisearch.port}`,
|
2023-05-04 16:52:14 -07:00
|
|
|
apiKey: config.meilisearch.apiKey,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
2022-09-17 11:27:08 -07:00
|
|
|
const $redis: Provider = {
|
|
|
|
provide: DI.redis,
|
2023-05-23 17:59:30 -07:00
|
|
|
useFactory: (config: Config) => {
|
2023-07-20 03:50:31 -07:00
|
|
|
return new Redis.Redis(config.redis);
|
2022-09-17 11:27:08 -07:00
|
|
|
},
|
2022-09-21 13:11:26 -07:00
|
|
|
inject: [DI.config],
|
2022-09-17 11:27:08 -07:00
|
|
|
};
|
|
|
|
|
2023-04-09 01:09:27 -07:00
|
|
|
const $redisForPub: Provider = {
|
|
|
|
provide: DI.redisForPub,
|
2023-05-23 17:59:30 -07:00
|
|
|
useFactory: (config: Config) => {
|
2023-07-20 03:50:31 -07:00
|
|
|
const redis = new Redis.Redis(config.redisForPubsub);
|
2023-04-09 01:09:27 -07:00
|
|
|
return redis;
|
|
|
|
},
|
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
|
|
|
const $redisForSub: Provider = {
|
|
|
|
provide: DI.redisForSub,
|
2023-05-23 17:59:30 -07:00
|
|
|
useFactory: (config: Config) => {
|
2023-07-20 03:50:31 -07:00
|
|
|
const redis = new Redis.Redis(config.redisForPubsub);
|
2023-04-06 19:20:14 -07:00
|
|
|
redis.subscribe(config.host);
|
|
|
|
return redis;
|
2022-09-17 11:27:08 -07:00
|
|
|
},
|
2022-09-21 13:11:26 -07:00
|
|
|
inject: [DI.config],
|
2022-09-17 11:27:08 -07:00
|
|
|
};
|
|
|
|
|
2023-10-03 04:26:11 -07:00
|
|
|
const $redisForTimelines: Provider = {
|
|
|
|
provide: DI.redisForTimelines,
|
|
|
|
useFactory: (config: Config) => {
|
|
|
|
return new Redis.Redis(config.redisForTimelines);
|
|
|
|
},
|
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
2022-09-17 11:27:08 -07:00
|
|
|
@Global()
|
|
|
|
@Module({
|
|
|
|
imports: [RepositoryModule],
|
2023-10-03 04:26:11 -07:00
|
|
|
providers: [$config, $db, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines],
|
|
|
|
exports: [$config, $db, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, RepositoryModule],
|
2022-09-17 11:27:08 -07:00
|
|
|
})
|
|
|
|
export class GlobalModule implements OnApplicationShutdown {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.db) private db: DataSource,
|
2022-09-20 12:54:21 -07:00
|
|
|
@Inject(DI.redis) private redisClient: Redis.Redis,
|
2023-04-09 01:09:27 -07:00
|
|
|
@Inject(DI.redisForPub) private redisForPub: Redis.Redis,
|
|
|
|
@Inject(DI.redisForSub) private redisForSub: Redis.Redis,
|
2023-10-03 04:26:11 -07:00
|
|
|
@Inject(DI.redisForTimelines) private redisForTimelines: Redis.Redis,
|
2024-01-07 19:28:13 -08:00
|
|
|
) { }
|
2022-09-17 11:27:08 -07:00
|
|
|
|
2023-05-28 21:21:26 -07:00
|
|
|
public async dispose(): Promise<void> {
|
2024-01-07 19:28:13 -08:00
|
|
|
// Wait for all potential DB queries
|
|
|
|
await allSettled();
|
|
|
|
// And then disconnect from DB
|
2022-09-17 11:27:08 -07:00
|
|
|
await Promise.all([
|
|
|
|
this.db.destroy(),
|
|
|
|
this.redisClient.disconnect(),
|
2023-04-09 01:09:27 -07:00
|
|
|
this.redisForPub.disconnect(),
|
|
|
|
this.redisForSub.disconnect(),
|
2023-10-03 04:26:11 -07:00
|
|
|
this.redisForTimelines.disconnect(),
|
2022-09-17 11:27:08 -07:00
|
|
|
]);
|
|
|
|
}
|
2023-05-28 21:21:26 -07:00
|
|
|
|
|
|
|
async onApplicationShutdown(signal: string): Promise<void> {
|
|
|
|
await this.dispose();
|
|
|
|
}
|
2022-09-17 11:27:08 -07:00
|
|
|
}
|