Add schedule management features

- Implemented `HandleSchedules` component for creating and updating schedules with validation.
- Added `ShowSchedules` component to display a list of schedules with options to edit and delete.
- Created API routes for schedule management including create, update, delete, and list functionalities.
- Defined the `schedule` table schema in the database with necessary fields and relationships.
- Integrated schedule management into the application service dashboard, allowing users to manage schedules directly from the UI.
This commit is contained in:
Mauricio Siu
2025-05-02 03:21:13 -06:00
parent 7ae3ff22ee
commit d4064805eb
11 changed files with 5881 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
import { Button } from "@/components/ui/button";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { api } from "@/utils/api";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
const formSchema = z.object({
name: z.string().min(1, "Name is required"),
cronExpression: z.string().min(1, "Cron expression is required"),
command: z.string().min(1, "Command is required"),
});
interface Props {
applicationId: string;
onSuccess?: () => void;
defaultValues?: {
name: string;
cronExpression: string;
command: string;
};
scheduleId?: string;
}
export const HandleSchedules = ({
applicationId,
onSuccess,
defaultValues,
scheduleId,
}: Props) => {
const utils = api.useContext();
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: defaultValues || {
name: "",
cronExpression: "",
command: "",
},
});
const { mutate: createSchedule, isLoading: isCreating } =
api.schedule.create.useMutation({
onSuccess: () => {
utils.schedule.list.invalidate({ applicationId });
form.reset();
onSuccess?.();
},
});
const { mutate: updateSchedule, isLoading: isUpdating } =
api.schedule.update.useMutation({
onSuccess: () => {
utils.schedule.list.invalidate({ applicationId });
onSuccess?.();
},
});
const isLoading = isCreating || isUpdating;
const onSubmit = (values: z.infer<typeof formSchema>) => {
if (scheduleId) {
updateSchedule({
...values,
scheduleId,
applicationId,
});
} else {
createSchedule({
...values,
applicationId,
});
}
};
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="name"
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input placeholder="Daily backup" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="cronExpression"
render={({ field }) => (
<FormItem>
<FormLabel>Cron Expression</FormLabel>
<FormControl>
<Input placeholder="0 0 * * *" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="command"
render={({ field }) => (
<FormItem>
<FormLabel>Command</FormLabel>
<FormControl>
<Input placeholder="npm run backup" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" disabled={isLoading}>
{scheduleId ? "Update" : "Create"} Schedule
</Button>
</form>
</Form>
);
};

View File

@@ -0,0 +1,119 @@
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { api } from "@/utils/api";
import { useState } from "react";
import { HandleSchedules } from "./handle-schedules";
interface Props {
applicationId: string;
}
export const ShowSchedules = ({ applicationId }: Props) => {
const [isOpen, setIsOpen] = useState(false);
const [editingSchedule, setEditingSchedule] = useState<{
scheduleId: string;
name: string;
cronExpression: string;
command: string;
} | null>(null);
const { data: schedules } = api.schedule.list.useQuery({
applicationId,
});
const { mutate: deleteSchedule } = api.schedule.delete.useMutation({
onSuccess: () => {
utils.schedule.list.invalidate({ applicationId });
},
});
const utils = api.useContext();
const onClose = () => {
setIsOpen(false);
setEditingSchedule(null);
};
return (
<div className="space-y-4">
<div className="flex justify-between items-center">
<h2 className="text-2xl font-bold">Schedules</h2>
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button>Create Schedule</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>
{editingSchedule ? "Edit" : "Create"} Schedule
</DialogTitle>
</DialogHeader>
<HandleSchedules
applicationId={applicationId}
onSuccess={onClose}
defaultValues={editingSchedule || undefined}
scheduleId={editingSchedule?.scheduleId}
/>
</DialogContent>
</Dialog>
</div>
{schedules && schedules.length > 0 ? (
<Table>
<TableHeader>
<TableRow>
<TableHead>Name</TableHead>
<TableHead>Cron Expression</TableHead>
<TableHead>Command</TableHead>
<TableHead>Actions</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{schedules.map((schedule) => (
<TableRow key={schedule.scheduleId}>
<TableCell>{schedule.name}</TableCell>
<TableCell>{schedule.cronExpression}</TableCell>
<TableCell>{schedule.command}</TableCell>
<TableCell className="space-x-2">
<Button
variant="outline"
onClick={() => {
setEditingSchedule(schedule);
setIsOpen(true);
}}
>
Edit
</Button>
<Button
variant="destructive"
onClick={() =>
deleteSchedule({ scheduleId: schedule.scheduleId })
}
>
Delete
</Button>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
) : (
<div className="text-center text-gray-500">No schedules found</div>
)}
</div>
);
};

View File

@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS "schedule" (
"scheduleId" text PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"cronExpression" text NOT NULL,
"command" text NOT NULL,
"applicationId" text NOT NULL REFERENCES "application"("applicationId") ON DELETE CASCADE,
"createdAt" text NOT NULL
);

View File

@@ -0,0 +1,10 @@
CREATE TABLE "schedule" (
"scheduleId" text PRIMARY KEY NOT NULL,
"name" text NOT NULL,
"cronExpression" text NOT NULL,
"command" text NOT NULL,
"applicationId" text NOT NULL,
"createdAt" text NOT NULL
);
--> statement-breakpoint
ALTER TABLE "schedule" ADD CONSTRAINT "schedule_applicationId_application_applicationId_fk" FOREIGN KEY ("applicationId") REFERENCES "public"."application"("applicationId") ON DELETE cascade ON UPDATE no action;

File diff suppressed because it is too large Load Diff

View File

@@ -617,6 +617,13 @@
"when": 1745723563822,
"tag": "0087_lively_risque",
"breakpoints": true
},
{
"idx": 88,
"version": "7",
"when": 1746177535905,
"tag": "0088_worthless_surge",
"breakpoints": true
}
]
}

View File

@@ -12,6 +12,7 @@ import { ShowEnvironment } from "@/components/dashboard/application/environment/
import { ShowGeneralApplication } from "@/components/dashboard/application/general/show";
import { ShowDockerLogs } from "@/components/dashboard/application/logs/show";
import { ShowPreviewDeployments } from "@/components/dashboard/application/preview-deployments/show-preview-deployments";
import { ShowSchedules } from "@/components/dashboard/application/schedules/show-schedules";
import { UpdateApplication } from "@/components/dashboard/application/update-application";
import { DeleteService } from "@/components/dashboard/compose/delete-service";
import { ContainerFreeMonitoring } from "@/components/dashboard/monitoring/free/container/show-free-container-monitoring";
@@ -232,6 +233,7 @@ const Service = (
<TabsTrigger value="preview-deployments">
Preview Deployments
</TabsTrigger>
<TabsTrigger value="schedules">Schedules</TabsTrigger>
<TabsTrigger value="deployments">Deployments</TabsTrigger>
<TabsTrigger value="logs">Logs</TabsTrigger>
{((data?.serverId && isCloud) || !data?.server) && (
@@ -308,6 +310,11 @@ const Service = (
/>
</div>
</TabsContent>
<TabsContent value="schedules">
<div className="flex flex-col gap-4 pt-2.5">
<ShowSchedules applicationId={applicationId} />
</div>
</TabsContent>
<TabsContent value="deployments" className="w-full">
<div className="flex flex-col gap-4 pt-2.5">
<ShowDeployments applicationId={applicationId} />

View File

@@ -35,6 +35,7 @@ import { sshRouter } from "./routers/ssh-key";
import { stripeRouter } from "./routers/stripe";
import { swarmRouter } from "./routers/swarm";
import { userRouter } from "./routers/user";
import { scheduleRouter } from "./routers/schedule";
/**
* This is the primary router for your server.
*
@@ -78,6 +79,7 @@ export const appRouter = createTRPCRouter({
swarm: swarmRouter,
ai: aiRouter,
organization: organizationRouter,
schedule: scheduleRouter,
});
// export type definition of API

View File

@@ -0,0 +1,85 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import {
createScheduleSchema,
schedules,
} from "@dokploy/server/db/schema/schedule";
import { eq } from "drizzle-orm";
import { createTRPCRouter, protectedProcedure } from "../trpc";
export const scheduleRouter = createTRPCRouter({
create: protectedProcedure
.input(createScheduleSchema)
.mutation(async ({ ctx, input }) => {
const [schedule] = await ctx.db
.insert(schedules)
.values(input)
.returning();
return schedule;
}),
update: protectedProcedure
.input(createScheduleSchema.extend({ scheduleId: z.string() }))
.mutation(async ({ ctx, input }) => {
const { scheduleId, ...rest } = input;
const [schedule] = await ctx.db
.update(schedules)
.set(rest)
.where(eq(schedules.scheduleId, scheduleId))
.returning();
if (!schedule) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Schedule not found",
});
}
return schedule;
}),
delete: protectedProcedure
.input(z.object({ scheduleId: z.string() }))
.mutation(async ({ ctx, input }) => {
const [schedule] = await ctx.db
.delete(schedules)
.where(eq(schedules.scheduleId, input.scheduleId))
.returning();
if (!schedule) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Schedule not found",
});
}
return schedule;
}),
list: protectedProcedure
.input(z.object({ applicationId: z.string() }))
.query(async ({ ctx, input }) => {
return ctx.db
.select()
.from(schedules)
.where(eq(schedules.applicationId, input.applicationId));
}),
one: protectedProcedure
.input(z.object({ scheduleId: z.string() }))
.query(async ({ ctx, input }) => {
const [schedule] = await ctx.db
.select()
.from(schedules)
.where(eq(schedules.scheduleId, input.scheduleId));
if (!schedule) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Schedule not found",
});
}
return schedule;
}),
});

View File

@@ -31,3 +31,4 @@ export * from "./utils";
export * from "./preview-deployments";
export * from "./ai";
export * from "./account";
export * from "./schedule";

View File

@@ -0,0 +1,38 @@
import { relations } from "drizzle-orm";
import { pgTable, text } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid";
import { z } from "zod";
import { applications } from "./application";
export const schedules = pgTable("schedule", {
scheduleId: text("scheduleId")
.notNull()
.primaryKey()
.$defaultFn(() => nanoid()),
name: text("name").notNull(),
cronExpression: text("cronExpression").notNull(),
command: text("command").notNull(),
applicationId: text("applicationId")
.notNull()
.references(() => applications.applicationId, {
onDelete: "cascade",
}),
createdAt: text("createdAt")
.notNull()
.$defaultFn(() => new Date().toISOString()),
});
export const schedulesRelations = relations(schedules, ({ one }) => ({
application: one(applications, {
fields: [schedules.applicationId],
references: [applications.applicationId],
}),
}));
export const createScheduleSchema = createInsertSchema(schedules, {
name: z.string().min(1),
cronExpression: z.string().min(1),
command: z.string().min(1),
applicationId: z.string().min(1),
});