Skip to content

Commit cbb3450

Browse files
committed
fix(schedule): return deployment metadata from runManually and fail early on missing container
1 parent a058d1f commit cbb3450

3 files changed

Lines changed: 90 additions & 68 deletions

File tree

‎apps/dokploy/components/dashboard/application/schedules/show-schedules.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ export const ShowSchedules = ({ id, scheduleType = "application" }: Props) => {
5858
const handleRunManually = async (scheduleId: string) => {
5959
setRunningSchedules((prev) => new Set(prev).add(scheduleId));
6060
try {
61-
await runManually({ scheduleId });
62-
toast.success("Schedule run successfully");
61+
const result = await runManually({ scheduleId });
62+
if (result.status === "error") {
63+
toast.error("Schedule run failed, check the deployment logs");
64+
} else {
65+
toast.success("Schedule run successfully");
66+
}
6367
await refetchSchedules();
6468
} catch {
6569
toast.error("Error running schedule");

‎apps/dokploy/server/api/routers/schedule.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,17 @@ export const scheduleRouter = createTRPCRouter({
329329
await checkPermission(ctx, { schedule: ["create"] });
330330
}
331331
try {
332-
await runCommand(input.scheduleId);
332+
const deployment = await runCommand(input.scheduleId);
333333
await audit(ctx, {
334334
action: "run",
335335
resourceType: "schedule",
336336
resourceId: input.scheduleId,
337337
});
338-
return true;
338+
return {
339+
status: deployment.status,
340+
deploymentId: deployment.deploymentId,
341+
logPath: deployment.logPath,
342+
};
339343
} catch (error) {
340344
throw new TRPCError({
341345
code: "INTERNAL_SERVER_ERROR",

‎packages/server/src/utils/schedules/utils.ts‎

Lines changed: 78 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,46 @@ export const runCommand = async (scheduleId: string) => {
5555
description: "Schedule",
5656
});
5757

58-
if (scheduleType === "application" || scheduleType === "compose") {
59-
let containerId = "";
60-
let serverId = "";
61-
if (scheduleType === "application" && application) {
62-
const container = await getServiceContainer(
63-
application.appName,
64-
application.serverId,
65-
);
66-
containerId = container?.Id || "";
67-
serverId = application.serverId || "";
68-
}
69-
if (scheduleType === "compose" && compose) {
70-
const container = await getComposeContainer(compose, serviceName || "");
71-
containerId = container?.Id || "";
72-
serverId = compose.serverId || "";
73-
}
58+
try {
59+
if (scheduleType === "application" || scheduleType === "compose") {
60+
let containerId = "";
61+
let serverId = "";
62+
if (scheduleType === "application" && application) {
63+
const container = await getServiceContainer(
64+
application.appName,
65+
application.serverId,
66+
);
67+
containerId = container?.Id || "";
68+
serverId = application.serverId || "";
69+
}
70+
if (scheduleType === "compose" && compose) {
71+
const container = await getComposeContainer(compose, serviceName || "");
72+
containerId = container?.Id || "";
73+
serverId = compose.serverId || "";
74+
}
75+
76+
if (!containerId) {
77+
const target =
78+
scheduleType === "compose"
79+
? `service '${serviceName}' of compose '${compose?.name}'`
80+
: `application '${application?.appName}'`;
81+
const message = `Container not found for ${target}, make sure the service is running`;
82+
if (serverId) {
83+
await execAsyncRemote(
84+
serverId,
85+
`echo ${quote([`❌ ${message}`])} >> ${quote([deployment.logPath])}`,
86+
);
87+
} else {
88+
const writeStream = createWriteStream(deployment.logPath, {
89+
flags: "a",
90+
});
91+
writeStream.write(`❌ ${message}\n`);
92+
writeStream.end();
93+
}
94+
throw new Error(message);
95+
}
7496

75-
if (serverId) {
76-
try {
97+
if (serverId) {
7798
await execAsyncRemote(
7899
serverId,
79100
`
@@ -86,47 +107,44 @@ export const runCommand = async (scheduleId: string) => {
86107
echo "✅ Command executed successfully" >> ${quote([deployment.logPath])};
87108
`,
88109
);
89-
} catch (error) {
90-
await updateDeploymentStatus(deployment.deploymentId, "error");
91-
throw error;
92-
}
93-
} else {
94-
const writeStream = createWriteStream(deployment.logPath, { flags: "a" });
110+
} else {
111+
const writeStream = createWriteStream(deployment.logPath, {
112+
flags: "a",
113+
});
95114

96-
try {
97-
if (IS_CLOUD) {
115+
try {
116+
if (IS_CLOUD) {
117+
writeStream.write(
118+
"This feature is not available in the cloud version.",
119+
);
120+
writeStream.end();
121+
return { ...deployment, status: "running" as const };
122+
}
98123
writeStream.write(
99-
"This feature is not available in the cloud version.",
124+
`docker exec ${containerId} ${shellType} -c ${command}\n`,
125+
);
126+
await spawnAsync(
127+
"docker",
128+
["exec", containerId, shellType, "-c", command],
129+
(data) => {
130+
if (writeStream.writable) {
131+
writeStream.write(data);
132+
}
133+
},
100134
);
135+
136+
writeStream.write("✅ Command executed successfully\n");
101137
writeStream.end();
102-
return;
138+
} catch (error) {
139+
writeStream.write("❌ Command failed\n");
140+
writeStream.write(
141+
error instanceof Error ? error.message : "Unknown error",
142+
);
143+
writeStream.end();
144+
throw error;
103145
}
104-
writeStream.write(
105-
`docker exec ${containerId} ${shellType} -c ${command}\n`,
106-
);
107-
await spawnAsync(
108-
"docker",
109-
["exec", containerId, shellType, "-c", command],
110-
(data) => {
111-
if (writeStream.writable) {
112-
writeStream.write(data);
113-
}
114-
},
115-
);
116-
117-
writeStream.write("✅ Command executed successfully\n");
118-
} catch (error) {
119-
writeStream.write("❌ Command failed\n");
120-
writeStream.write(
121-
error instanceof Error ? error.message : "Unknown error",
122-
);
123-
writeStream.end();
124-
await updateDeploymentStatus(deployment.deploymentId, "error");
125-
throw error;
126146
}
127-
}
128-
} else if (scheduleType === "dokploy-server") {
129-
try {
147+
} else if (scheduleType === "dokploy-server") {
130148
const writeStream = createWriteStream(deployment.logPath, { flags: "a" });
131149
const { SCHEDULES_PATH } = paths();
132150
const fullPath = path.join(SCHEDULES_PATH, appName || "");
@@ -151,18 +169,13 @@ export const runCommand = async (scheduleId: string) => {
151169
cwd: fullPath,
152170
},
153171
);
154-
} catch (error) {
155-
await updateDeploymentStatus(deployment.deploymentId, "error");
156-
throw error;
157-
}
158-
} else if (scheduleType === "server") {
159-
try {
172+
} else if (scheduleType === "server") {
160173
const { SCHEDULES_PATH } = paths(true);
161174
const fullPath = path.join(SCHEDULES_PATH, appName || "");
162175
const command = `
163176
set -e
164177
echo "Running script" >> ${deployment.logPath};
165-
bash -c ${fullPath}/script.sh 2>&1 | tee -a ${deployment.logPath} || {
178+
bash -c ${fullPath}/script.sh 2>&1 | tee -a ${deployment.logPath} || {
166179
echo "❌ Command failed" >> ${deployment.logPath};
167180
exit 1;
168181
}
@@ -177,10 +190,11 @@ export const runCommand = async (scheduleId: string) => {
177190
});
178191
}
179192
});
180-
} catch (error) {
181-
await updateDeploymentStatus(deployment.deploymentId, "error");
182-
throw error;
183193
}
194+
await updateDeploymentStatus(deployment.deploymentId, "done");
195+
return { ...deployment, status: "done" as const };
196+
} catch {
197+
await updateDeploymentStatus(deployment.deploymentId, "error");
198+
return { ...deployment, status: "error" as const };
184199
}
185-
await updateDeploymentStatus(deployment.deploymentId, "done");
186200
};

0 commit comments

Comments
 (0)