Skip to content
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

Merged
merged 16 commits into from
Sep 20, 2023
Merged

Logger status #33

merged 16 commits into from
Sep 20, 2023

Conversation

idan-orbs
Copy link
Collaborator

No description provided.

const serviceLaunchTime = Math.round(new Date().getTime() / 1000);
const statusFilePath = process.env.STATUS_FILE_PATH || "/opt/orbs/status/status.json";

let error = '';
Copy link
Member

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}`);
});

Copy link
Member

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

Copy link
Collaborator Author

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?

@@ -1,9 +1,21 @@
import express, { Express, NextFunction, Request, Response } from "express";
import { request, ClientRequest, RequestOptions, IncomingMessage } from "http";
import {writeStatusToDisk} from "./status";
Copy link
Member

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

} catch (err: any) {
console.error(`Could not find version: ${err.message}`);
}
return '';
Copy link
Member

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() {
Copy link
Member

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/

Copy link
Collaborator Author

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

@idan-orbs idan-orbs merged commit 3dd307a into main Sep 20, 2023
2 checks passed
@idan-orbs idan-orbs deleted the logger_status branch September 20, 2023 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants