Skip to content

Commit f12ecc3

Browse files
authored
fix: don't cancel schedule deployments running outside the panel process on restart (Dokploy#5053)
Fixes Dokploy#4986. initCancelDeployments blindly marked every 'running' deployment as 'cancelled' on boot, including schedule runs whose actual work (docker exec into another container, or SSH to a remote host) is decoupled from the Dokploy process and keeps running after a restart. Now only deployments with no schedule, or schedules of type dokploy-server (a real child process), get cancelled. Also resolve any stale 'running' deployment for a schedule when a new run starts, so restarted panels don't leave ghost 'running' rows forever.
1 parent 3cad115 commit f12ecc3

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎packages/server/src/services/deployment.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,19 @@ export const createDeploymentSchedule = async (
453453
schedule.compose?.serverId ||
454454
schedule.server?.serverId;
455455
await removeLastTenDeployments(deployment.scheduleId, "schedule", serverId);
456+
await db
457+
.update(deployments)
458+
.set({
459+
status: "error",
460+
errorMessage: "Superseded by a new run of this schedule.",
461+
finishedAt: new Date().toISOString(),
462+
})
463+
.where(
464+
and(
465+
eq(deployments.scheduleId, deployment.scheduleId),
466+
eq(deployments.status, "running"),
467+
),
468+
);
456469
try {
457470
const { SCHEDULES_PATH } = paths(!!serverId);
458471
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");

‎packages/server/src/utils/startup/cancel-deployments.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
1-
import { applications, compose, deployments } from "@dokploy/server/db/schema";
1+
import {
2+
applications,
3+
compose,
4+
deployments,
5+
schedules,
6+
} from "@dokploy/server/db/schema";
27
import { eq, inArray } from "drizzle-orm";
38
import { db } from "../../db/index";
49

510
export const initCancelDeployments = async () => {
611
try {
712
console.log("Setting up cancel deployments....");
813

14+
const runningDeployments = await db
15+
.select({
16+
deploymentId: deployments.deploymentId,
17+
scheduleId: deployments.scheduleId,
18+
scheduleType: schedules.scheduleType,
19+
})
20+
.from(deployments)
21+
.leftJoin(schedules, eq(deployments.scheduleId, schedules.scheduleId))
22+
.where(eq(deployments.status, "running"));
23+
24+
const deploymentIdsToCancel = runningDeployments
25+
.filter(
26+
(deployment) =>
27+
!deployment.scheduleId ||
28+
deployment.scheduleType === "dokploy-server",
29+
)
30+
.map((deployment) => deployment.deploymentId);
31+
32+
if (deploymentIdsToCancel.length === 0) {
33+
console.log("Cancelled 0 deployments");
34+
return;
35+
}
36+
937
const result = await db
1038
.update(deployments)
1139
.set({
1240
status: "cancelled",
1341
})
14-
.where(eq(deployments.status, "running"))
42+
.where(inArray(deployments.deploymentId, deploymentIdsToCancel))
1543
.returning();
1644

17-
// Reset the related services so they don't stay stuck in "running".
1845
const applicationIds = [
1946
...new Set(
2047
result

0 commit comments

Comments
 (0)