Add retry connection to redis. Clean up up redis/session logic

This commit is contained in:
Brian Cao 2022-09-05 22:47:45 -07:00
parent 81ca9c9665
commit 99914d6e98
5 changed files with 16 additions and 9 deletions

View File

@ -33,6 +33,8 @@ function getClient() {
global[KAFKA] = client;
}
log('Kafka initialized');
return client;
}
@ -44,6 +46,8 @@ async function getProducer() {
global[KAFKA_PRODUCER] = producer;
}
log('Kafka producer initialized');
return producer;
}

View File

@ -13,7 +13,12 @@ function getClient() {
return null;
}
const redis = new Redis(process.env.REDIS_URL);
const redis = new Redis(process.env.REDIS_URL, {
retryStrategy(times) {
log(`Redis reconnecting attempt: ${times}`);
return 5000;
},
});
if (process.env.NODE_ENV !== 'production') {
global[REDIS] = redis;

View File

@ -32,7 +32,7 @@ export async function getSession(req) {
// Check if website exists
if (redis.client) {
websiteId = await redis.client.get(`website:${website_uuid}`);
websiteId = Number(await redis.client.get(`website:${website_uuid}`));
}
// Check database if redis does not have
@ -49,23 +49,21 @@ export async function getSession(req) {
const session_uuid = uuid(websiteId, hostname, ip, userAgent);
let sessionCreated = false;
let sessionId = null;
let session = null;
// Check if session exists
if (redis.client) {
sessionCreated = !!(await redis.client.get(`session:${session_uuid}`));
sessionId = Number(await redis.client.get(`session:${session_uuid}`));
}
// Check database if redis does not have
if (!sessionCreated) {
if (!sessionId) {
session = await getSessionByUuid(session_uuid);
sessionCreated = !!session;
sessionId = session ? session.session_id : null;
}
if (!sessionCreated) {
if (!sessionId) {
try {
session = await createSession(websiteId, {
session_uuid,

View File

@ -23,7 +23,7 @@ async function relationalQuery(website_id, data) {
})
.then(async res => {
if (redis.client && res) {
await redis.client.set(`session:${res.session_uuid}`, 1);
await redis.client.set(`session:${res.session_uuid}`, res.session_id);
}
return res;

View File

@ -19,7 +19,7 @@ async function relationalQuery(session_uuid) {
})
.then(async res => {
if (redis.client && res) {
await redis.client.set(`session:${res.session_uuid}`, 1);
await redis.client.set(`session:${res.session_uuid}`, res.session_id);
}
return res;