diff --git a/lib/kafka.js b/lib/kafka.js index 6d007009..6b6f315f 100644 --- a/lib/kafka.js +++ b/lib/kafka.js @@ -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; } diff --git a/lib/redis.js b/lib/redis.js index 92c8bdb9..63b513e6 100644 --- a/lib/redis.js +++ b/lib/redis.js @@ -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; diff --git a/lib/session.js b/lib/session.js index 1b79f316..6e78333b 100644 --- a/lib/session.js +++ b/lib/session.js @@ -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, diff --git a/queries/analytics/session/createSession.js b/queries/analytics/session/createSession.js index 8153b8f4..4e23dac1 100644 --- a/queries/analytics/session/createSession.js +++ b/queries/analytics/session/createSession.js @@ -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; diff --git a/queries/analytics/session/getSessionByUuid.js b/queries/analytics/session/getSessionByUuid.js index 6412ca8c..911848dc 100644 --- a/queries/analytics/session/getSessionByUuid.js +++ b/queries/analytics/session/getSessionByUuid.js @@ -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;