fix race conditions in check_connect.js
This commit is contained in:
parent
55df1ad10f
commit
524ddb9677
@ -12,27 +12,49 @@ const config = loadConfig();
|
|||||||
// createPostgresDataSource handels primaries and replicas automatically.
|
// createPostgresDataSource handels primaries and replicas automatically.
|
||||||
// usually, it only opens connections first use, so we force it using
|
// usually, it only opens connections first use, so we force it using
|
||||||
// .initialize()
|
// .initialize()
|
||||||
createPostgresDataSource(config)
|
async function connectToPostgres(){
|
||||||
.initialize()
|
const source = createPostgresDataSource(config);
|
||||||
.then(c => { c.destroy() })
|
await source.initialize();
|
||||||
.catch(e => { throw e });
|
await source.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
// Connect to all redis servers
|
// Connect to all redis servers
|
||||||
function connectToRedis(redisOptions) {
|
async function connectToRedis(redisOptions) {
|
||||||
const redis = new Redis(redisOptions);
|
return await new Promise(async (resolve, reject) => {
|
||||||
redis.on('connect', () => redis.disconnect());
|
const redis = new Redis({
|
||||||
redis.on('error', (e) => {
|
...redisOptions,
|
||||||
throw e;
|
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not all of these are defined, the default one gets reused.
|
// 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.
|
// so we use a Set to only try connecting once to each **uniq** redis.
|
||||||
(new Set([
|
const promises = Array
|
||||||
|
.from(new Set([
|
||||||
config.redis,
|
config.redis,
|
||||||
config.redisForPubsub,
|
config.redisForPubsub,
|
||||||
config.redisForJobQueue,
|
config.redisForJobQueue,
|
||||||
config.redisForTimelines,
|
config.redisForTimelines,
|
||||||
config.redisForReactions,
|
config.redisForReactions,
|
||||||
])).forEach(connectToRedis);
|
]))
|
||||||
|
.map(connectToRedis)
|
||||||
|
.concat([
|
||||||
|
connectToPostgres()
|
||||||
|
]);
|
||||||
|
|
||||||
|
await Promise.allSettled(promises);
|
||||||
|
Loading…
Reference in New Issue
Block a user