import { zodResolver } from "@hookform/resolvers/zod"; import { DatabaseZap, Info, PenBoxIcon, PlusCircle, RefreshCw, } from "lucide-react"; import { useEffect, useState } from "react"; import { type Control, useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { AlertBlock } from "@/components/shared/alert-block"; import { CodeEditor } from "@/components/shared/code-editor"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; import { api } from "@/utils/api"; import type { CacheType } from "../domains/handle-domain"; export const commonCronExpressions = [ { label: "Every minute", value: "* * * * *" }, { label: "Every hour", value: "0 * * * *" }, { label: "Every day at midnight", value: "0 0 * * *" }, { label: "Every Sunday at midnight", value: "0 0 * * 0" }, { label: "Every month on the 1st at midnight", value: "0 0 1 * *" }, { label: "Every 15 minutes", value: "*/15 * * * *" }, { label: "Every weekday at midnight", value: "0 0 * * 1-5" }, { label: "Custom", value: "custom" }, ]; const formSchema = z .object({ name: z.string().min(1, "Name is required"), cronExpression: z.string().min(1, "Cron expression is required"), shellType: z.enum(["bash", "sh"]).default("bash"), command: z.string(), enabled: z.boolean().default(true), serviceName: z.string(), scheduleType: z.enum([ "application", "compose", "server", "dokploy-server", ]), script: z.string(), }) .superRefine((data, ctx) => { if (data.scheduleType === "compose" && !data.serviceName) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Service name is required", path: ["serviceName"], }); } if ( (data.scheduleType === "dokploy-server" || data.scheduleType === "server") && !data.script ) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Script is required", path: ["script"], }); } if ( (data.scheduleType === "application" || data.scheduleType === "compose") && !data.command ) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "Command is required", path: ["command"], }); } }); interface Props { id?: string; scheduleId?: string; scheduleType?: "application" | "compose" | "server" | "dokploy-server"; } export const ScheduleFormField = ({ name, formControl, }: { name: string; formControl: Control; }) => { const [selectedOption, setSelectedOption] = useState(""); return ( ( Schedule

Cron expression format: minute hour day month weekday

Example: 0 0 * * * (daily at midnight)

{ const value = e.target.value; const commonExpression = commonCronExpressions.find( (expression) => expression.value === value, ); if (commonExpression) { setSelectedOption(commonExpression.value); } else { setSelectedOption("custom"); } field.onChange(e); }} />
Choose a predefined schedule or enter a custom cron expression
)} /> ); }; export const HandleSchedules = ({ id, scheduleId, scheduleType }: Props) => { const [isOpen, setIsOpen] = useState(false); const [cacheType, setCacheType] = useState("cache"); const utils = api.useUtils(); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { name: "", cronExpression: "", shellType: "bash", command: "", enabled: true, serviceName: "", scheduleType: scheduleType || "application", script: "", }, }); const scheduleTypeForm = form.watch("scheduleType"); const { data: schedule } = api.schedule.one.useQuery( { scheduleId: scheduleId || "" }, { enabled: !!scheduleId }, ); const { data: services, isFetching: isLoadingServices, error: errorServices, refetch: refetchServices, } = api.compose.loadServices.useQuery( { composeId: id || "", type: cacheType, }, { retry: false, refetchOnWindowFocus: false, enabled: !!id && scheduleType === "compose", }, ); useEffect(() => { if (scheduleId && schedule) { form.reset({ name: schedule.name, cronExpression: schedule.cronExpression, shellType: schedule.shellType, command: schedule.command, enabled: schedule.enabled, serviceName: schedule.serviceName || "", scheduleType: schedule.scheduleType, script: schedule.script || "", }); } }, [form, schedule, scheduleId]); const { mutateAsync, isLoading } = scheduleId ? api.schedule.update.useMutation() : api.schedule.create.useMutation(); const onSubmit = async (values: z.infer) => { if (!id && !scheduleId) return; await mutateAsync({ ...values, scheduleId: scheduleId || "", ...(scheduleType === "application" && { applicationId: id || "", }), ...(scheduleType === "compose" && { composeId: id || "", }), ...(scheduleType === "server" && { serverId: id || "", }), ...(scheduleType === "dokploy-server" && { userId: id || "", }), }) .then(() => { toast.success( `Schedule ${scheduleId ? "updated" : "created"} successfully`, ); utils.schedule.list.invalidate({ id, scheduleType, }); setIsOpen(false); }) .catch((error) => { toast.error( error instanceof Error ? error.message : "An unknown error occurred", ); }); }; return ( {scheduleId ? ( ) : ( )} {scheduleId ? "Edit" : "Create"} Schedule {scheduleId ? "Manage" : "Create"} a schedule to run a task at a specific time or interval.
{scheduleTypeForm === "compose" && (
{errorServices && ( {errorServices?.message} )} ( Service Name

Fetch: Will clone the repository and load the services

Cache: If you previously deployed this compose, it will read the services from the last deployment/fetch from the repository

)} />
)} ( Task Name A descriptive name for your scheduled task )} /> {(scheduleTypeForm === "application" || scheduleTypeForm === "compose") && ( <> ( Shell Type Choose the shell to execute your command )} /> ( Command The command to execute in your container )} /> )} {(scheduleTypeForm === "dokploy-server" || scheduleTypeForm === "server") && ( ( Script )} /> )} ( Enabled )} />
); };