refactor(multi-server): add docker cleanup cron

This commit is contained in:
Mauricio Siu 2024-09-21 22:47:19 -06:00
parent 82588f3e16
commit ff482ffe28
3 changed files with 22 additions and 4 deletions

View File

@ -29,9 +29,7 @@ interface Props {
}
export const SetupServer = ({ serverId }: Props) => {
const utils = api.useUtils();
const [isOpen, setIsOpen] = useState(false);
const url = useUrl();
const { data: server } = api.server.one.useQuery(
{
serverId,

View File

@ -1,6 +1,5 @@
import { db } from "@/server/db";
import { type apiCreateServer, server } from "@/server/db/schema";
import { generatePassword } from "@/templates/utils";
import { TRPCError } from "@trpc/server";
import { eq } from "drizzle-orm";
@ -15,7 +14,6 @@ export const createServer = async (
.values({
...input,
adminId: adminId,
redisPassword: generatePassword(12),
})
.returning()
.then((value) => value[0]);
@ -72,3 +70,8 @@ export const updateServerById = async (
return result;
};
export const getAllServers = async () => {
const servers = await db.query.server.findMany();
return servers;
};

View File

@ -10,6 +10,7 @@ import { runMariadbBackup } from "./mariadb";
import { runMongoBackup } from "./mongo";
import { runMySqlBackup } from "./mysql";
import { runPostgresBackup } from "./postgres";
import { getAllServers } from "@/server/api/services/server";
export const initCronJobs = async () => {
console.log("Setting up cron jobs....");
@ -27,6 +28,22 @@ export const initCronJobs = async () => {
});
}
const servers = await getAllServers();
for (const server of servers) {
const { appName, serverId } = server;
if (serverId) {
scheduleJob(serverId, "0 0 * * *", async () => {
console.log(
`SERVER-BACKUP[${new Date().toLocaleString()}] Running Cleanup ${appName}`,
);
await cleanUpUnusedImages(serverId);
await cleanUpDockerBuilder(serverId);
await cleanUpSystemPrune(serverId);
});
}
}
const pgs = await db.query.postgres.findMany({
with: {
backups: {