mirror of
https://github.com/20kaushik02/spotify-manager.git
synced 2026-01-25 14:14:06 +00:00
top level await, redis error handling, log format
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -103,8 +103,9 @@ dist
|
|||||||
# TernJS port file
|
# TernJS port file
|
||||||
.tern-port
|
.tern-port
|
||||||
|
|
||||||
# SQLite db
|
# Ephemeral databases
|
||||||
*.db
|
*.db
|
||||||
|
*.rdb
|
||||||
|
|
||||||
# production
|
# production
|
||||||
/build
|
/build
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { createClient } from "redis";
|
import { createClient } from "redis";
|
||||||
|
|
||||||
import { sleep } from "../utils/flake.ts";
|
|
||||||
import logger from "../utils/logger.ts";
|
import logger from "../utils/logger.ts";
|
||||||
|
|
||||||
if (!process.env["REDIS_URI"])
|
if (!process.env["REDIS_URI"])
|
||||||
@@ -9,18 +8,16 @@ if (!process.env["REDIS_URI"])
|
|||||||
// Initialize
|
// Initialize
|
||||||
const redisClient: ReturnType<typeof createClient> = createClient({
|
const redisClient: ReturnType<typeof createClient> = createClient({
|
||||||
url: process.env["REDIS_URI"],
|
url: process.env["REDIS_URI"],
|
||||||
|
socket: {
|
||||||
|
keepAlive: 25 * 1000, // 25s
|
||||||
|
},
|
||||||
|
});
|
||||||
|
redisClient.on("error", (error) => {
|
||||||
|
logger.error("redisClient", { error });
|
||||||
|
throw error;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check connection
|
await redisClient.connect();
|
||||||
(async () => {
|
logger.info("Connected to Redis store");
|
||||||
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;
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
export { redisClient };
|
export { redisClient };
|
||||||
|
|||||||
3
index.ts
3
index.ts
@@ -13,7 +13,6 @@ import { RedisStore } from "connect-redis";
|
|||||||
import { redisClient } from "./config/redis.ts";
|
import { redisClient } from "./config/redis.ts";
|
||||||
|
|
||||||
import { sessionName } from "./constants.ts";
|
import { sessionName } from "./constants.ts";
|
||||||
import seqConn from "./models/index.ts";
|
|
||||||
|
|
||||||
import { isAuthenticated } from "./middleware/authCheck.ts";
|
import { isAuthenticated } from "./middleware/authCheck.ts";
|
||||||
import { getCurrentUsersProfile } from "./api/spotify.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...`);
|
if (signal) logger.debug(`${signal} signal received, shutting down now...`);
|
||||||
|
|
||||||
Promise.allSettled([
|
Promise.allSettled([
|
||||||
redisClient.disconnect,
|
|
||||||
seqConn.close,
|
|
||||||
promisify(server.close),
|
promisify(server.close),
|
||||||
]).then(() => {
|
]).then(() => {
|
||||||
logger.info("Cleaned up, exiting.");
|
logger.info("Cleaned up, exiting.");
|
||||||
|
|||||||
@@ -17,16 +17,13 @@ if (!process.env["DB_URI"])
|
|||||||
const config = seqConfig[process.env["NODE_ENV"]];
|
const config = seqConfig[process.env["NODE_ENV"]];
|
||||||
const seqConn: Sequelize = new Sequelize(process.env["DB_URI"], config);
|
const seqConn: Sequelize = new Sequelize(process.env["DB_URI"], config);
|
||||||
|
|
||||||
// Check connection
|
try {
|
||||||
(async () => {
|
|
||||||
try {
|
|
||||||
await seqConn.authenticate();
|
await seqConn.authenticate();
|
||||||
logger.info("Sequelize auth success");
|
logger.info("Sequelize auth success");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Sequelize auth error", { error });
|
logger.error("Sequelize auth error", { error });
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
})();
|
|
||||||
|
|
||||||
// Load models
|
// Load models
|
||||||
seqConn.addModels([links, playlists]);
|
seqConn.addModels([links, playlists]);
|
||||||
|
|||||||
@@ -29,9 +29,9 @@ const logFormat = printf(({ level, message, label, timestamp, ...meta }) => {
|
|||||||
const errorObj: Error = meta["error"] as Error;
|
const errorObj: Error = meta["error"] as Error;
|
||||||
if (errorObj) {
|
if (errorObj) {
|
||||||
return (
|
return (
|
||||||
`${timestamp} [${level.toUpperCase()}]: ${message}${metaFormat(
|
`${timestamp} [${level.toUpperCase()}]: ${message}` + // line 1
|
||||||
errorObj
|
`${metaFormat(errorObj)}\n` + // metadata
|
||||||
)}\n` + `${errorObj["stack"] ?? ""}`
|
`${errorObj["stack"] ?? ""}` // stack trace if any
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return `${timestamp} [${level.toUpperCase()}]: ${message}${metaFormat(meta)}`;
|
return `${timestamp} [${level.toUpperCase()}]: ${message}${metaFormat(meta)}`;
|
||||||
|
|||||||
Reference in New Issue
Block a user