mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-07-04 13:35:22 +02:00
feat: add kill process functionality to deployments
- Implemented a new mutation to kill a running deployment process by its PID. - Updated the deployment schema to include a PID field. - Enhanced the deployment service to handle process termination and status updates. - Modified the deployment scripts to echo PID and schedule ID for better tracking. - Added error handling for the kill process operation.
This commit is contained in:
@@ -31,6 +31,7 @@ export const deployments = pgTable("deployment", {
|
||||
description: text("description"),
|
||||
status: deploymentStatus("status").default("running"),
|
||||
logPath: text("logPath").notNull(),
|
||||
pid: text("pid"),
|
||||
applicationId: text("applicationId").references(
|
||||
() => applications.applicationId,
|
||||
{ onDelete: "cascade" },
|
||||
|
||||
@@ -40,6 +40,7 @@ export const findDeploymentById = async (deploymentId: string) => {
|
||||
where: eq(deployments.deploymentId, deploymentId),
|
||||
with: {
|
||||
application: true,
|
||||
schedule: true,
|
||||
},
|
||||
});
|
||||
if (!deployment) {
|
||||
|
||||
@@ -109,7 +109,12 @@ export const updateSchedule = async (
|
||||
const handleScript = async (schedule: Schedule) => {
|
||||
const { SCHEDULES_PATH } = paths(!!schedule?.serverId);
|
||||
const fullPath = path.join(SCHEDULES_PATH, schedule?.appName || "");
|
||||
const encodedContent = encodeBase64(schedule?.script || "");
|
||||
|
||||
// Add PID and Schedule ID echo by default to all scripts
|
||||
const scriptWithPid = `echo "PID: $$ | Schedule ID: ${schedule.scheduleId}"
|
||||
${schedule?.script || ""}`;
|
||||
|
||||
const encodedContent = encodeBase64(scriptWithPid);
|
||||
const script = `
|
||||
mkdir -p ${fullPath}
|
||||
rm -f ${fullPath}/script.sh
|
||||
|
||||
@@ -2,7 +2,10 @@ import { createWriteStream } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { paths } from "@dokploy/server/constants";
|
||||
import type { Schedule } from "@dokploy/server/db/schema/schedule";
|
||||
import { createDeploymentSchedule } from "@dokploy/server/services/deployment";
|
||||
import {
|
||||
createDeploymentSchedule,
|
||||
updateDeployment,
|
||||
} from "@dokploy/server/services/deployment";
|
||||
import { updateDeploymentStatus } from "@dokploy/server/services/deployment";
|
||||
import { findScheduleById } from "@dokploy/server/services/schedule";
|
||||
import { scheduleJob as scheduleJobNode, scheduledJobs } from "node-schedule";
|
||||
@@ -113,8 +116,16 @@ export const runCommand = async (scheduleId: string) => {
|
||||
await spawnAsync(
|
||||
"bash",
|
||||
["-c", "./script.sh"],
|
||||
(data) => {
|
||||
async (data) => {
|
||||
if (writeStream.writable) {
|
||||
// we need to extract the PID and Schedule ID from the data
|
||||
const pid = data.match(/PID: (\d+)/)?.[1];
|
||||
|
||||
if (pid) {
|
||||
await updateDeployment(deployment.deploymentId, {
|
||||
pid,
|
||||
});
|
||||
}
|
||||
writeStream.write(data);
|
||||
}
|
||||
},
|
||||
@@ -133,13 +144,21 @@ export const runCommand = async (scheduleId: string) => {
|
||||
const command = `
|
||||
set -e
|
||||
echo "Running script" >> ${deployment.logPath};
|
||||
bash -c ${fullPath}/script.sh >> ${deployment.logPath} 2>> ${deployment.logPath} || {
|
||||
bash -c ${fullPath}/script.sh 2>&1 | tee -a ${deployment.logPath} || {
|
||||
echo "❌ Command failed" >> ${deployment.logPath};
|
||||
exit 1;
|
||||
}
|
||||
echo "✅ Command executed successfully" >> ${deployment.logPath};
|
||||
`;
|
||||
await execAsyncRemote(serverId, command);
|
||||
await execAsyncRemote(serverId, command, async (data) => {
|
||||
// we need to extract the PID and Schedule ID from the data
|
||||
const pid = data.match(/PID: (\d+)/)?.[1];
|
||||
if (pid) {
|
||||
await updateDeployment(deployment.deploymentId, {
|
||||
pid,
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
await updateDeploymentStatus(deployment.deploymentId, "error");
|
||||
throw error;
|
||||
|
||||
Reference in New Issue
Block a user