Skip to content

Commit

Permalink
fix: verify if file exist before get stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed Jan 1, 2025
1 parent 8e2e476 commit 09fa0ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,7 @@ export const SetupMonitoring = ({ serverId }: Props) => {
...(!form.watch("excludedServices")?.includes("*") ? ["*"] : []),
];

const { mutateAsync } = api.server.setupMonitoring.useMutation({
onSuccess: () => {
toast.success("Server updated successfully");
},
});
const { mutateAsync } = api.server.setupMonitoring.useMutation();

const onSubmit = async (values: Schema) => {
await mutateAsync({
Expand Down
24 changes: 13 additions & 11 deletions apps/monitoring/src/containers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ export const logContainerMetrics = () => {

const { maxFileSizeMB = 10 } = containerConfig;

const stats = await fs.promises.stat(containerPath);
const fileSizeInMB = stats.size / (1024 * 1024);

console.log(fileSizeInMB);
if (fileSizeInMB >= maxFileSizeMB) {
const fileContent = fs.readFileSync(containerPath, "utf-8");
const lines = fileContent.split("\n").filter((line) => line.trim());

const linesToKeep = Math.floor(lines.length / 2);
const newContent = `${lines.slice(-linesToKeep).join("\n")}\n`;
fs.writeFileSync(containerPath, newContent);
if (fs.existsSync(containerPath)) {
const stats = await fs.promises.stat(containerPath);
const fileSizeInMB = stats.size / (1024 * 1024);
if (fileSizeInMB >= maxFileSizeMB) {
const fileContent = fs.readFileSync(containerPath, "utf-8");
const lines = fileContent
.split("\n")
.filter((line) => line.trim());

const linesToKeep = Math.floor(lines.length / 2);
const newContent = `${lines.slice(-linesToKeep).join("\n")}\n`;
fs.writeFileSync(containerPath, newContent);
}
}

await fs.promises.appendFile(containerPath, logLine);
Expand Down

0 comments on commit 09fa0ed

Please sign in to comment.