2023-07-27 00:31:52 -05:00
|
|
|
/*
|
2024-02-13 09:59:27 -06:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 00:31:52 -05:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
import { Global, Inject, Module } from '@nestjs/common';
|
2023-04-13 23:50:05 -05:00
|
|
|
import * as Redis from 'ioredis';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { DataSource } from 'typeorm';
|
2023-05-04 18:52:14 -05:00
|
|
|
import { MeiliSearch } from 'meilisearch';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { DI } from './di-symbols.js';
|
2023-05-23 19:59:30 -05:00
|
|
|
import { Config, loadConfig } from './config.js';
|
2023-02-07 04:50:38 -06:00
|
|
|
import { createPostgresDataSource } from './postgres.js';
|
2022-12-08 02:19:37 -06:00
|
|
|
import { RepositoryModule } from './models/RepositoryModule.js';
|
2024-01-07 21:28:13 -06:00
|
|
|
import { allSettled } from './misc/promise-tracker.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import type { Provider, OnApplicationShutdown } from '@nestjs/common';
|
2024-09-21 22:53:13 -05:00
|
|
|
import { MiMeta } from '@/models/Meta.js';
|
|
|
|
import { GlobalEvents } from './core/GlobalEventService.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
|
|
|
|
const $config: Provider = {
|
|
|
|
provide: DI.config,
|
2023-04-06 21:20:14 -05:00
|
|
|
useValue: loadConfig(),
|
2022-09-17 13:27:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const $db: Provider = {
|
|
|
|
provide: DI.db,
|
2022-09-21 15:11:26 -05:00
|
|
|
useFactory: async (config) => {
|
2023-02-07 04:50:38 -06:00
|
|
|
const db = createPostgresDataSource(config);
|
2022-09-17 13:27:08 -05:00
|
|
|
return await db.initialize();
|
|
|
|
},
|
2022-09-21 15:11:26 -05:00
|
|
|
inject: [DI.config],
|
2022-09-17 13:27:08 -05:00
|
|
|
};
|
|
|
|
|
2023-05-04 18:52:14 -05:00
|
|
|
const $meilisearch: Provider = {
|
|
|
|
provide: DI.meilisearch,
|
2023-05-23 19:59:30 -05:00
|
|
|
useFactory: (config: Config) => {
|
2023-05-04 18:52:14 -05:00
|
|
|
if (config.meilisearch) {
|
|
|
|
return new MeiliSearch({
|
2024-01-07 21:28:13 -06:00
|
|
|
host: `${config.meilisearch.ssl ? 'https' : 'http'}://${config.meilisearch.host}:${config.meilisearch.port}`,
|
2023-05-04 18:52:14 -05:00
|
|
|
apiKey: config.meilisearch.apiKey,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
const $redis: Provider = {
|
|
|
|
provide: DI.redis,
|
2023-05-23 19:59:30 -05:00
|
|
|
useFactory: (config: Config) => {
|
2023-07-20 05:50:31 -05:00
|
|
|
return new Redis.Redis(config.redis);
|
2022-09-17 13:27:08 -05:00
|
|
|
},
|
2022-09-21 15:11:26 -05:00
|
|
|
inject: [DI.config],
|
2022-09-17 13:27:08 -05:00
|
|
|
};
|
|
|
|
|
2023-04-09 03:09:27 -05:00
|
|
|
const $redisForPub: Provider = {
|
|
|
|
provide: DI.redisForPub,
|
2023-05-23 19:59:30 -05:00
|
|
|
useFactory: (config: Config) => {
|
2023-07-20 05:50:31 -05:00
|
|
|
const redis = new Redis.Redis(config.redisForPubsub);
|
2023-04-09 03:09:27 -05:00
|
|
|
return redis;
|
|
|
|
},
|
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
|
|
|
const $redisForSub: Provider = {
|
|
|
|
provide: DI.redisForSub,
|
2023-05-23 19:59:30 -05:00
|
|
|
useFactory: (config: Config) => {
|
2023-07-20 05:50:31 -05:00
|
|
|
const redis = new Redis.Redis(config.redisForPubsub);
|
2023-04-06 21:20:14 -05:00
|
|
|
redis.subscribe(config.host);
|
|
|
|
return redis;
|
2022-09-17 13:27:08 -05:00
|
|
|
},
|
2022-09-21 15:11:26 -05:00
|
|
|
inject: [DI.config],
|
2022-09-17 13:27:08 -05:00
|
|
|
};
|
|
|
|
|
2023-10-03 06:26:11 -05:00
|
|
|
const $redisForTimelines: Provider = {
|
|
|
|
provide: DI.redisForTimelines,
|
|
|
|
useFactory: (config: Config) => {
|
|
|
|
return new Redis.Redis(config.redisForTimelines);
|
|
|
|
},
|
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
2024-09-20 07:03:53 -05:00
|
|
|
const $redisForReactions: Provider = {
|
|
|
|
provide: DI.redisForReactions,
|
|
|
|
useFactory: (config: Config) => {
|
|
|
|
return new Redis.Redis(config.redisForReactions);
|
|
|
|
},
|
|
|
|
inject: [DI.config],
|
|
|
|
};
|
|
|
|
|
2024-09-21 22:53:13 -05:00
|
|
|
const $meta: Provider = {
|
|
|
|
provide: DI.meta,
|
|
|
|
useFactory: async (db: DataSource, redisForSub: Redis.Redis) => {
|
|
|
|
const meta = await db.transaction(async transactionalEntityManager => {
|
|
|
|
// 過去のバグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
|
|
|
|
const metas = await transactionalEntityManager.find(MiMeta, {
|
|
|
|
order: {
|
|
|
|
id: 'DESC',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const meta = metas[0];
|
|
|
|
|
|
|
|
if (meta) {
|
|
|
|
return meta;
|
|
|
|
} else {
|
|
|
|
// metaが空のときfetchMetaが同時に呼ばれるとここが同時に呼ばれてしまうことがあるのでフェイルセーフなupsertを使う
|
|
|
|
const saved = await transactionalEntityManager
|
|
|
|
.upsert(
|
|
|
|
MiMeta,
|
|
|
|
{
|
|
|
|
id: 'x',
|
|
|
|
},
|
|
|
|
['id'],
|
|
|
|
)
|
|
|
|
.then((x) => transactionalEntityManager.findOneByOrFail(MiMeta, x.identifiers[0]));
|
|
|
|
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
async function onMessage(_: string, data: string): Promise<void> {
|
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
|
|
if (obj.channel === 'internal') {
|
|
|
|
const { type, body } = obj.message as GlobalEvents['internal']['payload'];
|
|
|
|
switch (type) {
|
|
|
|
case 'metaUpdated': {
|
2024-09-24 04:29:02 -05:00
|
|
|
for (const key in body.after) {
|
|
|
|
(meta as any)[key] = (body.after as any)[key];
|
2024-09-21 22:53:13 -05:00
|
|
|
}
|
|
|
|
meta.proxyAccount = null; // joinなカラムは通常取ってこないので
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
redisForSub.on('message', onMessage);
|
|
|
|
|
|
|
|
return meta;
|
|
|
|
},
|
|
|
|
inject: [DI.db, DI.redisForSub],
|
|
|
|
};
|
|
|
|
|
2022-09-17 13:27:08 -05:00
|
|
|
@Global()
|
|
|
|
@Module({
|
|
|
|
imports: [RepositoryModule],
|
2024-09-21 22:53:13 -05:00
|
|
|
providers: [$config, $db, $meta, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, $redisForReactions],
|
|
|
|
exports: [$config, $db, $meta, $meilisearch, $redis, $redisForPub, $redisForSub, $redisForTimelines, $redisForReactions, RepositoryModule],
|
2022-09-17 13:27:08 -05:00
|
|
|
})
|
|
|
|
export class GlobalModule implements OnApplicationShutdown {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.db) private db: DataSource,
|
2022-09-20 14:54:21 -05:00
|
|
|
@Inject(DI.redis) private redisClient: Redis.Redis,
|
2023-04-09 03:09:27 -05:00
|
|
|
@Inject(DI.redisForPub) private redisForPub: Redis.Redis,
|
|
|
|
@Inject(DI.redisForSub) private redisForSub: Redis.Redis,
|
2023-10-03 06:26:11 -05:00
|
|
|
@Inject(DI.redisForTimelines) private redisForTimelines: Redis.Redis,
|
2024-09-20 07:03:53 -05:00
|
|
|
@Inject(DI.redisForReactions) private redisForReactions: Redis.Redis,
|
2024-01-07 21:28:13 -06:00
|
|
|
) { }
|
2022-09-17 13:27:08 -05:00
|
|
|
|
2023-05-28 23:21:26 -05:00
|
|
|
public async dispose(): Promise<void> {
|
2024-01-07 21:28:13 -06:00
|
|
|
// Wait for all potential DB queries
|
|
|
|
await allSettled();
|
|
|
|
// And then disconnect from DB
|
2022-09-17 13:27:08 -05:00
|
|
|
await Promise.all([
|
|
|
|
this.db.destroy(),
|
|
|
|
this.redisClient.disconnect(),
|
2023-04-09 03:09:27 -05:00
|
|
|
this.redisForPub.disconnect(),
|
|
|
|
this.redisForSub.disconnect(),
|
2023-10-03 06:26:11 -05:00
|
|
|
this.redisForTimelines.disconnect(),
|
2024-09-20 07:03:53 -05:00
|
|
|
this.redisForReactions.disconnect(),
|
2022-09-17 13:27:08 -05:00
|
|
|
]);
|
|
|
|
}
|
2023-05-28 23:21:26 -05:00
|
|
|
|
|
|
|
async onApplicationShutdown(signal: string): Promise<void> {
|
|
|
|
await this.dispose();
|
|
|
|
}
|
2022-09-17 13:27:08 -05:00
|
|
|
}
|