mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-19 02:15:17 +00:00
Merge pull request #3811 from Dokploy/3332-zombie-processes-from-failed-log-cleanup
refactor: improve error handling and logging in cron job setup for ba…
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import fs from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
import { paths } from "@dokploy/server/constants";
|
import { paths } from "@dokploy/server/constants";
|
||||||
import {
|
import {
|
||||||
getWebServerSettings,
|
getWebServerSettings,
|
||||||
@@ -12,8 +14,6 @@ export const startLogCleanup = async (
|
|||||||
cronExpression = "0 0 * * *",
|
cronExpression = "0 0 * * *",
|
||||||
): Promise<boolean> => {
|
): Promise<boolean> => {
|
||||||
try {
|
try {
|
||||||
const { DYNAMIC_TRAEFIK_PATH } = paths();
|
|
||||||
|
|
||||||
const existingJob = scheduledJobs[LOG_CLEANUP_JOB_NAME];
|
const existingJob = scheduledJobs[LOG_CLEANUP_JOB_NAME];
|
||||||
if (existingJob) {
|
if (existingJob) {
|
||||||
existingJob.cancel();
|
existingJob.cancel();
|
||||||
@@ -21,10 +21,17 @@ export const startLogCleanup = async (
|
|||||||
|
|
||||||
scheduleJob(LOG_CLEANUP_JOB_NAME, cronExpression, async () => {
|
scheduleJob(LOG_CLEANUP_JOB_NAME, cronExpression, async () => {
|
||||||
try {
|
try {
|
||||||
await execAsync(
|
const { DYNAMIC_TRAEFIK_PATH } = paths();
|
||||||
`tail -n 1000 ${DYNAMIC_TRAEFIK_PATH}/access.log > ${DYNAMIC_TRAEFIK_PATH}/access.log.tmp && mv ${DYNAMIC_TRAEFIK_PATH}/access.log.tmp ${DYNAMIC_TRAEFIK_PATH}/access.log`,
|
const accessLogPath = path.join(DYNAMIC_TRAEFIK_PATH, "access.log");
|
||||||
);
|
|
||||||
|
|
||||||
|
if (!fs.existsSync(accessLogPath)) {
|
||||||
|
console.error("Access log file does not exist");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await execAsync(
|
||||||
|
`tail -n 1000 ${accessLogPath} > ${accessLogPath}.tmp && mv ${accessLogPath}.tmp ${accessLogPath}`,
|
||||||
|
);
|
||||||
await execAsync("docker exec dokploy-traefik kill -USR1 1");
|
await execAsync("docker exec dokploy-traefik kill -USR1 1");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error during log cleanup:", error);
|
console.error("Error during log cleanup:", error);
|
||||||
|
|||||||
@@ -30,15 +30,19 @@ export const initCronJobs = async () => {
|
|||||||
const webServerSettings = await getWebServerSettings();
|
const webServerSettings = await getWebServerSettings();
|
||||||
|
|
||||||
if (webServerSettings?.enableDockerCleanup) {
|
if (webServerSettings?.enableDockerCleanup) {
|
||||||
scheduleJob("docker-cleanup", CLEANUP_CRON_JOB, async () => {
|
try {
|
||||||
console.log(
|
scheduleJob("docker-cleanup", CLEANUP_CRON_JOB, async () => {
|
||||||
`Docker Cleanup ${new Date().toLocaleString()}] Running docker cleanup`,
|
console.log(
|
||||||
);
|
`Docker Cleanup ${new Date().toLocaleString()}] Running docker cleanup`,
|
||||||
|
);
|
||||||
|
|
||||||
await cleanupAll();
|
await cleanupAll();
|
||||||
|
|
||||||
await sendDockerCleanupNotifications(admin.user.id);
|
await sendDockerCleanupNotifications(admin.user.id);
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[Backup] Docker Cleanup Error", error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const servers = await getAllServers();
|
const servers = await getAllServers();
|
||||||
@@ -46,18 +50,22 @@ export const initCronJobs = async () => {
|
|||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
const { serverId, enableDockerCleanup, name } = server;
|
const { serverId, enableDockerCleanup, name } = server;
|
||||||
if (enableDockerCleanup) {
|
if (enableDockerCleanup) {
|
||||||
scheduleJob(serverId, CLEANUP_CRON_JOB, async () => {
|
try {
|
||||||
console.log(
|
scheduleJob(serverId, CLEANUP_CRON_JOB, async () => {
|
||||||
`SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${name}`,
|
console.log(
|
||||||
);
|
`SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${name}`,
|
||||||
|
);
|
||||||
|
|
||||||
await cleanupAll(serverId);
|
await cleanupAll(serverId);
|
||||||
|
|
||||||
await sendDockerCleanupNotifications(
|
await sendDockerCleanupNotifications(
|
||||||
admin.user.id,
|
admin.user.id,
|
||||||
`Docker cleanup for Server ${name} (${serverId})`,
|
`Docker cleanup for Server ${name} (${serverId})`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[Backup] ${error}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,11 +95,15 @@ export const initCronJobs = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (webServerSettings?.logCleanupCron) {
|
if (webServerSettings?.logCleanupCron) {
|
||||||
console.log(
|
try {
|
||||||
"Starting log requests cleanup",
|
console.log(
|
||||||
webServerSettings.logCleanupCron,
|
"Starting log requests cleanup",
|
||||||
);
|
webServerSettings.logCleanupCron,
|
||||||
await startLogCleanup(webServerSettings.logCleanupCron);
|
);
|
||||||
|
await startLogCleanup(webServerSettings.logCleanupCron);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[Backup] Log Cleanup Error", error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user