Skip to content

Commit

Permalink
Merge pull request #631 from Dokploy/canary
Browse files Browse the repository at this point in the history
v0.10.6
  • Loading branch information
Siumauricio authored Oct 31, 2024
2 parents db9136d + 52a19a3 commit 6b4e52f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion apps/dokploy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dokploy",
"version": "v0.10.5",
"version": "v0.10.6",
"private": true,
"license": "Apache-2.0",
"type": "module",
Expand Down
73 changes: 57 additions & 16 deletions packages/server/src/services/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export const createDeployment = async (
const application = await findApplicationById(deployment.applicationId);

try {
// await removeLastTenDeployments(deployment.applicationId);
await removeLastTenDeployments(
deployment.applicationId,
application.serverId,
);
const { LOGS_PATH } = paths(!!application.serverId);
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");
const fileName = `${application.appName}-${formattedDateTime}.log`;
Expand Down Expand Up @@ -106,7 +109,10 @@ export const createDeploymentCompose = async (
) => {
const compose = await findComposeById(deployment.composeId);
try {
// await removeLastTenComposeDeployments(deployment.composeId);
await removeLastTenComposeDeployments(
deployment.composeId,
compose.serverId,
);
const { LOGS_PATH } = paths(!!compose.serverId);
const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss");
const fileName = `${compose.appName}-${formattedDateTime}.log`;
Expand Down Expand Up @@ -181,36 +187,72 @@ export const removeDeploymentsByApplicationId = async (
.returning();
};

const removeLastTenDeployments = async (applicationId: string) => {
const removeLastTenDeployments = async (
applicationId: string,
serverId: string | null,
) => {
const deploymentList = await db.query.deployments.findMany({
where: eq(deployments.applicationId, applicationId),
orderBy: desc(deployments.createdAt),
});

if (deploymentList.length > 10) {
const deploymentsToDelete = deploymentList.slice(10);
for (const oldDeployment of deploymentsToDelete) {
const logPath = path.join(oldDeployment.logPath);
if (existsSync(logPath)) {
await fsPromises.unlink(logPath);
if (serverId) {
let command = "";
for (const oldDeployment of deploymentsToDelete) {
const logPath = path.join(oldDeployment.logPath);

command += `
rm -rf ${logPath};
`;
await removeDeployment(oldDeployment.deploymentId);
}

await execAsyncRemote(serverId, command);
} else {
for (const oldDeployment of deploymentsToDelete) {
const logPath = path.join(oldDeployment.logPath);
if (existsSync(logPath)) {
await fsPromises.unlink(logPath);
}
await removeDeployment(oldDeployment.deploymentId);
}
await removeDeployment(oldDeployment.deploymentId);
}
}
};

const removeLastTenComposeDeployments = async (composeId: string) => {
const removeLastTenComposeDeployments = async (
composeId: string,
serverId: string | null,
) => {
const deploymentList = await db.query.deployments.findMany({
where: eq(deployments.composeId, composeId),
orderBy: desc(deployments.createdAt),
});
if (deploymentList.length > 10) {
const deploymentsToDelete = deploymentList.slice(10);
for (const oldDeployment of deploymentsToDelete) {
const logPath = path.join(oldDeployment.logPath);
if (existsSync(logPath)) {
await fsPromises.unlink(logPath);
if (serverId) {
let command = "";
const deploymentsToDelete = deploymentList.slice(10);
for (const oldDeployment of deploymentsToDelete) {
const logPath = path.join(oldDeployment.logPath);

command += `
rm -rf ${logPath};
`;
await removeDeployment(oldDeployment.deploymentId);
}

await execAsyncRemote(serverId, command);
} else {
const deploymentsToDelete = deploymentList.slice(10);
for (const oldDeployment of deploymentsToDelete) {
const logPath = path.join(oldDeployment.logPath);
if (existsSync(logPath)) {
await fsPromises.unlink(logPath);
}
await removeDeployment(oldDeployment.deploymentId);
}
await removeDeployment(oldDeployment.deploymentId);
}
}
};
Expand Down Expand Up @@ -327,7 +369,6 @@ export const createServerDeployment = async (
}
return deploymentCreate[0];
} catch (error) {
console.log(error);
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error to create the deployment",
Expand Down

0 comments on commit 6b4e52f

Please sign in to comment.