mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
Merge pull request #423 from Dokploy/refactor/requests
Refactor/requests
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { DialogAction } from "@/components/shared/dialog-action";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import {
|
import {
|
||||||
Card,
|
Card,
|
||||||
@@ -20,9 +21,13 @@ export const ShowRequests = () => {
|
|||||||
const { data: isLogRotateActive, refetch: refetchLogRotate } =
|
const { data: isLogRotateActive, refetch: refetchLogRotate } =
|
||||||
api.settings.getLogRotateStatus.useQuery();
|
api.settings.getLogRotateStatus.useQuery();
|
||||||
|
|
||||||
const { mutateAsync } = api.settings.activateLogRotate.useMutation();
|
const { mutateAsync: toggleLogRotate } =
|
||||||
const { mutateAsync: deactivateLogRotate } =
|
api.settings.toggleLogRotate.useMutation();
|
||||||
api.settings.deactivateLogRotate.useMutation();
|
|
||||||
|
const { data: isActive, refetch } =
|
||||||
|
api.settings.haveActivateRequests.useQuery();
|
||||||
|
const { mutateAsync: toggleRequests } =
|
||||||
|
api.settings.toggleRequests.useMutation();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -33,29 +38,45 @@ export const ShowRequests = () => {
|
|||||||
<CardDescription>
|
<CardDescription>
|
||||||
<span>Showing web and API requests over time</span>
|
<span>Showing web and API requests over time</span>
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
{!isLogRotateActive && (
|
<div className="flex w-fit gap-4">
|
||||||
<Button
|
<DialogAction
|
||||||
onClick={() => {
|
title={isActive ? "Deactivate Requests" : "Activate Requests"}
|
||||||
mutateAsync()
|
description="You will also need to restart Traefik to apply the changes"
|
||||||
|
onClick={async () => {
|
||||||
|
await toggleRequests({ enable: !isActive })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
toast.success("Log rotate activated");
|
refetch();
|
||||||
refetchLogRotate();
|
toast.success(
|
||||||
|
`Requests ${isActive ? "deactivated" : "activated"}`,
|
||||||
|
);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
toast.error(err.message);
|
toast.error(err.message);
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Activate Log Rotate
|
<Button>{isActive ? "Deactivate" : "Activate"}</Button>
|
||||||
</Button>
|
</DialogAction>
|
||||||
)}
|
|
||||||
|
|
||||||
{isLogRotateActive && (
|
<DialogAction
|
||||||
<Button
|
title={
|
||||||
|
isLogRotateActive
|
||||||
|
? "Activate Log Rotate"
|
||||||
|
: "Deactivate Log Rotate"
|
||||||
|
}
|
||||||
|
description={
|
||||||
|
isLogRotateActive
|
||||||
|
? "This will make the logs rotate on interval 1 day and maximum size of 100 MB and maximum 6 logs"
|
||||||
|
: "The log rotation will be disabled"
|
||||||
|
}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
deactivateLogRotate()
|
toggleLogRotate({
|
||||||
|
enable: !isLogRotateActive,
|
||||||
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
toast.success("Log rotate deactivated");
|
toast.success(
|
||||||
|
`Log rotate ${isLogRotateActive ? "activated" : "deactivated"}`,
|
||||||
|
);
|
||||||
refetchLogRotate();
|
refetchLogRotate();
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
@@ -63,9 +84,13 @@ export const ShowRequests = () => {
|
|||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Deactivate Log Rotate
|
<Button variant="secondary">
|
||||||
</Button>
|
{isLogRotateActive
|
||||||
)}
|
? "Activate Log Rotate"
|
||||||
|
: "Deactivate Log Rotate"}
|
||||||
|
</Button>
|
||||||
|
</DialogAction>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
|
|||||||
45
apps/dokploy/components/shared/dialog-action.tsx
Normal file
45
apps/dokploy/components/shared/dialog-action.tsx
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogAction,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogHeader,
|
||||||
|
AlertDialogTitle,
|
||||||
|
AlertDialogTrigger,
|
||||||
|
} from "@/components/ui/alert-dialog";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
title?: string;
|
||||||
|
description?: string;
|
||||||
|
onClick: () => void;
|
||||||
|
children?: React.ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DialogAction = ({
|
||||||
|
onClick,
|
||||||
|
children,
|
||||||
|
description,
|
||||||
|
title,
|
||||||
|
}: Props) => {
|
||||||
|
return (
|
||||||
|
<AlertDialog>
|
||||||
|
<AlertDialogTrigger asChild>{children}</AlertDialogTrigger>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>
|
||||||
|
{title ?? "Are you absolutely sure?"}
|
||||||
|
</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription>
|
||||||
|
{description ?? "This action cannot be undone."}
|
||||||
|
</AlertDialogDescription>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||||
|
<AlertDialogAction onClick={onClick}>Confirm</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -41,6 +41,7 @@ import {
|
|||||||
} from "@/server/utils/traefik/web-server";
|
} from "@/server/utils/traefik/web-server";
|
||||||
import { generateOpenApiDocument } from "@dokploy/trpc-openapi";
|
import { generateOpenApiDocument } from "@dokploy/trpc-openapi";
|
||||||
import { TRPCError } from "@trpc/server";
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import { dump, load } from "js-yaml";
|
||||||
import { scheduleJob, scheduledJobs } from "node-schedule";
|
import { scheduleJob, scheduledJobs } from "node-schedule";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { appRouter } from "../root";
|
import { appRouter } from "../root";
|
||||||
@@ -372,14 +373,71 @@ export const settingsRouter = createTRPCRouter({
|
|||||||
const processedLogs = processLogs(rawConfig as string);
|
const processedLogs = processLogs(rawConfig as string);
|
||||||
return processedLogs || [];
|
return processedLogs || [];
|
||||||
}),
|
}),
|
||||||
activateLogRotate: adminProcedure.mutation(async () => {
|
|
||||||
await logRotationManager.activate();
|
|
||||||
return true;
|
|
||||||
}),
|
|
||||||
deactivateLogRotate: adminProcedure.mutation(async () => {
|
|
||||||
return await logRotationManager.deactivate();
|
|
||||||
}),
|
|
||||||
getLogRotateStatus: adminProcedure.query(async () => {
|
getLogRotateStatus: adminProcedure.query(async () => {
|
||||||
return await logRotationManager.getStatus();
|
return await logRotationManager.getStatus();
|
||||||
}),
|
}),
|
||||||
|
toggleLogRotate: adminProcedure
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
enable: z.boolean(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.mutation(async ({ input }) => {
|
||||||
|
if (input.enable) {
|
||||||
|
await logRotationManager.activate();
|
||||||
|
} else {
|
||||||
|
await logRotationManager.deactivate();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}),
|
||||||
|
haveActivateRequests: adminProcedure.query(async () => {
|
||||||
|
const config = readMainConfig();
|
||||||
|
|
||||||
|
if (!config) return false;
|
||||||
|
const parsedConfig = load(config) as {
|
||||||
|
accessLog?: {
|
||||||
|
filePath: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return !!parsedConfig?.accessLog?.filePath;
|
||||||
|
}),
|
||||||
|
toggleRequests: adminProcedure
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
enable: z.boolean(),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.mutation(async ({ input }) => {
|
||||||
|
const mainConfig = readMainConfig();
|
||||||
|
if (!mainConfig) return false;
|
||||||
|
|
||||||
|
const currentConfig = load(mainConfig) as {
|
||||||
|
accessLog?: {
|
||||||
|
filePath: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
if (input.enable) {
|
||||||
|
const config = {
|
||||||
|
accessLog: {
|
||||||
|
filePath: "/etc/dokploy/traefik/dynamic/access.log",
|
||||||
|
format: "json",
|
||||||
|
bufferingSize: 100,
|
||||||
|
filters: {
|
||||||
|
retryAttempts: true,
|
||||||
|
minDuration: "10ms",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
currentConfig.accessLog = config.accessLog;
|
||||||
|
} else {
|
||||||
|
currentConfig.accessLog = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeMainConfig(dump(currentConfig));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -59,12 +59,6 @@ export const createApplication = async (
|
|||||||
|
|
||||||
if (process.env.NODE_ENV === "development") {
|
if (process.env.NODE_ENV === "development") {
|
||||||
createTraefikConfig(newApplication.appName);
|
createTraefikConfig(newApplication.appName);
|
||||||
await tx.insert(domains).values({
|
|
||||||
applicationId: newApplication.applicationId,
|
|
||||||
host: `${newApplication.appName}.docker.localhost`,
|
|
||||||
port: process.env.NODE_ENV === "development" ? 3000 : 80,
|
|
||||||
certificateType: "none",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return newApplication;
|
return newApplication;
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ class LogRotationManager {
|
|||||||
await this.deactivateStream();
|
await this.deactivateStream();
|
||||||
}
|
}
|
||||||
await execAsync(
|
await execAsync(
|
||||||
"docker kill -s USR1 $(docker ps -q --filter name=traefik)",
|
"docker kill -s USR1 $(docker ps -q --filter name=dokploy-traefik)",
|
||||||
);
|
);
|
||||||
console.log("USR1 Signal send to Traefik");
|
console.log("USR1 Signal send to Traefik");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ export function processLogs(logString: string): HourlyData[] {
|
|||||||
.map((entry) => {
|
.map((entry) => {
|
||||||
try {
|
try {
|
||||||
const log: LogEntry = JSON.parse(entry);
|
const log: LogEntry = JSON.parse(entry);
|
||||||
|
if (log.ServiceName === "dokploy-service-app@file") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
const date = new Date(log.StartUTC);
|
const date = new Date(log.StartUTC);
|
||||||
return `${date.toISOString().slice(0, 13)}:00:00Z`;
|
return `${date.toISOString().slice(0, 13)}:00:00Z`;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -89,6 +92,10 @@ export function parseRawConfig(
|
|||||||
parsedLogs = parsedLogs.slice(startIndex, startIndex + page.pageSize);
|
parsedLogs = parsedLogs.slice(startIndex, startIndex + page.pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parsedLogs = parsedLogs.filter(
|
||||||
|
(log) => log.ServiceName !== "dokploy-service-app@file",
|
||||||
|
);
|
||||||
|
|
||||||
return { data: parsedLogs, totalCount };
|
return { data: parsedLogs, totalCount };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error parsing rawConfig:", error);
|
console.error("Error parsing rawConfig:", error);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import fs, { writeFileSync } from "node:fs";
|
import fs, { writeFileSync } from "node:fs";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import type { Domain } from "@/server/api/services/domain";
|
import type { Domain } from "@/server/api/services/domain";
|
||||||
import { DYNAMIC_TRAEFIK_PATH } from "@/server/constants";
|
import { DYNAMIC_TRAEFIK_PATH, MAIN_TRAEFIK_PATH } from "@/server/constants";
|
||||||
import { dump, load } from "js-yaml";
|
import { dump, load } from "js-yaml";
|
||||||
import type { FileConfig, HttpLoadBalancerService } from "./file-types";
|
import type { FileConfig, HttpLoadBalancerService } from "./file-types";
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user