Merge pull request #1022 from Dokploy/fix/cron-jobs

fix: add try catch inside of cron jobs
This commit is contained in:
Mauricio Siu
2024-12-29 15:49:28 -06:00
committed by GitHub

View File

@@ -7,7 +7,6 @@ import {
cleanUpSystemPrune, cleanUpSystemPrune,
cleanUpUnusedImages, cleanUpUnusedImages,
} from "../docker/utils"; } from "../docker/utils";
import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
import { sendDockerCleanupNotifications } from "../notifications/docker-cleanup"; import { sendDockerCleanupNotifications } from "../notifications/docker-cleanup";
import { runMariadbBackup } from "./mariadb"; import { runMariadbBackup } from "./mariadb";
import { runMongoBackup } from "./mongo"; import { runMongoBackup } from "./mongo";
@@ -34,18 +33,18 @@ export const initCronJobs = async () => {
const servers = await getAllServers(); const servers = await getAllServers();
for (const server of servers) { for (const server of servers) {
const { appName, serverId, enableDockerCleanup } = server; const { serverId, enableDockerCleanup, name } = server;
if (enableDockerCleanup) { if (enableDockerCleanup) {
scheduleJob(serverId, "0 0 * * *", async () => { scheduleJob(serverId, "0 0 * * *", async () => {
console.log( console.log(
`SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${appName}`, `SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${name}`,
); );
await cleanUpUnusedImages(serverId); await cleanUpUnusedImages(serverId);
await cleanUpDockerBuilder(serverId); await cleanUpDockerBuilder(serverId);
await cleanUpSystemPrune(serverId); await cleanUpSystemPrune(serverId);
await sendDockerCleanupNotifications( await sendDockerCleanupNotifications(
admin.adminId, admin.adminId,
`Docker cleanup for Server ${appName}`, `Docker cleanup for Server ${name} (${serverId})`,
); );
}); });
} }
@@ -53,7 +52,6 @@ export const initCronJobs = async () => {
const pgs = await db.query.postgres.findMany({ const pgs = await db.query.postgres.findMany({
with: { with: {
project: true,
backups: { backups: {
with: { with: {
destination: true, destination: true,
@@ -67,41 +65,23 @@ export const initCronJobs = async () => {
}); });
for (const pg of pgs) { for (const pg of pgs) {
for (const backup of pg.backups) { for (const backup of pg.backups) {
const { schedule, backupId, enabled } = backup; const { schedule, backupId, enabled, database } = backup;
if (enabled) { if (enabled) {
try { console.log(
scheduleJob(backupId, schedule, async () => { `[Backup] Postgres DB ${pg.name} for ${database} Activated`,
console.log( );
`PG-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, scheduleJob(backupId, schedule, async () => {
); console.log(
runPostgresBackup(pg, backup); `PG-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`,
}); );
runPostgresBackup(pg, backup);
await sendDatabaseBackupNotifications({ });
applicationName: pg.name,
projectName: pg.project.name,
databaseType: "postgres",
type: "success",
adminId: pg.project.adminId,
});
} catch (error) {
await sendDatabaseBackupNotifications({
applicationName: pg.name,
projectName: pg.project.name,
databaseType: "postgres",
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
adminId: pg.project.adminId,
});
}
} }
} }
} }
const mariadbs = await db.query.mariadb.findMany({ const mariadbs = await db.query.mariadb.findMany({
with: { with: {
project: true,
backups: { backups: {
with: { with: {
destination: true, destination: true,
@@ -116,40 +96,23 @@ export const initCronJobs = async () => {
for (const maria of mariadbs) { for (const maria of mariadbs) {
for (const backup of maria.backups) { for (const backup of maria.backups) {
const { schedule, backupId, enabled } = backup; const { schedule, backupId, enabled, database } = backup;
if (enabled) { if (enabled) {
try { console.log(
scheduleJob(backupId, schedule, async () => { `[Backup] MariaDB DB ${maria.name} for ${database} Activated`,
console.log( );
`MARIADB-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, scheduleJob(backupId, schedule, async () => {
); console.log(
await runMariadbBackup(maria, backup); `MARIADB-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`,
}); );
await sendDatabaseBackupNotifications({ await runMariadbBackup(maria, backup);
applicationName: maria.name, });
projectName: maria.project.name,
databaseType: "mariadb",
type: "success",
adminId: maria.project.adminId,
});
} catch (error) {
await sendDatabaseBackupNotifications({
applicationName: maria.name,
projectName: maria.project.name,
databaseType: "mariadb",
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
adminId: maria.project.adminId,
});
}
} }
} }
} }
const mongodbs = await db.query.mongo.findMany({ const mongodbs = await db.query.mongo.findMany({
with: { with: {
project: true,
backups: { backups: {
with: { with: {
destination: true, destination: true,
@@ -166,38 +129,19 @@ export const initCronJobs = async () => {
for (const backup of mongo.backups) { for (const backup of mongo.backups) {
const { schedule, backupId, enabled } = backup; const { schedule, backupId, enabled } = backup;
if (enabled) { if (enabled) {
try { console.log(`[Backup] MongoDB DB ${mongo.name} Activated`);
scheduleJob(backupId, schedule, async () => { scheduleJob(backupId, schedule, async () => {
console.log( console.log(
`MONGO-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, `MONGO-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`,
); );
await runMongoBackup(mongo, backup); await runMongoBackup(mongo, backup);
}); });
await sendDatabaseBackupNotifications({
applicationName: mongo.name,
projectName: mongo.project.name,
databaseType: "mongodb",
type: "success",
adminId: mongo.project.adminId,
});
} catch (error) {
await sendDatabaseBackupNotifications({
applicationName: mongo.name,
projectName: mongo.project.name,
databaseType: "mongodb",
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
adminId: mongo.project.adminId,
});
}
} }
} }
} }
const mysqls = await db.query.mysql.findMany({ const mysqls = await db.query.mysql.findMany({
with: { with: {
project: true,
backups: { backups: {
with: { with: {
destination: true, destination: true,
@@ -214,31 +158,13 @@ export const initCronJobs = async () => {
for (const backup of mysql.backups) { for (const backup of mysql.backups) {
const { schedule, backupId, enabled } = backup; const { schedule, backupId, enabled } = backup;
if (enabled) { if (enabled) {
try { console.log(`[Backup] MySQL DB ${mysql.name} Activated`);
scheduleJob(backupId, schedule, async () => { scheduleJob(backupId, schedule, async () => {
console.log( console.log(
`MYSQL-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, `MYSQL-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`,
); );
await runMySqlBackup(mysql, backup); await runMySqlBackup(mysql, backup);
}); });
await sendDatabaseBackupNotifications({
applicationName: mysql.name,
projectName: mysql.project.name,
databaseType: "mysql",
type: "success",
adminId: mysql.project.adminId,
});
} catch (error) {
await sendDatabaseBackupNotifications({
applicationName: mysql.name,
projectName: mysql.project.name,
databaseType: "mysql",
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
adminId: mysql.project.adminId,
});
}
} }
} }
} }