Skip to content

Commit

Permalink
Merge pull request #54 from ZyrenthDevelopment/backend
Browse files Browse the repository at this point in the history
backend updates
  • Loading branch information
Alexejhero authored Jun 15, 2024
2 parents e65d3ac + eb7529a commit e1489e7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
50 changes: 49 additions & 1 deletion ebs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import expressWs from "express-ws";
import bodyParser from "body-parser";
import { privateApiAuth, publicApiAuth } from "./util/middleware";
import { initDb } from "./util/db";
import { sendToLogger } from "./util/logger";

dotenv();

Expand All @@ -30,7 +31,54 @@ async function main() {
require("./modules/transactions");
require("./modules/game");

const { setIngame } = require("./modules/config");

process.stdin.resume();

["exit", "SIGINT", "SIGTERM"].forEach((signal) =>
process.on(signal, () => {
try {
console.log("Exiting...");

setIngame(false);

// Give the pubsub some time to broadcast the new config.
setTimeout(() => {
process.exit(0);
}, 5_000);
} catch (err) {
console.error("Error while exiting:", err);
process.exit(1);
}
})
);

["unhandledRejection", "uncaughtException"].forEach((event) =>
process.on(event, (err) => {
try {
console.error("Unhandled error:", err);

sendToLogger({
transactionToken: null,
userIdInsecure: null,
important: true,
fields: [
{
header: "Unhandled error/exception:",
content: err?.stack ?? err,
},
],
}).then();

// Exit and hope that Docker will auto-restart the container.
process.kill(process.pid, "SIGTERM");
} catch (err) {
console.error("Error while error handling, mhm:", err);
process.exit(1);
}
})
);
});
}

main().catch(console.error);
main().catch(console.error);
4 changes: 2 additions & 2 deletions ebs/src/util/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function asyncCatch(fn: (req: Request, res: Response, next: NextFunction)
return async (req: Request, res: Response, next: NextFunction) => {
try {
await fn(req, res, next);
} catch (err) {
} catch (err: any) {
console.log(err);

sendToLogger({
Expand All @@ -70,7 +70,7 @@ export function asyncCatch(fn: (req: Request, res: Response, next: NextFunction)
fields: [
{
header: "Error in asyncCatch",
content: err,
content: err?.stack ?? err,
},
],
}).then();
Expand Down

0 comments on commit e1489e7

Please sign in to comment.