mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-24 02:06:19 +01:00
Add retry connection to redis. Clean up up redis/session logic
This commit is contained in:
parent
81ca9c9665
commit
99914d6e98
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user