2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-04-14 05:14:00 +00:00
|
|
|
import Redis from 'ioredis';
|
2024-04-04 13:25:28 +00:00
|
|
|
import { loadConfig } from '../built/config.js';
|
2024-08-04 16:09:48 +00:00
|
|
|
import { createPostgresDataSource } from '../built/postgres.js';
|
2023-02-24 05:09:17 +00:00
|
|
|
|
|
|
|
const config = loadConfig();
|
|
|
|
|
2024-08-04 16:09:48 +00:00
|
|
|
// createPostgresDataSource handels primaries and replicas automatically.
|
|
|
|
// usually, it only opens connections first use, so we force it using
|
|
|
|
// .initialize()
|
2024-10-26 12:57:26 +00:00
|
|
|
async function connectToPostgres(){
|
|
|
|
const source = createPostgresDataSource(config);
|
|
|
|
await source.initialize();
|
|
|
|
await source.destroy();
|
|
|
|
}
|
2024-08-04 16:09:48 +00:00
|
|
|
|
|
|
|
// Connect to all redis servers
|
2024-10-26 12:57:26 +00:00
|
|
|
async function connectToRedis(redisOptions) {
|
|
|
|
return await new Promise(async (resolve, reject) => {
|
|
|
|
const redis = new Redis({
|
|
|
|
...redisOptions,
|
|
|
|
lazyConnect: true,
|
|
|
|
reconnectOnError: false,
|
|
|
|
showFriendlyErrorStack: true,
|
|
|
|
});
|
|
|
|
redis.on('error', e => reject(e));
|
|
|
|
|
|
|
|
try {
|
|
|
|
await redis.connect();
|
|
|
|
resolve();
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
redis.disconnect(false);
|
|
|
|
}
|
2024-08-04 16:09:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not all of these are defined, the default one gets reused.
|
|
|
|
// so we use a Set to only try connecting once to each **uniq** redis.
|
2024-10-26 12:57:26 +00:00
|
|
|
const promises = Array
|
|
|
|
.from(new Set([
|
|
|
|
config.redis,
|
|
|
|
config.redisForPubsub,
|
|
|
|
config.redisForJobQueue,
|
|
|
|
config.redisForTimelines,
|
|
|
|
config.redisForReactions,
|
|
|
|
]))
|
|
|
|
.map(connectToRedis)
|
|
|
|
.concat([
|
|
|
|
connectToPostgres()
|
|
|
|
]);
|
|
|
|
|
|
|
|
await Promise.allSettled(promises);
|