mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2025-12-06 06:44:07 +00:00
top level await, redis error handling, log format
This commit is contained in:
parent
04e1ba3804
commit
ca1ad74834
3
.gitignore
vendored
3
.gitignore
vendored
@ -103,8 +103,9 @@ dist
|
||||
# TernJS port file
|
||||
.tern-port
|
||||
|
||||
# SQLite db
|
||||
# Ephemeral databases
|
||||
*.db
|
||||
*.rdb
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
@ -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 };
|
||||
|
||||
3
index.ts
3
index.ts
@ -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.");
|
||||
|
||||
@ -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]);
|
||||
|
||||
@ -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)}`;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user