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

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

View File

@@ -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,

View File

@@ -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],

View File

@@ -126,3 +126,5 @@ export {
stopLogCleanup,
getLogCleanupStatus,
} from "./utils/access-log/handler";
export * from "./utils/schedules/utils";

View File

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

View 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;
};

View File

@@ -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) {

View 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");
};