mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
Add schedule logs feature and enhance schedule management
- Introduced a new component `ShowSchedulesLogs` to display logs for each schedule, allowing users to view deployment logs associated with their schedules. - Updated the `ShowSchedules` component to integrate the new logs feature, providing a button to access logs for each schedule. - Enhanced the `schedule` schema by adding an `appName` column to better identify applications associated with schedules. - Updated the API to support fetching deployments with their associated schedules, improving data retrieval for the frontend. - Implemented utility functions for managing schedule-related operations, including creating and removing deployments linked to schedules.
This commit is contained in:
parent
442f051457
commit
f2bb01c800
@ -0,0 +1,99 @@
|
||||
import { DateTooltip } from "@/components/shared/date-tooltip";
|
||||
import { StatusTooltip } from "@/components/shared/status-tooltip";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog";
|
||||
|
||||
import type { RouterOutputs } from "@/utils/api";
|
||||
import { useState } from "react";
|
||||
import { ShowDeployment } from "../deployments/show-deployment";
|
||||
|
||||
interface Props {
|
||||
deployments: RouterOutputs["deployment"]["all"];
|
||||
serverId?: string;
|
||||
trigger?: React.ReactNode;
|
||||
}
|
||||
|
||||
export const ShowSchedulesLogs = ({
|
||||
deployments,
|
||||
serverId,
|
||||
trigger,
|
||||
}: Props) => {
|
||||
const [activeLog, setActiveLog] = useState<
|
||||
RouterOutputs["deployment"]["all"][number] | null
|
||||
>(null);
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
return (
|
||||
<Dialog open={isOpen} onOpenChange={setIsOpen}>
|
||||
<DialogTrigger asChild>
|
||||
{trigger ? (
|
||||
trigger
|
||||
) : (
|
||||
<Button className="sm:w-auto w-full" size="sm" variant="outline">
|
||||
View Logs
|
||||
</Button>
|
||||
)}
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-h-screen overflow-y-auto sm:max-w-5xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Logs</DialogTitle>
|
||||
<DialogDescription>
|
||||
See all the logs for this schedule
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="grid gap-4">
|
||||
{deployments?.map((deployment, index) => (
|
||||
<div
|
||||
key={deployment.deploymentId}
|
||||
className="flex items-center justify-between rounded-lg border p-4 gap-2"
|
||||
>
|
||||
<div className="flex flex-col">
|
||||
<span className="flex items-center gap-4 font-medium capitalize text-foreground">
|
||||
{index + 1} {deployment.status}
|
||||
<StatusTooltip
|
||||
status={deployment?.status}
|
||||
className="size-2.5"
|
||||
/>
|
||||
</span>
|
||||
<span className="text-sm text-muted-foreground">
|
||||
{deployment.title}
|
||||
</span>
|
||||
{deployment.description && (
|
||||
<span className="break-all text-sm text-muted-foreground">
|
||||
{deployment.description}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex flex-col items-end gap-2">
|
||||
<div className="text-sm capitalize text-muted-foreground">
|
||||
<DateTooltip date={deployment.createdAt} />
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={() => {
|
||||
setActiveLog(deployment);
|
||||
}}
|
||||
>
|
||||
View
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</DialogContent>
|
||||
<ShowDeployment
|
||||
serverId={serverId || ""}
|
||||
open={Boolean(activeLog && activeLog.logPath !== null)}
|
||||
onClose={() => setActiveLog(null)}
|
||||
logPath={activeLog?.logPath || ""}
|
||||
errorMessage={activeLog?.errorMessage || ""}
|
||||
/>
|
||||
</Dialog>
|
||||
);
|
||||
};
|
@ -26,6 +26,8 @@ import {
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { toast } from "sonner";
|
||||
import { ShowSchedulesLogs } from "./show-schedules-logs";
|
||||
|
||||
interface Props {
|
||||
applicationId: string;
|
||||
@ -50,6 +52,12 @@ export const ShowSchedules = ({ applicationId }: Props) => {
|
||||
},
|
||||
});
|
||||
|
||||
const { mutateAsync: runManually } = api.schedule.runManually.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.schedule.list.invalidate({ applicationId });
|
||||
},
|
||||
});
|
||||
|
||||
const utils = api.useContext();
|
||||
|
||||
const onClose = () => {
|
||||
@ -107,7 +115,10 @@ export const ShowSchedules = ({ applicationId }: Props) => {
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{schedules.map((schedule) => (
|
||||
{schedules.map((schedule) => {
|
||||
const application = schedule.application;
|
||||
const deployments = schedule.deployments;
|
||||
return (
|
||||
<TableRow key={schedule.scheduleId}>
|
||||
<TableCell className="font-medium">
|
||||
{schedule.name}
|
||||
@ -134,9 +145,32 @@ export const ShowSchedules = ({ applicationId }: Props) => {
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<div className="flex justify-end gap-2">
|
||||
<ShowSchedulesLogs
|
||||
deployments={deployments}
|
||||
serverId={application.serverId || undefined}
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={async () => {
|
||||
await runManually({
|
||||
scheduleId: schedule.scheduleId,
|
||||
})
|
||||
.then(() => {
|
||||
toast.success("Schedule run successfully");
|
||||
})
|
||||
.catch((error) => {
|
||||
toast.error(
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: "Error running schedule",
|
||||
);
|
||||
});
|
||||
}}
|
||||
>
|
||||
Run Manual Schedule
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
setEditingSchedule(schedule);
|
||||
setIsOpen(true);
|
||||
@ -150,7 +184,9 @@ export const ShowSchedules = ({ applicationId }: Props) => {
|
||||
size="sm"
|
||||
className="text-destructive hover:text-destructive"
|
||||
onClick={() =>
|
||||
deleteSchedule({ scheduleId: schedule.scheduleId })
|
||||
deleteSchedule({
|
||||
scheduleId: schedule.scheduleId,
|
||||
})
|
||||
}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
@ -159,7 +195,8 @@ export const ShowSchedules = ({ applicationId }: Props) => {
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
);
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
|
1
apps/dokploy/drizzle/0091_amused_warlock.sql
Normal file
1
apps/dokploy/drizzle/0091_amused_warlock.sql
Normal file
@ -0,0 +1 @@
|
||||
ALTER TABLE "schedule" ADD COLUMN "appName" text NOT NULL;
|
5502
apps/dokploy/drizzle/meta/0091_snapshot.json
Normal file
5502
apps/dokploy/drizzle/meta/0091_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -638,6 +638,13 @@
|
||||
"when": 1746178996842,
|
||||
"tag": "0090_colossal_azazel",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 91,
|
||||
"version": "7",
|
||||
"when": 1746180131377,
|
||||
"tag": "0091_amused_warlock",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
@ -4,8 +4,10 @@ import {
|
||||
createScheduleSchema,
|
||||
schedules,
|
||||
} from "@dokploy/server/db/schema/schedule";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import { createTRPCRouter, protectedProcedure } from "../trpc";
|
||||
import { runCommand } from "@dokploy/server/index";
|
||||
import { deployments } from "@dokploy/server/db/schema/deployment";
|
||||
|
||||
export const scheduleRouter = createTRPCRouter({
|
||||
create: protectedProcedure
|
||||
@ -59,10 +61,15 @@ export const scheduleRouter = createTRPCRouter({
|
||||
list: protectedProcedure
|
||||
.input(z.object({ applicationId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.db
|
||||
.select()
|
||||
.from(schedules)
|
||||
.where(eq(schedules.applicationId, input.applicationId));
|
||||
return ctx.db.query.schedules.findMany({
|
||||
where: eq(schedules.applicationId, input.applicationId),
|
||||
with: {
|
||||
application: true,
|
||||
deployments: {
|
||||
orderBy: [desc(deployments.createdAt)],
|
||||
},
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
||||
one: protectedProcedure
|
||||
@ -84,20 +91,10 @@ export const scheduleRouter = createTRPCRouter({
|
||||
}),
|
||||
|
||||
runManually: protectedProcedure
|
||||
.input(z.object({ scheduleId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const schedule = await ctx.db
|
||||
.select()
|
||||
.from(schedules)
|
||||
.where(eq(schedules.scheduleId, input.scheduleId));
|
||||
.input(z.object({ scheduleId: z.string().min(1) }))
|
||||
.mutation(async ({ input }) => {
|
||||
await runCommand(input.scheduleId);
|
||||
|
||||
if (!schedule) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Schedule not found",
|
||||
});
|
||||
}
|
||||
|
||||
return schedule;
|
||||
return true;
|
||||
}),
|
||||
});
|
||||
|
@ -23,5 +23,6 @@ export const paths = (isServer = false) => {
|
||||
CERTIFICATES_PATH: `${DYNAMIC_TRAEFIK_PATH}/certificates`,
|
||||
MONITORING_PATH: `${BASE_PATH}/monitoring`,
|
||||
REGISTRY_PATH: `${BASE_PATH}/registry`,
|
||||
SCHEDULES_PATH: `${BASE_PATH}/schedules`,
|
||||
};
|
||||
};
|
||||
|
@ -136,6 +136,17 @@ export const apiCreateDeploymentServer = schema
|
||||
serverId: z.string().min(1),
|
||||
});
|
||||
|
||||
export const apiCreateDeploymentSchedule = schema
|
||||
.pick({
|
||||
title: true,
|
||||
status: true,
|
||||
logPath: true,
|
||||
description: true,
|
||||
})
|
||||
.extend({
|
||||
scheduleId: z.string().min(1),
|
||||
});
|
||||
|
||||
export const apiFindAllByApplication = schema
|
||||
.pick({
|
||||
applicationId: true,
|
||||
|
@ -5,6 +5,8 @@ import { nanoid } from "nanoid";
|
||||
import { z } from "zod";
|
||||
import { applications } from "./application";
|
||||
import { deployments } from "./deployment";
|
||||
import { generateAppName } from "./utils";
|
||||
|
||||
export const schedules = pgTable("schedule", {
|
||||
scheduleId: text("scheduleId")
|
||||
.notNull()
|
||||
@ -12,6 +14,9 @@ export const schedules = pgTable("schedule", {
|
||||
.$defaultFn(() => nanoid()),
|
||||
name: text("name").notNull(),
|
||||
cronExpression: text("cronExpression").notNull(),
|
||||
appName: text("appName")
|
||||
.notNull()
|
||||
.$defaultFn(() => generateAppName("schedule")),
|
||||
command: text("command").notNull(),
|
||||
applicationId: text("applicationId")
|
||||
.notNull()
|
||||
@ -24,6 +29,8 @@ export const schedules = pgTable("schedule", {
|
||||
.$defaultFn(() => new Date().toISOString()),
|
||||
});
|
||||
|
||||
export type Schedule = typeof schedules.$inferSelect;
|
||||
|
||||
export const schedulesRelations = relations(schedules, ({ one, many }) => ({
|
||||
application: one(applications, {
|
||||
fields: [schedules.applicationId],
|
||||
|
@ -126,3 +126,5 @@ export {
|
||||
stopLogCleanup,
|
||||
getLogCleanupStatus,
|
||||
} from "./utils/access-log/handler";
|
||||
|
||||
export * from "./utils/schedules/utils";
|
||||
|
@ -6,6 +6,7 @@ import {
|
||||
type apiCreateDeployment,
|
||||
type apiCreateDeploymentCompose,
|
||||
type apiCreateDeploymentPreview,
|
||||
type apiCreateDeploymentSchedule,
|
||||
type apiCreateDeploymentServer,
|
||||
deployments,
|
||||
} from "@dokploy/server/db/schema";
|
||||
@ -27,6 +28,7 @@ import {
|
||||
findPreviewDeploymentById,
|
||||
updatePreviewDeployment,
|
||||
} from "./preview-deployment";
|
||||
import { findScheduleById } from "./schedule";
|
||||
|
||||
export type Deployment = typeof deployments.$inferSelect;
|
||||
|
||||
@ -270,6 +272,77 @@ echo "Initializing deployment" >> ${logFilePath};
|
||||
}
|
||||
};
|
||||
|
||||
export const createDeploymentSchedule = async (
|
||||
deployment: Omit<
|
||||
typeof apiCreateDeploymentSchedule._type,
|
||||
"deploymentId" | "createdAt" | "status" | "logPath"
|
||||
>,
|
||||
) => {
|
||||
const schedule = await findScheduleById(deployment.scheduleId);
|
||||
|
||||
try {
|
||||
await removeDeploymentsSchedule(
|
||||
deployment.scheduleId,
|
||||
schedule.application.serverId,
|
||||
);
|
||||
const { SCHEDULES_PATH } = paths(!!schedule.application.serverId);
|
||||
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");
|
||||
const fileName = `${schedule.appName}-${formattedDateTime}.log`;
|
||||
const logFilePath = path.join(SCHEDULES_PATH, schedule.appName, fileName);
|
||||
|
||||
if (schedule.application.serverId) {
|
||||
const server = await findServerById(schedule.application.serverId);
|
||||
|
||||
const command = `
|
||||
mkdir -p ${SCHEDULES_PATH}/${schedule.appName};
|
||||
echo "Initializing schedule" >> ${logFilePath};
|
||||
`;
|
||||
|
||||
await execAsyncRemote(server.serverId, command);
|
||||
} else {
|
||||
await fsPromises.mkdir(path.join(SCHEDULES_PATH, schedule.appName), {
|
||||
recursive: true,
|
||||
});
|
||||
await fsPromises.writeFile(logFilePath, "Initializing schedule\n");
|
||||
}
|
||||
|
||||
const deploymentCreate = await db
|
||||
.insert(deployments)
|
||||
.values({
|
||||
scheduleId: deployment.scheduleId,
|
||||
title: deployment.title || "Deployment",
|
||||
status: "running",
|
||||
logPath: logFilePath,
|
||||
description: deployment.description || "",
|
||||
})
|
||||
.returning();
|
||||
if (deploymentCreate.length === 0 || !deploymentCreate[0]) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Error creating the deployment",
|
||||
});
|
||||
}
|
||||
return deploymentCreate[0];
|
||||
} catch (error) {
|
||||
await db
|
||||
.insert(deployments)
|
||||
.values({
|
||||
scheduleId: deployment.scheduleId,
|
||||
title: deployment.title || "Deployment",
|
||||
status: "error",
|
||||
logPath: "",
|
||||
description: deployment.description || "",
|
||||
errorMessage: `An error have occured: ${error instanceof Error ? error.message : error}`,
|
||||
})
|
||||
.returning();
|
||||
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Error creating the deployment",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const removeDeployment = async (deploymentId: string) => {
|
||||
try {
|
||||
const deployment = await db
|
||||
@ -401,6 +474,41 @@ export const removeLastTenPreviewDeploymenById = async (
|
||||
}
|
||||
};
|
||||
|
||||
export const removeDeploymentsSchedule = async (
|
||||
scheduleId: string,
|
||||
serverId: string | null,
|
||||
) => {
|
||||
const deploymentList = await db.query.deployments.findMany({
|
||||
where: eq(deployments.scheduleId, scheduleId),
|
||||
orderBy: desc(deployments.createdAt),
|
||||
});
|
||||
|
||||
if (deploymentList.length > 10) {
|
||||
const deploymentsToDelete = deploymentList.slice(9);
|
||||
if (serverId) {
|
||||
let command = "";
|
||||
for (const oldDeployment of deploymentsToDelete) {
|
||||
const logPath = path.join(oldDeployment.logPath);
|
||||
|
||||
command += `
|
||||
rm -rf ${logPath};
|
||||
`;
|
||||
await removeDeployment(oldDeployment.deploymentId);
|
||||
}
|
||||
|
||||
await execAsyncRemote(serverId, command);
|
||||
} else {
|
||||
for (const oldDeployment of deploymentsToDelete) {
|
||||
const logPath = path.join(oldDeployment.logPath);
|
||||
if (existsSync(logPath)) {
|
||||
await fsPromises.unlink(logPath);
|
||||
}
|
||||
await removeDeployment(oldDeployment.deploymentId);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const removeDeployments = async (application: Application) => {
|
||||
const { appName, applicationId } = application;
|
||||
const { LOGS_PATH } = paths(!!application.serverId);
|
||||
|
21
packages/server/src/services/schedule.ts
Normal file
21
packages/server/src/services/schedule.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { schedules } from "../db/schema/schedule";
|
||||
import { db } from "../db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
export const findScheduleById = async (scheduleId: string) => {
|
||||
const schedule = await db.query.schedules.findFirst({
|
||||
where: eq(schedules.scheduleId, scheduleId),
|
||||
with: {
|
||||
application: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!schedule) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "Schedule not found",
|
||||
});
|
||||
}
|
||||
return schedule;
|
||||
};
|
@ -18,6 +18,7 @@ export const setupDirectories = () => {
|
||||
MAIN_TRAEFIK_PATH,
|
||||
MONITORING_PATH,
|
||||
SSH_PATH,
|
||||
SCHEDULES_PATH,
|
||||
} = paths();
|
||||
const directories = [
|
||||
BASE_PATH,
|
||||
@ -28,6 +29,7 @@ export const setupDirectories = () => {
|
||||
SSH_PATH,
|
||||
CERTIFICATES_PATH,
|
||||
MONITORING_PATH,
|
||||
SCHEDULES_PATH,
|
||||
];
|
||||
|
||||
for (const dir of directories) {
|
||||
|
83
packages/server/src/utils/schedules/utils.ts
Normal file
83
packages/server/src/utils/schedules/utils.ts
Normal file
@ -0,0 +1,83 @@
|
||||
import type { Schedule } from "@dokploy/server/db/schema/schedule";
|
||||
import { findScheduleById } from "@dokploy/server/services/schedule";
|
||||
import { scheduleJob as scheduleJobNode } from "node-schedule";
|
||||
import {
|
||||
getRemoteServiceContainer,
|
||||
getServiceContainer,
|
||||
} from "../docker/utils";
|
||||
import { execAsyncRemote } from "../process/execAsync";
|
||||
import { spawnAsync } from "../process/spawnAsync";
|
||||
import { createDeploymentSchedule } from "@dokploy/server/services/deployment";
|
||||
import { createWriteStream } from "node:fs";
|
||||
import { updateDeploymentStatus } from "@dokploy/server/services/deployment";
|
||||
export const scheduleJob = (schedule: Schedule) => {
|
||||
const { cronExpression, scheduleId } = schedule;
|
||||
|
||||
scheduleJobNode(cronExpression, async () => {
|
||||
await runCommand(scheduleId);
|
||||
});
|
||||
};
|
||||
|
||||
export const runCommand = async (scheduleId: string) => {
|
||||
const { application, command } = await findScheduleById(scheduleId);
|
||||
|
||||
const isServer = !!application.serverId;
|
||||
|
||||
const { Id: containerId } = isServer
|
||||
? await getRemoteServiceContainer(
|
||||
application.serverId || "",
|
||||
application.appName,
|
||||
)
|
||||
: await getServiceContainer(application.appName);
|
||||
|
||||
const deployment = await createDeploymentSchedule({
|
||||
scheduleId,
|
||||
title: "Schedule",
|
||||
description: "Schedule",
|
||||
});
|
||||
|
||||
if (isServer) {
|
||||
try {
|
||||
await execAsyncRemote(
|
||||
application.serverId,
|
||||
`
|
||||
set -e
|
||||
docker exec ${containerId} sh -c "${command}" || {
|
||||
echo "❌ Command failed" >> ${deployment.logPath};
|
||||
exit 1;
|
||||
}
|
||||
`,
|
||||
);
|
||||
} catch (error) {
|
||||
await updateDeploymentStatus(deployment.deploymentId, "error");
|
||||
throw error;
|
||||
}
|
||||
} else {
|
||||
const writeStream = createWriteStream(deployment.logPath, { flags: "a" });
|
||||
|
||||
try {
|
||||
writeStream.write(`${command}\n`);
|
||||
await spawnAsync(
|
||||
"docker",
|
||||
["exec", containerId, "sh", "-c", command],
|
||||
(data) => {
|
||||
if (writeStream.writable) {
|
||||
writeStream.write(data);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
writeStream.write("✅ Command executed successfully\n");
|
||||
} catch (error) {
|
||||
writeStream.write("❌ Command failed\n");
|
||||
writeStream.write(
|
||||
error instanceof Error ? error.message : "Unknown error",
|
||||
);
|
||||
writeStream.end();
|
||||
await updateDeploymentStatus(deployment.deploymentId, "error");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
await updateDeploymentStatus(deployment.deploymentId, "done");
|
||||
};
|
Loading…
Reference in New Issue
Block a user