fix: add missing notifications in cron jobs

This commit is contained in:
Mauricio Siu
2024-12-21 02:45:58 -06:00
parent 1aae523a0b
commit 7a8bb8f71d
2 changed files with 116 additions and 28 deletions

View File

@@ -267,11 +267,11 @@ export const settingsRouter = createTRPCRouter({
message: "You are not authorized to access this admin", message: "You are not authorized to access this admin",
}); });
} }
await updateAdmin(ctx.user.authId, { const adminUpdated = await updateAdmin(ctx.user.authId, {
enableDockerCleanup: input.enableDockerCleanup, enableDockerCleanup: input.enableDockerCleanup,
}); });
if (admin.enableDockerCleanup) { if (adminUpdated?.enableDockerCleanup) {
scheduleJob("docker-cleanup", "0 0 * * *", async () => { scheduleJob("docker-cleanup", "0 0 * * *", async () => {
console.log( console.log(
`Docker Cleanup ${new Date().toLocaleString()}] Running...`, `Docker Cleanup ${new Date().toLocaleString()}] Running...`,

View File

@@ -11,6 +11,8 @@ import { runMariadbBackup } from "./mariadb";
import { runMongoBackup } from "./mongo"; import { runMongoBackup } from "./mongo";
import { runMySqlBackup } from "./mysql"; import { runMySqlBackup } from "./mysql";
import { runPostgresBackup } from "./postgres"; import { runPostgresBackup } from "./postgres";
import { sendDockerCleanupNotifications } from "../notifications/docker-cleanup";
import { sendDatabaseBackupNotifications } from "../notifications/database-backup";
export const initCronJobs = async () => { export const initCronJobs = async () => {
console.log("Setting up cron jobs...."); console.log("Setting up cron jobs....");
@@ -25,14 +27,15 @@ export const initCronJobs = async () => {
await cleanUpUnusedImages(); await cleanUpUnusedImages();
await cleanUpDockerBuilder(); await cleanUpDockerBuilder();
await cleanUpSystemPrune(); await cleanUpSystemPrune();
await sendDockerCleanupNotifications(admin.adminId);
}); });
} }
const servers = await getAllServers(); const servers = await getAllServers();
for (const server of servers) { for (const server of servers) {
const { appName, serverId } = server; const { appName, serverId, enableDockerCleanup } = server;
if (serverId) { 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 ${appName}`,
@@ -40,12 +43,17 @@ export const initCronJobs = async () => {
await cleanUpUnusedImages(serverId); await cleanUpUnusedImages(serverId);
await cleanUpDockerBuilder(serverId); await cleanUpDockerBuilder(serverId);
await cleanUpSystemPrune(serverId); await cleanUpSystemPrune(serverId);
await sendDockerCleanupNotifications(
admin.adminId,
`Docker cleanup for Server ${appName}`,
);
}); });
} }
} }
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,
@@ -61,18 +69,39 @@ export const initCronJobs = async () => {
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) {
scheduleJob(backupId, schedule, async () => { try {
console.log( scheduleJob(backupId, schedule, async () => {
`PG-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, console.log(
); `PG-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`,
runPostgresBackup(pg, backup); );
}); 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,
@@ -89,18 +118,38 @@ export const initCronJobs = async () => {
for (const backup of maria.backups) { for (const backup of maria.backups) {
const { schedule, backupId, enabled } = backup; const { schedule, backupId, enabled } = backup;
if (enabled) { if (enabled) {
scheduleJob(backupId, schedule, async () => { try {
console.log( scheduleJob(backupId, schedule, async () => {
`MARIADB-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, console.log(
); `MARIADB-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`,
await runMariadbBackup(maria, backup); );
}); await runMariadbBackup(maria, backup);
});
await sendDatabaseBackupNotifications({
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,
@@ -117,18 +166,38 @@ 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) {
scheduleJob(backupId, schedule, async () => { try {
console.log( scheduleJob(backupId, schedule, async () => {
`MONGO-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, console.log(
); `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,
@@ -145,12 +214,31 @@ 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) {
scheduleJob(backupId, schedule, async () => { try {
console.log( scheduleJob(backupId, schedule, async () => {
`MYSQL-SERVER[${new Date().toLocaleString()}] Running Backup ${backupId}`, console.log(
); `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,
});
}
} }
} }
} }