-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logger status #33
Logger status #33
Conversation
logging/src/main.ts
Outdated
const serviceLaunchTime = Math.round(new Date().getTime() / 1000); | ||
const statusFilePath = process.env.STATUS_FILE_PATH || "/opt/orbs/status/status.json"; | ||
|
||
let error = ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I asked ChatGPT for advice about how we can avoid this "global" error variable and it suggested this:
import express, { Express, NextFunction, Request, Response } from "express";
import { request, ClientRequest, RequestOptions, IncomingMessage } from "http";
import { writeStatusToDisk } from "./status";
const app: Express = express();
const port: number = 80;
const serviceLaunchTime = Math.round(new Date().getTime() / 1000);
const statusFilePath = process.env.STATUS_FILE_PATH || "/opt/orbs/status/status.json";
setInterval(() => {
writeStatusToDisk(statusFilePath, serviceLaunchTime);
}, 5 * 60 * 1000);
const validNameRegex = /^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/;
app.use((_: Request, res: Response, next: NextFunction) => {
res.setHeader("Content-Disposition", "inline");
res.setHeader("Content-Type", "text/plain; charset=utf-8");
next();
});
const decodeDockerLogs = (data: Buffer): string => {
// ... [no change in this function]
};
app.get("/service/:name/log", (req: Request, res: Response, next: NextFunction) => {
const containerName: string = req.params.name;
if (!validNameRegex.test(containerName)) {
return next(new Error("Invalid container name"));
}
const options: RequestOptions = {
// ... [no change in this part]
};
const clientRequest: ClientRequest = request(options, (resp: IncomingMessage) => {
// ... [no change in this function]
});
clientRequest.on("error", (e: Error) => {
next(e); // Pass the error to the error handling middleware
});
clientRequest.end();
});
// Dedicated error handling middleware
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error(err.message);
writeStatusToDisk(statusFilePath, serviceLaunchTime, err.message); // Log the error
res.status(500).send("An unexpected error occurred. Try again later");
});
app.listen(port, () => {
console.log(`Logging service listening at http://localhost:${port}`);
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think? I do like that it centralises the whole error handling logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So how does it work? app.use
is handling the errors, and next
is in charge of surfacing them?
logging/src/main.ts
Outdated
@@ -1,9 +1,21 @@ | |||
import express, { Express, NextFunction, Request, Response } from "express"; | |||
import { request, ClientRequest, RequestOptions, IncomingMessage } from "http"; | |||
import {writeStatusToDisk} from "./status"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I be annoying and ask you to run Prettier on this file? Formatting is quite different in places
logging/src/status.ts
Outdated
} catch (err: any) { | ||
console.error(`Could not find version: ${err.message}`); | ||
} | ||
return ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just return something like "Version not found" for clarity?
@@ -1,14 +1,34 @@ | |||
#!/bin/bash | |||
|
|||
echo -e "${BLUE}Performing a health check...${NC}\n" | |||
check_services() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also look at using docker-compose -d --wait
in the future - https://maciejwalkowiak.com/blog/docker-compose-waiting-containers-ready/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, wasn't familiar.
I tried it now locally but it doesn't work. Probably b/c services are stuck on "starting" and not "healthy".
Creating a follow-up ticket
No description provided.