mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
- 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.
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
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");
|
|
};
|