mirror of
https://github.com/kremalicious/umami.git
synced 2024-12-24 18:26:20 +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;
|
global[KAFKA] = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log('Kafka initialized');
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +46,8 @@ async function getProducer() {
|
|||||||
global[KAFKA_PRODUCER] = producer;
|
global[KAFKA_PRODUCER] = producer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log('Kafka producer initialized');
|
||||||
|
|
||||||
return producer;
|
return producer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,12 @@ function getClient() {
|
|||||||
return null;
|
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') {
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
global[REDIS] = redis;
|
global[REDIS] = redis;
|
||||||
|
@ -32,7 +32,7 @@ export async function getSession(req) {
|
|||||||
|
|
||||||
// Check if website exists
|
// Check if website exists
|
||||||
if (redis.client) {
|
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
|
// Check database if redis does not have
|
||||||
@ -49,23 +49,21 @@ export async function getSession(req) {
|
|||||||
|
|
||||||
const session_uuid = uuid(websiteId, hostname, ip, userAgent);
|
const session_uuid = uuid(websiteId, hostname, ip, userAgent);
|
||||||
|
|
||||||
let sessionCreated = false;
|
|
||||||
let sessionId = null;
|
let sessionId = null;
|
||||||
let session = null;
|
let session = null;
|
||||||
|
|
||||||
// Check if session exists
|
// Check if session exists
|
||||||
if (redis.client) {
|
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
|
// Check database if redis does not have
|
||||||
if (!sessionCreated) {
|
if (!sessionId) {
|
||||||
session = await getSessionByUuid(session_uuid);
|
session = await getSessionByUuid(session_uuid);
|
||||||
sessionCreated = !!session;
|
|
||||||
sessionId = session ? session.session_id : null;
|
sessionId = session ? session.session_id : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sessionCreated) {
|
if (!sessionId) {
|
||||||
try {
|
try {
|
||||||
session = await createSession(websiteId, {
|
session = await createSession(websiteId, {
|
||||||
session_uuid,
|
session_uuid,
|
||||||
|
@ -23,7 +23,7 @@ async function relationalQuery(website_id, data) {
|
|||||||
})
|
})
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
if (redis.client && 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;
|
return res;
|
||||||
|
@ -19,7 +19,7 @@ async function relationalQuery(session_uuid) {
|
|||||||
})
|
})
|
||||||
.then(async res => {
|
.then(async res => {
|
||||||
if (redis.client && 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;
|
return res;
|
||||||
|
Loading…
Reference in New Issue
Block a user