Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Review fixes

Blue Mouse0ba0a31dcbb0
diff --git a/backend/apps/cloud/src/demo-data/demo-data.service.ts b/backend/apps/cloud/src/demo-data/demo-data.service.ts
index aafbd7e62..e0703a0e1 100644
--- a/backend/apps/cloud/src/demo-data/demo-data.service.ts
+++ b/backend/apps/cloud/src/demo-data/demo-data.service.ts
@@ -39,6 +39,7 @@ const DEMO_PROJECT_ID = 'DEMODEMODEMO'
const LOCK_KEY = 'demo-data:lock'
const LAST_GENERATED_AT_KEY = 'demo-data:last-generated-at'
const LOCK_TTL_SECONDS = 20 * 60
+const LOCK_RENEW_INTERVAL_MS = Math.floor((LOCK_TTL_SECONDS * 1000) / 3)
const BACKFILL_DAYS = 90
const DEMO_HOST = 'swetrix.com'
@@ -818,19 +819,25 @@ export class DemoDataService implements OnModuleInit {
}
const token = randomUUID()
- const hasLock = await redis.set(
- LOCK_KEY,
- token,
- 'EX',
- LOCK_TTL_SECONDS,
- 'NX',
- )
-
- if (hasLock !== 'OK') {
- return
- }
+ let hasLock = false
+ let lockHeartbeat: ReturnType<typeof setInterval> | undefined
try {
+ const lockResult = await redis.set(
+ LOCK_KEY,
+ token,
+ 'EX',
+ LOCK_TTL_SECONDS,
+ 'NX',
+ )
+
+ if (lockResult !== 'OK') {
+ return
+ }
+
+ hasLock = true
+ lockHeartbeat = this.startLockHeartbeat(token)
+
const project = await this.getDemoProject()
if (!project) {
@@ -867,7 +874,13 @@ export class DemoDataService implements OnModuleInit {
reason instanceof Error ? reason.stack : String(reason),
)
} finally {
- await this.releaseLock(token)
+ if (lockHeartbeat) {
+ clearInterval(lockHeartbeat)
+ }
+
+ if (hasLock) {
+ await this.releaseLock(token)
+ }
}
}
@@ -2168,6 +2181,30 @@ export class DemoDataService implements OnModuleInit {
}
}
+ private startLockHeartbeat(token: string) {
+ return setInterval(() => {
+ void this.refreshLock(token)
+ }, LOCK_RENEW_INTERVAL_MS)
+ }
+
+ private async refreshLock(token: string) {
+ try {
+ const renewed = await redis.eval(
+ "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('EXPIRE', KEYS[1], ARGV[2]) else return 0 end",
+ 1,
+ LOCK_KEY,
+ token,
+ LOCK_TTL_SECONDS,
+ )
+
+ if (Number(renewed) !== 1) {
+ this.logger.warn('Demo data lock is no longer owned by this process')
+ }
+ } catch (reason) {
+ this.logger.warn(`Unable to refresh demo data lock: ${reason}`)
+ }
+ }
+
private async releaseLock(token: string) {
try {
await redis.eval(
diff --git a/web/app/routes/projects.$id.tsx b/web/app/routes/projects.$id.tsx
index 6652f766d..d64445824 100644
--- a/web/app/routes/projects.$id.tsx
+++ b/web/app/routes/projects.$id.tsx
@@ -527,6 +527,11 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
return redirect('/dashboard')
}
+ // legacy live demo redirect
+ if (projectId === 'STEzHcB1rALV') {
+ return redirect('/demo')
+ }
+
return data<ProjectLoaderData>(
{
project: null,