top level await, redis error handling, log format

This commit is contained in:
Kaushik Narayan R 2025-03-12 19:08:20 -07:00
parent 04e1ba3804
commit ca1ad74834
5 changed files with 21 additions and 29 deletions

3
.gitignore vendored
View File

@ -103,8 +103,9 @@ dist
# TernJS port file
.tern-port
# SQLite db
# Ephemeral databases
*.db
*.rdb
# production
/build

View File

@ -1,6 +1,5 @@
import { createClient } from "redis";
import { sleep } from "../utils/flake.ts";
import logger from "../utils/logger.ts";
if (!process.env["REDIS_URI"])
@ -9,18 +8,16 @@ if (!process.env["REDIS_URI"])
// Initialize
const redisClient: ReturnType<typeof createClient> = createClient({
url: process.env["REDIS_URI"],
socket: {
keepAlive: 25 * 1000, // 25s
},
});
redisClient.on("error", (error) => {
logger.error("redisClient", { error });
throw error;
});
// Check connection
(async () => {
try {
await redisClient.connect();
while (!redisClient.isReady) await sleep(100);
logger.info("Connected to Redis store");
} catch (error) {
logger.error("Redis connection error", { error });
throw error;
}
})();
await redisClient.connect();
logger.info("Connected to Redis store");
export { redisClient };

View File

@ -13,7 +13,6 @@ import { RedisStore } from "connect-redis";
import { redisClient } from "./config/redis.ts";
import { sessionName } from "./constants.ts";
import seqConn from "./models/index.ts";
import { isAuthenticated } from "./middleware/authCheck.ts";
import { getCurrentUsersProfile } from "./api/spotify.ts";
@ -129,8 +128,6 @@ const cleanupFunc = (signal?: string) => {
if (signal) logger.debug(`${signal} signal received, shutting down now...`);
Promise.allSettled([
redisClient.disconnect,
seqConn.close,
promisify(server.close),
]).then(() => {
logger.info("Cleaned up, exiting.");

View File

@ -17,16 +17,13 @@ if (!process.env["DB_URI"])
const config = seqConfig[process.env["NODE_ENV"]];
const seqConn: Sequelize = new Sequelize(process.env["DB_URI"], config);
// Check connection
(async () => {
try {
await seqConn.authenticate();
logger.info("Sequelize auth success");
} catch (error) {
logger.error("Sequelize auth error", { error });
throw error;
}
})();
try {
await seqConn.authenticate();
logger.info("Sequelize auth success");
} catch (error) {
logger.error("Sequelize auth error", { error });
throw error;
}
// Load models
seqConn.addModels([links, playlists]);

View File

@ -29,9 +29,9 @@ const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
const errorObj: Error = meta["error"] as Error;
if (errorObj) {
return (
`${timestamp} [${level.toUpperCase()}]: ${message}${metaFormat(
errorObj
)}\n` + `${errorObj["stack"] ?? ""}`
`${timestamp} [${level.toUpperCase()}]: ${message}` + // line 1
`${metaFormat(errorObj)}\n` + // metadata
`${errorObj["stack"] ?? ""}` // stack trace if any
);
}
return `${timestamp} [${level.toUpperCase()}]: ${message}${metaFormat(meta)}`;