fix: enhance Redis connection handling and improve error logging in API startup

This commit is contained in:
Rotimi Best
2026-03-11 12:57:44 +01:00
parent ff1d4a2749
commit cd470b5d05
5 changed files with 34 additions and 12 deletions
+1 -2
View File
@@ -11,9 +11,8 @@ import { showRoutes } from 'hono/dev';
async function startServer() {
console.log('Starting server on port:', API_PORT);
// Connect to Redis before starting the server
// Connect to Redis (non-blocking: API starts even if Redis fails)
await connectRedis();
console.log('Redis connected successfully');
serve({ fetch: app.fetch, port: API_PORT });
+9 -1
View File
@@ -23,9 +23,17 @@ client.on('reconnecting', () => {
let isConnected = false;
export async function connectRedis(): Promise<void> {
if (!isConnected) {
if (!env.REDIS_URL) {
console.warn('REDIS_URL not set, Redis features disabled');
return;
}
if (isConnected) return;
try {
await client.connect();
isConnected = true;
} catch (error) {
console.error('Redis connection failed, API will run without Redis:', error);
// Don't throw - allow API to start; rate limiting and caching will degrade gracefully
}
}
@@ -1,6 +1,7 @@
import { getOrgBySiteName } from '$features/org/api/org.server';
import { getApiKeyHeaders } from '$lib/utils/services/api/server';
import { PUBLIC_IS_SELFHOSTED } from '$env/static/public';
import { env } from '$env/dynamic/private';
import { getApiKeyHeaders } from '$lib/utils/services/api/server';
import { getOrgBySiteName } from '$features/org/api/org.server';
import { getSubdomain } from '$features/app/layout-setup';
import { redirect } from '@sveltejs/kit';
@@ -12,7 +13,8 @@ export const load = async ({ params, url, cookies }) => {
const isOrgSite = subdomain && !APP_SUBDOMAINS.includes(subdomain);
// If this is LMS but user is on org site, redirect to LMS
if (isOrgSite) {
if (isOrgSite && PUBLIC_IS_SELFHOSTED !== 'true') {
console.log('isOrgSite redirecting to lms');
redirect(307, `/lms`);
}
@@ -20,11 +22,15 @@ export const load = async ({ params, url, cookies }) => {
const cookieKey = `${ORG_ID_COOKIE_PREFIX}${siteName}`;
const cachedOrgId = cookies.get(cookieKey);
console.log('cachedOrgId', cachedOrgId);
console.log('cookieKey', cookieKey);
let orgId: string | undefined = cachedOrgId;
if (!orgId) {
const apiKeyHeaders = getApiKeyHeaders();
const org = await getOrgBySiteName(siteName, apiKeyHeaders);
console.log('org', org);
if (org?.id) {
orgId = org.id;
@@ -37,6 +43,8 @@ export const load = async ({ params, url, cookies }) => {
}
}
console.log('returning orgId', orgId);
return {
orgName: siteName,
orgId
@@ -13,6 +13,9 @@ export const load = async ({ params, parent, cookies }) => {
stats: null
};
}
console.log('cache', cache);
console.log('orgId', orgId);
console.log('siteName', siteName);
if (cache[orgId]) {
return {
@@ -21,11 +24,15 @@ export const load = async ({ params, parent, cookies }) => {
};
}
// Fetch dashboard stats
const statsResponse = await classroomio.dash.stats.$get({ query: { siteName } }, getApiHeaders(cookies, orgId));
const statsData = await statsResponse.json();
cache[orgId] = statsData.success ? statsData.data : null;
try {
console.log('fetching stats', cookies);
const statsResponse = await classroomio.dash.stats.$get({ query: { siteName } }, getApiHeaders(cookies, orgId));
const statsData = await statsResponse.json();
cache[orgId] = statsData.success ? statsData.data : null;
} catch (error) {
console.error('Failed to fetch dashboard stats:', error);
cache[orgId] = null;
}
return {
orgName: siteName,
+1 -1
View File
@@ -11,7 +11,7 @@
"build": "rm -rf dist tsconfig.tsbuildinfo && tsc && tsc-alias",
"dev": "tsc --watch",
"db": "drizzle-kit",
"db:reset": "tsx src/scripts/db-reset.ts",
"db:reset": "tsx src/scripts/db-reset.ts --skip-seed",
"db:setup": "tsx src/scripts/db-setup.ts",
"db:setup:seed": "tsx src/scripts/db-setup.ts --seed",
"db:exercise-backfill": "tsx src/scripts/backfill-exercise-course.ts",