fix: add try catch inside of cron jobs

This commit is contained in:
Mauricio Siu
2024-12-29 15:35:15 -06:00
parent ba45b27608
commit d65d050494

View File

@@ -36,9 +36,12 @@ export const initCronJobs = async () => {
for (const server of servers) { for (const server of servers) {
const { appName, serverId, enableDockerCleanup } = server; const { appName, serverId, enableDockerCleanup } = server;
if (enableDockerCleanup) { if (enableDockerCleanup) {
console.log(
`Setting up server cleanup schedule for ${appName} with ID ${serverId}`,
);
scheduleJob(serverId, "0 0 * * *", async () => { scheduleJob(serverId, "0 0 * * *", async () => {
console.log( console.log(
`SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${appName}`, `SERVER-CLEANUP[${new Date().toLocaleString()}] Running Cleanup ${appName}`,
); );
await cleanUpUnusedImages(serverId); await cleanUpUnusedImages(serverId);
await cleanUpDockerBuilder(serverId); await cleanUpDockerBuilder(serverId);
@@ -65,36 +68,46 @@ 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 } = backup;
if (enabled) { if (enabled) {
try { console.log(
scheduleJob(backupId, schedule, async () => { `Setting up backup schedule for ${pg.name} with ID ${backupId} and schedule ${schedule}`,
);
scheduleJob(backupId, schedule, async () => {
console.log(
`PG-SERVER[${new Date().toLocaleString()}] Starting Backup ${backupId} for database ${pg.name}`,
);
try {
await runPostgresBackup(pg, backup);
console.log( console.log(
`PG-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, `PG-SERVER[${new Date().toLocaleString()}] Backup completed successfully for ${backupId}`,
); );
runPostgresBackup(pg, backup); await sendDatabaseBackupNotifications({
}); applicationName: pg.name,
projectName: pg.project.name,
await sendDatabaseBackupNotifications({ databaseType: "postgres",
applicationName: pg.name, type: "success",
projectName: pg.project.name, adminId: pg.project.adminId,
databaseType: "postgres", });
type: "success", } catch (error) {
adminId: pg.project.adminId, console.error(
}); `PG-SERVER[${new Date().toLocaleString()}] Backup failed for ${backupId}:`,
} catch (error) { error,
await sendDatabaseBackupNotifications({ );
applicationName: pg.name, await sendDatabaseBackupNotifications({
projectName: pg.project.name, applicationName: pg.name,
databaseType: "postgres", projectName: pg.project.name,
type: "error", databaseType: "postgres",
// @ts-ignore type: "error",
errorMessage: error?.message || "Error message not provided", // @ts-ignore
adminId: pg.project.adminId, errorMessage: error?.message || "Error message not provided",
}); adminId: pg.project.adminId,
} });
}
});
} }
} }
} }
@@ -119,29 +132,46 @@ export const initCronJobs = async () => {
const { schedule, backupId, enabled } = backup; const { schedule, backupId, enabled } = backup;
if (enabled) { if (enabled) {
try { try {
console.log(
`Setting up backup schedule for ${maria.name} with ID ${backupId} and schedule ${schedule}`,
);
scheduleJob(backupId, schedule, async () => { scheduleJob(backupId, schedule, async () => {
console.log( console.log(
`MARIADB-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, `MARIADB-SERVER[${new Date().toLocaleString()}] Starting Backup ${backupId} for database ${maria.name}`,
); );
await runMariadbBackup(maria, backup); try {
}); await runMariadbBackup(maria, backup);
await sendDatabaseBackupNotifications({ console.log(
applicationName: maria.name, `MARIADB-SERVER[${new Date().toLocaleString()}] Backup completed successfully for ${backupId}`,
projectName: maria.project.name, );
databaseType: "mariadb", await sendDatabaseBackupNotifications({
type: "success", applicationName: maria.name,
adminId: maria.project.adminId, projectName: maria.project.name,
databaseType: "mariadb",
type: "success",
adminId: maria.project.adminId,
});
} catch (error) {
console.error(
`MARIADB-SERVER[${new Date().toLocaleString()}] Backup failed for ${backupId}:`,
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,
});
}
}); });
} catch (error) { } catch (error) {
await sendDatabaseBackupNotifications({ console.error(
applicationName: maria.name, `Failed to schedule backup for ${maria.name} with ID ${backupId}:`,
projectName: maria.project.name, error,
databaseType: "mariadb", );
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
adminId: maria.project.adminId,
});
} }
} }
} }
@@ -166,31 +196,41 @@ 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(
scheduleJob(backupId, schedule, async () => { `Setting up backup schedule for ${mongo.name} with ID ${backupId} and schedule ${schedule}`,
console.log( );
`MONGO-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, scheduleJob(backupId, schedule, async () => {
); console.log(
`MONGO-SERVER[${new Date().toLocaleString()}] Starting Backup ${backupId} for database ${mongo.name}`,
);
try {
await runMongoBackup(mongo, backup); await runMongoBackup(mongo, backup);
}); console.log(
await sendDatabaseBackupNotifications({ `MONGO-SERVER[${new Date().toLocaleString()}] Backup completed successfully for ${backupId}`,
applicationName: mongo.name, );
projectName: mongo.project.name, await sendDatabaseBackupNotifications({
databaseType: "mongodb", applicationName: mongo.name,
type: "success", projectName: mongo.project.name,
adminId: mongo.project.adminId, databaseType: "mongodb",
}); type: "success",
} catch (error) { adminId: mongo.project.adminId,
await sendDatabaseBackupNotifications({ });
applicationName: mongo.name, } catch (error) {
projectName: mongo.project.name, console.error(
databaseType: "mongodb", `MONGO-SERVER[${new Date().toLocaleString()}] Backup failed for ${backupId}:`,
type: "error", error,
// @ts-ignore );
errorMessage: error?.message || "Error message not provided", await sendDatabaseBackupNotifications({
adminId: mongo.project.adminId, applicationName: mongo.name,
}); projectName: mongo.project.name,
} databaseType: "mongodb",
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
adminId: mongo.project.adminId,
});
}
});
} }
} }
} }
@@ -214,31 +254,41 @@ 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(
scheduleJob(backupId, schedule, async () => { `Setting up backup schedule for ${mysql.name} with ID ${backupId} and schedule ${schedule}`,
console.log( );
`MYSQL-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, scheduleJob(backupId, schedule, async () => {
); console.log(
`MYSQL-SERVER[${new Date().toLocaleString()}] Starting Backup ${backupId} for database ${mysql.name}`,
);
try {
await runMySqlBackup(mysql, backup); await runMySqlBackup(mysql, backup);
}); console.log(
await sendDatabaseBackupNotifications({ `MYSQL-SERVER[${new Date().toLocaleString()}] Backup completed successfully for ${backupId}`,
applicationName: mysql.name, );
projectName: mysql.project.name, await sendDatabaseBackupNotifications({
databaseType: "mysql", applicationName: mysql.name,
type: "success", projectName: mysql.project.name,
adminId: mysql.project.adminId, databaseType: "mysql",
}); type: "success",
} catch (error) { adminId: mysql.project.adminId,
await sendDatabaseBackupNotifications({ });
applicationName: mysql.name, } catch (error) {
projectName: mysql.project.name, console.error(
databaseType: "mysql", `MYSQL-SERVER[${new Date().toLocaleString()}] Backup failed for ${backupId}:`,
type: "error", error,
// @ts-ignore );
errorMessage: error?.message || "Error message not provided", await sendDatabaseBackupNotifications({
adminId: mysql.project.adminId, applicationName: mysql.name,
}); projectName: mysql.project.name,
} databaseType: "mysql",
type: "error",
// @ts-ignore
errorMessage: error?.message || "Error message not provided",
adminId: mysql.project.adminId,
});
}
});
} }
} }
} }