init redis lazy

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-07-20 20:17:32 +02:00
parent 492141547d
commit f2298a1b05
19 changed files with 134 additions and 86 deletions

View File

@@ -1,11 +1,10 @@
import { logger } from '@/utils/logger';
import { getClientIp, parseIp } from '@/utils/parseIp';
import type { FastifyReply, FastifyRequest } from 'fastify';
import { generateDeviceId } from '@openpanel/common';
import { getSalts } from '@openpanel/db';
import { eventsQueue } from '@openpanel/queue';
import { redis } from '@openpanel/redis';
import { getRedisCache } from '@openpanel/redis';
import type { PostEventPayload } from '@openpanel/sdk';
export async function postEvent(
@@ -38,7 +37,7 @@ export async function postEvent(
});
// this will ensure that we don't have multiple events creating sessions
const locked = await redis.set(
const locked = await getRedisCache().set(
`request:priority:${currentDeviceId}-${previousDeviceId}`,
'locked',
'EX',

View File

@@ -1,6 +1,5 @@
import { validateClerkJwt } from '@/utils/auth';
import type { FastifyReply, FastifyRequest } from 'fastify';
import { escape } from 'sqlstring';
import superjson from 'superjson';
import type * as WebSocket from 'ws';
@@ -13,7 +12,7 @@ import {
TABLE_NAMES,
transformMinimalEvent,
} from '@openpanel/db';
import { redis, redisPub, redisSub } from '@openpanel/redis';
import { getRedisCache, getRedisPub, getRedisSub } from '@openpanel/redis';
import { getProjectAccess } from '@openpanel/trpc';
export function getLiveEventInfo(key: string) {
@@ -36,8 +35,8 @@ export async function testVisitors(
return reply.status(404).send('No event found');
}
event.projectId = req.params.projectId;
redisPub.publish('event:received', superjson.stringify(event));
redis.set(
getRedisPub().publish('event:received', superjson.stringify(event));
getRedisCache().set(
`live:event:${event.projectId}:${Math.random() * 1000}`,
'',
'EX',
@@ -61,7 +60,7 @@ export async function testEvents(
if (!event) {
return reply.status(404).send('No event found');
}
redisPub.publish('event:saved', superjson.stringify(event));
getRedisPub().publish('event:saved', superjson.stringify(event));
reply.status(202).send(event);
}
@@ -77,8 +76,8 @@ export function wsVisitors(
) {
const { params } = req;
redisSub.subscribe('event:received');
redisSub.psubscribe('__key*:expired');
getRedisSub().subscribe('event:received');
getRedisSub().psubscribe('__key*:expired');
const message = (channel: string, message: string) => {
if (channel === 'event:received') {
@@ -99,14 +98,14 @@ export function wsVisitors(
}
};
redisSub.on('message', message);
redisSub.on('pmessage', pmessage);
getRedisSub().on('message', message);
getRedisSub().on('pmessage', pmessage);
connection.socket.on('close', () => {
redisSub.unsubscribe('event:saved');
redisSub.punsubscribe('__key*:expired');
redisSub.off('message', message);
redisSub.off('pmessage', pmessage);
getRedisSub().unsubscribe('event:saved');
getRedisSub().punsubscribe('__key*:expired');
getRedisSub().off('message', message);
getRedisSub().off('pmessage', pmessage);
});
}
@@ -140,7 +139,7 @@ export async function wsProjectEvents(
projectId: params.projectId,
});
redisSub.subscribe(subscribeToEvent);
getRedisSub().subscribe(subscribeToEvent);
const message = async (channel: string, message: string) => {
const event = getSuperJson<IServiceCreateEventPayload>(message);
@@ -159,10 +158,10 @@ export async function wsProjectEvents(
}
};
redisSub.on('message', message as any);
getRedisSub().on('message', message as any);
connection.socket.on('close', () => {
redisSub.unsubscribe(subscribeToEvent);
redisSub.off('message', message as any);
getRedisSub().unsubscribe(subscribeToEvent);
getRedisSub().off('message', message as any);
});
}

View File

@@ -5,7 +5,7 @@ import icoToPng from 'ico-to-png';
import sharp from 'sharp';
import { createHash } from '@openpanel/common';
import { redis } from '@openpanel/redis';
import { getRedisCache } from '@openpanel/redis';
interface GetFaviconParams {
url: string;
@@ -51,7 +51,7 @@ export async function getFavicon(
) {
function sendBuffer(buffer: Buffer, cacheKey?: string) {
if (cacheKey) {
redis.set(`favicon:${cacheKey}`, buffer.toString('base64'));
getRedisCache().set(`favicon:${cacheKey}`, buffer.toString('base64'));
}
reply.type('image/png');
return reply.send(buffer);
@@ -65,7 +65,7 @@ export async function getFavicon(
if (imageExtensions.find((ext) => url.endsWith(ext))) {
const cacheKey = createHash(url, 32);
const cache = await redis.get(`favicon:${cacheKey}`);
const cache = await getRedisCache().get(`favicon:${cacheKey}`);
if (cache) {
return sendBuffer(Buffer.from(cache, 'base64'));
}
@@ -76,7 +76,7 @@ export async function getFavicon(
}
const { hostname } = new URL(url);
const cache = await redis.get(`favicon:${hostname}`);
const cache = await getRedisCache().get(`favicon:${hostname}`);
if (cache) {
return sendBuffer(Buffer.from(cache, 'base64'));
@@ -104,9 +104,9 @@ export async function clearFavicons(
request: FastifyRequest,
reply: FastifyReply
) {
const keys = await redis.keys('favicon:*');
const keys = await getRedisCache().keys('favicon:*');
for (const key of keys) {
await redis.del(key);
await getRedisCache().del(key);
}
return reply.status(404).send('OK');
}

View File

@@ -10,7 +10,7 @@ import { round } from '@openpanel/common';
import { chQuery, db, TABLE_NAMES } from '@openpanel/db';
import type { IServiceClient } from '@openpanel/db';
import { eventsQueue } from '@openpanel/queue';
import { redis, redisPub } from '@openpanel/redis';
import { getRedisCache, getRedisPub } from '@openpanel/redis';
import type { AppRouter } from '@openpanel/trpc';
import { appRouter, createContext } from '@openpanel/trpc';
@@ -98,7 +98,7 @@ const startServer = async () => {
reply.send({ name: 'openpanel sdk api' });
});
fastify.get('/healthcheck', async (request, reply) => {
const redisRes = await withTimings(redis.keys('*'));
const redisRes = await withTimings(getRedisCache().keys('*'));
const dbRes = await withTimings(db.project.findFirst());
const queueRes = await withTimings(eventsQueue.getCompleted());
const chRes = await withTimings(
@@ -150,7 +150,7 @@ const startServer = async () => {
});
// Notify when keys expires
redisPub.config('SET', 'notify-keyspace-events', 'Ex');
getRedisPub().config('SET', 'notify-keyspace-events', 'Ex');
} catch (e) {
logger.error(e, 'Failed to start server');
}

View File

@@ -3,11 +3,11 @@ import { escape } from 'sqlstring';
import type { IClickhouseEvent } from '@openpanel/db';
import { chQuery, eventBuffer, TABLE_NAMES } from '@openpanel/db';
import { sessionsQueue } from '@openpanel/queue/src/queues';
import { redis } from '@openpanel/redis';
import { getRedisCache, getRedisQueue } from '@openpanel/redis';
async function debugStalledEvents() {
const keys = await redis.keys('bull:sessions:sessionEnd*');
const delayedZRange = await redis.zrange(
const keys = await getRedisQueue().keys('bull:sessions:sessionEnd*');
const delayedZRange = await getRedisQueue().zrange(
'bull:sessions:delayed',
0,
-1,
@@ -20,10 +20,14 @@ async function debugStalledEvents() {
}
return acc;
},
[] as Record<string, number>
{} as Record<string, number>
);
const opKeys = await getRedisCache().keys('op:*');
const stalledEvents = await getRedisCache().lrange(
'op:buffer:events:stalled',
0,
-1
);
const opKeys = await redis.keys('op:*');
const stalledEvents = await redis.lrange('op:buffer:events:stalled', 0, -1);
// keys.forEach((key) => {
// console.log(key);
// });
@@ -112,7 +116,14 @@ async function debugStalledEvents() {
delayedJobs.sort((a, b) => a.timestamp + a.delay - (b.timestamp + b.delay));
let delayedJobsCount = 0;
delayedJobs.forEach((job) => {
const date = new Date(delayedValues[job.id]);
if (!job.id) {
return;
}
const timestamp = delayedValues[job.id];
if (!timestamp) {
return;
}
const date = new Date(timestamp);
// if date is in the past
// if (date.getTime() - 1000 * 60 * 5 < Date.now()) {
if (date.getTime() < Date.now()) {

View File

@@ -5,12 +5,8 @@ import type { WorkerOptions } from 'bullmq';
import { Worker } from 'bullmq';
import express from 'express';
import {
connection,
cronQueue,
eventsQueue,
sessionsQueue,
} from '@openpanel/queue';
import { cronQueue, eventsQueue, sessionsQueue } from '@openpanel/queue';
import { getRedisQueue } from '@openpanel/redis';
import { cronJob } from './jobs/cron';
import { eventsJob } from './jobs/events';
@@ -23,12 +19,7 @@ serverAdapter.setBasePath(process.env.SELF_HOSTED ? '/worker' : '/');
const app = express();
const workerOptions: WorkerOptions = {
connection: {
...connection,
enableReadyCheck: false,
maxRetriesPerRequest: null,
enableOfflineQueue: true,
},
connection: getRedisQueue(),
concurrency: parseInt(process.env.CONCURRENCY || '1', 10),
};

View File

@@ -14,7 +14,7 @@ import type {
EventsQueuePayloadCreateSessionEnd,
EventsQueuePayloadIncomingEvent,
} from '@openpanel/queue';
import { redis } from '@openpanel/redis';
import { getRedisQueue } from '@openpanel/redis';
function noDateInFuture(eventDate: Date): Date {
if (eventDate > new Date()) {
@@ -217,7 +217,9 @@ async function getSessionEnd({
currentDeviceId: string;
previousDeviceId: string;
}) {
const sessionEndKeys = await redis.keys(`*:sessionEnd:${projectId}:*`);
const sessionEndKeys = await getRedisQueue().keys(
`*:sessionEnd:${projectId}:*`
);
const sessionEndJobCurrentDeviceId = await findJobByPrefix(
sessionsQueue,

View File

@@ -7,7 +7,6 @@ import type {
EventsQueuePayloadCreateSessionEnd,
EventsQueuePayloadIncomingEvent,
} from '@openpanel/queue';
import { redis } from '@openpanel/redis';
import { createSessionEnd } from './events.create-session-end';
import { incomingEvent } from './events.incoming-event';

View File

@@ -1,7 +1,7 @@
import client from 'prom-client';
import { cronQueue, eventsQueue, sessionsQueue } from '@openpanel/queue';
import { redis } from '@openpanel/redis';
import { getRedisCache } from '@openpanel/redis';
const Registry = client.Registry;
@@ -75,7 +75,7 @@ buffers.forEach((buffer) => {
name: `buffer_${buffer}_count`,
help: 'Number of users in the users array',
async collect() {
const metric = await redis.llen(`op:buffer:${buffer}`);
const metric = await getRedisCache().llen(`op:buffer:${buffer}`);
this.set(metric);
},
})
@@ -86,7 +86,9 @@ buffers.forEach((buffer) => {
name: `buffer_${buffer}_stalled_count`,
help: 'Number of users in the users array',
async collect() {
const metric = await redis.llen(`op:buffer:${buffer}:stalled`);
const metric = await getRedisCache().llen(
`op:buffer:${buffer}:stalled`
);
this.set(metric);
},
})