Merge pull request #4955 from Dokploy/fix/schedule-run-manually-deployment-metadata

fix(schedule): return deployment metadata from runManually and fail early on missing container
This commit is contained in:
Mauricio Siu
2026-08-02 16:22:35 -06:00
committed by GitHub
3 changed files with 90 additions and 68 deletions
@@ -58,8 +58,12 @@ export const ShowSchedules = ({ id, scheduleType = "application" }: Props) => {
const handleRunManually = async (scheduleId: string) => { const handleRunManually = async (scheduleId: string) => {
setRunningSchedules((prev) => new Set(prev).add(scheduleId)); setRunningSchedules((prev) => new Set(prev).add(scheduleId));
try { try {
await runManually({ scheduleId }); const result = await runManually({ scheduleId });
toast.success("Schedule run successfully"); if (result.status === "error") {
toast.error("Schedule run failed, check the deployment logs");
} else {
toast.success("Schedule run successfully");
}
await refetchSchedules(); await refetchSchedules();
} catch { } catch {
toast.error("Error running schedule"); toast.error("Error running schedule");
+6 -2
View File
@@ -329,13 +329,17 @@ export const scheduleRouter = createTRPCRouter({
await checkPermission(ctx, { schedule: ["create"] }); await checkPermission(ctx, { schedule: ["create"] });
} }
try { try {
await runCommand(input.scheduleId); const deployment = await runCommand(input.scheduleId);
await audit(ctx, { await audit(ctx, {
action: "run", action: "run",
resourceType: "schedule", resourceType: "schedule",
resourceId: input.scheduleId, resourceId: input.scheduleId,
}); });
return true; return {
status: deployment.status,
deploymentId: deployment.deploymentId,
logPath: deployment.logPath,
};
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "INTERNAL_SERVER_ERROR", code: "INTERNAL_SERVER_ERROR",
+78 -64
View File
@@ -55,25 +55,46 @@ export const runCommand = async (scheduleId: string) => {
description: "Schedule", description: "Schedule",
}); });
if (scheduleType === "application" || scheduleType === "compose") { try {
let containerId = ""; if (scheduleType === "application" || scheduleType === "compose") {
let serverId = ""; let containerId = "";
if (scheduleType === "application" && application) { let serverId = "";
const container = await getServiceContainer( if (scheduleType === "application" && application) {
application.appName, const container = await getServiceContainer(
application.serverId, application.appName,
); application.serverId,
containerId = container?.Id || ""; );
serverId = application.serverId || ""; containerId = container?.Id || "";
} serverId = application.serverId || "";
if (scheduleType === "compose" && compose) { }
const container = await getComposeContainer(compose, serviceName || ""); if (scheduleType === "compose" && compose) {
containerId = container?.Id || ""; const container = await getComposeContainer(compose, serviceName || "");
serverId = compose.serverId || ""; containerId = container?.Id || "";
} serverId = compose.serverId || "";
}
if (serverId) { if (!containerId) {
try { const target =
scheduleType === "compose"
? `service '${serviceName}' of compose '${compose?.name}'`
: `application '${application?.appName}'`;
const message = `Container not found for ${target}, make sure the service is running`;
if (serverId) {
await execAsyncRemote(
serverId,
`echo ${quote([`❌ ${message}`])} >> ${quote([deployment.logPath])}`,
);
} else {
const writeStream = createWriteStream(deployment.logPath, {
flags: "a",
});
writeStream.write(`❌ ${message}\n`);
writeStream.end();
}
throw new Error(message);
}
if (serverId) {
await execAsyncRemote( await execAsyncRemote(
serverId, serverId,
` `
@@ -86,47 +107,44 @@ export const runCommand = async (scheduleId: string) => {
echo "✅ Command executed successfully" >> ${quote([deployment.logPath])}; echo "✅ Command executed successfully" >> ${quote([deployment.logPath])};
`, `,
); );
} catch (error) { } else {
await updateDeploymentStatus(deployment.deploymentId, "error"); const writeStream = createWriteStream(deployment.logPath, {
throw error; flags: "a",
} });
} else {
const writeStream = createWriteStream(deployment.logPath, { flags: "a" });
try { try {
if (IS_CLOUD) { if (IS_CLOUD) {
writeStream.write(
"This feature is not available in the cloud version.",
);
writeStream.end();
return { ...deployment, status: "running" as const };
}
writeStream.write( writeStream.write(
"This feature is not available in the cloud version.", `docker exec ${containerId} ${shellType} -c ${command}\n`,
);
await spawnAsync(
"docker",
["exec", containerId, shellType, "-c", command],
(data) => {
if (writeStream.writable) {
writeStream.write(data);
}
},
);
writeStream.write("✅ Command executed successfully\n");
writeStream.end();
} catch (error) {
writeStream.write("❌ Command failed\n");
writeStream.write(
error instanceof Error ? error.message : "Unknown error",
); );
writeStream.end(); writeStream.end();
return; throw error;
} }
writeStream.write(
`docker exec ${containerId} ${shellType} -c ${command}\n`,
);
await spawnAsync(
"docker",
["exec", containerId, shellType, "-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;
} }
} } else if (scheduleType === "dokploy-server") {
} else if (scheduleType === "dokploy-server") {
try {
const writeStream = createWriteStream(deployment.logPath, { flags: "a" }); const writeStream = createWriteStream(deployment.logPath, { flags: "a" });
const { SCHEDULES_PATH } = paths(); const { SCHEDULES_PATH } = paths();
const fullPath = path.join(SCHEDULES_PATH, appName || ""); const fullPath = path.join(SCHEDULES_PATH, appName || "");
@@ -151,18 +169,13 @@ export const runCommand = async (scheduleId: string) => {
cwd: fullPath, cwd: fullPath,
}, },
); );
} catch (error) { } else if (scheduleType === "server") {
await updateDeploymentStatus(deployment.deploymentId, "error");
throw error;
}
} else if (scheduleType === "server") {
try {
const { SCHEDULES_PATH } = paths(true); const { SCHEDULES_PATH } = paths(true);
const fullPath = path.join(SCHEDULES_PATH, appName || ""); const fullPath = path.join(SCHEDULES_PATH, appName || "");
const command = ` const command = `
set -e set -e
echo "Running script" >> ${deployment.logPath}; echo "Running script" >> ${deployment.logPath};
bash -c ${fullPath}/script.sh 2>&1 | tee -a ${deployment.logPath} || { bash -c ${fullPath}/script.sh 2>&1 | tee -a ${deployment.logPath} || {
echo "❌ Command failed" >> ${deployment.logPath}; echo "❌ Command failed" >> ${deployment.logPath};
exit 1; exit 1;
} }
@@ -177,10 +190,11 @@ export const runCommand = async (scheduleId: string) => {
}); });
} }
}); });
} catch (error) {
await updateDeploymentStatus(deployment.deploymentId, "error");
throw error;
} }
await updateDeploymentStatus(deployment.deploymentId, "done");
return { ...deployment, status: "done" as const };
} catch {
await updateDeploymentStatus(deployment.deploymentId, "error");
return { ...deployment, status: "error" as const };
} }
await updateDeploymentStatus(deployment.deploymentId, "done");
}; };