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:
Mauricio Siu
2025-05-02 04:29:32 -06:00
parent 442f051457
commit f2bb01c800
14 changed files with 5949 additions and 71 deletions

View File

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

View File

@@ -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,59 +115,88 @@ export const ShowSchedules = ({ applicationId }: Props) => {
</TableRow>
</TableHeader>
<TableBody>
{schedules.map((schedule) => (
<TableRow key={schedule.scheduleId}>
<TableCell className="font-medium">
{schedule.name}
</TableCell>
<TableCell>
<Badge variant="secondary" className="font-mono">
{schedule.cronExpression}
</Badge>
</TableCell>
<TableCell>
<div className="flex items-center gap-2">
<Terminal className="w-4 h-4 text-muted-foreground" />
<code className="bg-muted px-2 py-1 rounded text-sm">
{schedule.command}
</code>
</div>
</TableCell>
<TableCell>
<Badge
variant={schedule.enabled ? "default" : "secondary"}
>
{schedule.enabled ? "Enabled" : "Disabled"}
</Badge>
</TableCell>
<TableCell className="text-right">
<div className="flex justify-end gap-2">
<Button
variant="ghost"
size="sm"
onClick={() => {
setEditingSchedule(schedule);
setIsOpen(true);
}}
{schedules.map((schedule) => {
const application = schedule.application;
const deployments = schedule.deployments;
return (
<TableRow key={schedule.scheduleId}>
<TableCell className="font-medium">
{schedule.name}
</TableCell>
<TableCell>
<Badge variant="secondary" className="font-mono">
{schedule.cronExpression}
</Badge>
</TableCell>
<TableCell>
<div className="flex items-center gap-2">
<Terminal className="w-4 h-4 text-muted-foreground" />
<code className="bg-muted px-2 py-1 rounded text-sm">
{schedule.command}
</code>
</div>
</TableCell>
<TableCell>
<Badge
variant={schedule.enabled ? "default" : "secondary"}
>
<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>
))}
{schedule.enabled ? "Enabled" : "Disabled"}
</Badge>
</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);
}}
>
<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>
</Table>
</div>

View File

@@ -0,0 +1 @@
ALTER TABLE "schedule" ADD COLUMN "appName" text NOT NULL;

File diff suppressed because it is too large Load Diff

View File

@@ -638,6 +638,13 @@
"when": 1746178996842,
"tag": "0090_colossal_azazel",
"breakpoints": true
},
{
"idx": 91,
"version": "7",
"when": 1746180131377,
"tag": "0091_amused_warlock",
"breakpoints": true
}
]
}

View File

@@ -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;
}),
});