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,
|
CardTitle,
|
||||||
} from "@/components/ui/card";
|
} from "@/components/ui/card";
|
||||||
import { Badge } from "@/components/ui/badge";
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { ShowSchedulesLogs } from "./show-schedules-logs";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
applicationId: string;
|
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 utils = api.useContext();
|
||||||
|
|
||||||
const onClose = () => {
|
const onClose = () => {
|
||||||
@ -107,59 +115,88 @@ export const ShowSchedules = ({ applicationId }: Props) => {
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHeader>
|
</TableHeader>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{schedules.map((schedule) => (
|
{schedules.map((schedule) => {
|
||||||
<TableRow key={schedule.scheduleId}>
|
const application = schedule.application;
|
||||||
<TableCell className="font-medium">
|
const deployments = schedule.deployments;
|
||||||
{schedule.name}
|
return (
|
||||||
</TableCell>
|
<TableRow key={schedule.scheduleId}>
|
||||||
<TableCell>
|
<TableCell className="font-medium">
|
||||||
<Badge variant="secondary" className="font-mono">
|
{schedule.name}
|
||||||
{schedule.cronExpression}
|
</TableCell>
|
||||||
</Badge>
|
<TableCell>
|
||||||
</TableCell>
|
<Badge variant="secondary" className="font-mono">
|
||||||
<TableCell>
|
{schedule.cronExpression}
|
||||||
<div className="flex items-center gap-2">
|
</Badge>
|
||||||
<Terminal className="w-4 h-4 text-muted-foreground" />
|
</TableCell>
|
||||||
<code className="bg-muted px-2 py-1 rounded text-sm">
|
<TableCell>
|
||||||
{schedule.command}
|
<div className="flex items-center gap-2">
|
||||||
</code>
|
<Terminal className="w-4 h-4 text-muted-foreground" />
|
||||||
</div>
|
<code className="bg-muted px-2 py-1 rounded text-sm">
|
||||||
</TableCell>
|
{schedule.command}
|
||||||
<TableCell>
|
</code>
|
||||||
<Badge
|
</div>
|
||||||
variant={schedule.enabled ? "default" : "secondary"}
|
</TableCell>
|
||||||
>
|
<TableCell>
|
||||||
{schedule.enabled ? "Enabled" : "Disabled"}
|
<Badge
|
||||||
</Badge>
|
variant={schedule.enabled ? "default" : "secondary"}
|
||||||
</TableCell>
|
|
||||||
<TableCell className="text-right">
|
|
||||||
<div className="flex justify-end gap-2">
|
|
||||||
<Button
|
|
||||||
variant="ghost"
|
|
||||||
size="sm"
|
|
||||||
onClick={() => {
|
|
||||||
setEditingSchedule(schedule);
|
|
||||||
setIsOpen(true);
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
<Edit2 className="w-4 h-4" />
|
{schedule.enabled ? "Enabled" : "Disabled"}
|
||||||
<span className="sr-only">Edit</span>
|
</Badge>
|
||||||
</Button>
|
</TableCell>
|
||||||
<Button
|
<TableCell className="text-right">
|
||||||
variant="ghost"
|
<div className="flex justify-end gap-2">
|
||||||
size="sm"
|
<ShowSchedulesLogs
|
||||||
className="text-destructive hover:text-destructive"
|
deployments={deployments}
|
||||||
onClick={() =>
|
serverId={application.serverId || undefined}
|
||||||
deleteSchedule({ scheduleId: schedule.scheduleId })
|
/>
|
||||||
}
|
<Button
|
||||||
>
|
variant="ghost"
|
||||||
<Trash2 className="w-4 h-4" />
|
size="sm"
|
||||||
<span className="sr-only">Delete</span>
|
onClick={async () => {
|
||||||
</Button>
|
await runManually({
|
||||||
</div>
|
scheduleId: schedule.scheduleId,
|
||||||
</TableCell>
|
})
|
||||||
</TableRow>
|
.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);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Edit2 className="w-4 h-4" />
|
||||||
|
<span className="sr-only">Edit</span>
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
className="text-destructive hover:text-destructive"
|
||||||
|
onClick={() =>
|
||||||
|
deleteSchedule({
|
||||||
|
scheduleId: schedule.scheduleId,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Trash2 className="w-4 h-4" />
|
||||||
|
<span className="sr-only">Delete</span>
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</TableBody>
|
</TableBody>
|
||||||
</Table>
|
</Table>
|
||||||
</div>
|
</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,
|
"when": 1746178996842,
|
||||||
"tag": "0090_colossal_azazel",
|
"tag": "0090_colossal_azazel",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 91,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1746180131377,
|
||||||
|
"tag": "0091_amused_warlock",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -4,8 +4,10 @@ import {
|
|||||||
createScheduleSchema,
|
createScheduleSchema,
|
||||||
schedules,
|
schedules,
|
||||||
} from "@dokploy/server/db/schema/schedule";
|
} from "@dokploy/server/db/schema/schedule";
|
||||||
import { eq } from "drizzle-orm";
|
import { desc, eq } from "drizzle-orm";
|
||||||
import { createTRPCRouter, protectedProcedure } from "../trpc";
|
import { createTRPCRouter, protectedProcedure } from "../trpc";
|
||||||
|
import { runCommand } from "@dokploy/server/index";
|
||||||
|
import { deployments } from "@dokploy/server/db/schema/deployment";
|
||||||
|
|
||||||
export const scheduleRouter = createTRPCRouter({
|
export const scheduleRouter = createTRPCRouter({
|
||||||
create: protectedProcedure
|
create: protectedProcedure
|
||||||
@ -59,10 +61,15 @@ export const scheduleRouter = createTRPCRouter({
|
|||||||
list: protectedProcedure
|
list: protectedProcedure
|
||||||
.input(z.object({ applicationId: z.string() }))
|
.input(z.object({ applicationId: z.string() }))
|
||||||
.query(async ({ ctx, input }) => {
|
.query(async ({ ctx, input }) => {
|
||||||
return ctx.db
|
return ctx.db.query.schedules.findMany({
|
||||||
.select()
|
where: eq(schedules.applicationId, input.applicationId),
|
||||||
.from(schedules)
|
with: {
|
||||||
.where(eq(schedules.applicationId, input.applicationId));
|
application: true,
|
||||||
|
deployments: {
|
||||||
|
orderBy: [desc(deployments.createdAt)],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
}),
|
}),
|
||||||
|
|
||||||
one: protectedProcedure
|
one: protectedProcedure
|
||||||
@ -84,20 +91,10 @@ export const scheduleRouter = createTRPCRouter({
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
runManually: protectedProcedure
|
runManually: protectedProcedure
|
||||||
.input(z.object({ scheduleId: z.string() }))
|
.input(z.object({ scheduleId: z.string().min(1) }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ input }) => {
|
||||||
const schedule = await ctx.db
|
await runCommand(input.scheduleId);
|
||||||
.select()
|
|
||||||
.from(schedules)
|
|
||||||
.where(eq(schedules.scheduleId, input.scheduleId));
|
|
||||||
|
|
||||||
if (!schedule) {
|
return true;
|
||||||
throw new TRPCError({
|
|
||||||
code: "NOT_FOUND",
|
|
||||||
message: "Schedule not found",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return schedule;
|
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
@ -23,5 +23,6 @@ export const paths = (isServer = false) => {
|
|||||||
CERTIFICATES_PATH: `${DYNAMIC_TRAEFIK_PATH}/certificates`,
|
CERTIFICATES_PATH: `${DYNAMIC_TRAEFIK_PATH}/certificates`,
|
||||||
MONITORING_PATH: `${BASE_PATH}/monitoring`,
|
MONITORING_PATH: `${BASE_PATH}/monitoring`,
|
||||||
REGISTRY_PATH: `${BASE_PATH}/registry`,
|
REGISTRY_PATH: `${BASE_PATH}/registry`,
|
||||||
|
SCHEDULES_PATH: `${BASE_PATH}/schedules`,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -136,6 +136,17 @@ export const apiCreateDeploymentServer = schema
|
|||||||
serverId: z.string().min(1),
|
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
|
export const apiFindAllByApplication = schema
|
||||||
.pick({
|
.pick({
|
||||||
applicationId: true,
|
applicationId: true,
|
||||||
|
@ -5,6 +5,8 @@ import { nanoid } from "nanoid";
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { applications } from "./application";
|
import { applications } from "./application";
|
||||||
import { deployments } from "./deployment";
|
import { deployments } from "./deployment";
|
||||||
|
import { generateAppName } from "./utils";
|
||||||
|
|
||||||
export const schedules = pgTable("schedule", {
|
export const schedules = pgTable("schedule", {
|
||||||
scheduleId: text("scheduleId")
|
scheduleId: text("scheduleId")
|
||||||
.notNull()
|
.notNull()
|
||||||
@ -12,6 +14,9 @@ export const schedules = pgTable("schedule", {
|
|||||||
.$defaultFn(() => nanoid()),
|
.$defaultFn(() => nanoid()),
|
||||||
name: text("name").notNull(),
|
name: text("name").notNull(),
|
||||||
cronExpression: text("cronExpression").notNull(),
|
cronExpression: text("cronExpression").notNull(),
|
||||||
|
appName: text("appName")
|
||||||
|
.notNull()
|
||||||
|
.$defaultFn(() => generateAppName("schedule")),
|
||||||
command: text("command").notNull(),
|
command: text("command").notNull(),
|
||||||
applicationId: text("applicationId")
|
applicationId: text("applicationId")
|
||||||
.notNull()
|
.notNull()
|
||||||
@ -24,6 +29,8 @@ export const schedules = pgTable("schedule", {
|
|||||||
.$defaultFn(() => new Date().toISOString()),
|
.$defaultFn(() => new Date().toISOString()),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export type Schedule = typeof schedules.$inferSelect;
|
||||||
|
|
||||||
export const schedulesRelations = relations(schedules, ({ one, many }) => ({
|
export const schedulesRelations = relations(schedules, ({ one, many }) => ({
|
||||||
application: one(applications, {
|
application: one(applications, {
|
||||||
fields: [schedules.applicationId],
|
fields: [schedules.applicationId],
|
||||||
|
@ -126,3 +126,5 @@ export {
|
|||||||
stopLogCleanup,
|
stopLogCleanup,
|
||||||
getLogCleanupStatus,
|
getLogCleanupStatus,
|
||||||
} from "./utils/access-log/handler";
|
} from "./utils/access-log/handler";
|
||||||
|
|
||||||
|
export * from "./utils/schedules/utils";
|
||||||
|
@ -6,6 +6,7 @@ import {
|
|||||||
type apiCreateDeployment,
|
type apiCreateDeployment,
|
||||||
type apiCreateDeploymentCompose,
|
type apiCreateDeploymentCompose,
|
||||||
type apiCreateDeploymentPreview,
|
type apiCreateDeploymentPreview,
|
||||||
|
type apiCreateDeploymentSchedule,
|
||||||
type apiCreateDeploymentServer,
|
type apiCreateDeploymentServer,
|
||||||
deployments,
|
deployments,
|
||||||
} from "@dokploy/server/db/schema";
|
} from "@dokploy/server/db/schema";
|
||||||
@ -27,6 +28,7 @@ import {
|
|||||||
findPreviewDeploymentById,
|
findPreviewDeploymentById,
|
||||||
updatePreviewDeployment,
|
updatePreviewDeployment,
|
||||||
} from "./preview-deployment";
|
} from "./preview-deployment";
|
||||||
|
import { findScheduleById } from "./schedule";
|
||||||
|
|
||||||
export type Deployment = typeof deployments.$inferSelect;
|
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) => {
|
export const removeDeployment = async (deploymentId: string) => {
|
||||||
try {
|
try {
|
||||||
const deployment = await db
|
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) => {
|
export const removeDeployments = async (application: Application) => {
|
||||||
const { appName, applicationId } = application;
|
const { appName, applicationId } = application;
|
||||||
const { LOGS_PATH } = paths(!!application.serverId);
|
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,
|
MAIN_TRAEFIK_PATH,
|
||||||
MONITORING_PATH,
|
MONITORING_PATH,
|
||||||
SSH_PATH,
|
SSH_PATH,
|
||||||
|
SCHEDULES_PATH,
|
||||||
} = paths();
|
} = paths();
|
||||||
const directories = [
|
const directories = [
|
||||||
BASE_PATH,
|
BASE_PATH,
|
||||||
@ -28,6 +29,7 @@ export const setupDirectories = () => {
|
|||||||
SSH_PATH,
|
SSH_PATH,
|
||||||
CERTIFICATES_PATH,
|
CERTIFICATES_PATH,
|
||||||
MONITORING_PATH,
|
MONITORING_PATH,
|
||||||
|
SCHEDULES_PATH,
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const dir of directories) {
|
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