feat(schedules): add schedules server

This commit is contained in:
Mauricio Siu
2024-10-05 22:11:38 -06:00
parent 651bf3a303
commit 43555cdabe
19 changed files with 584 additions and 122 deletions
+33 -7
View File
@@ -5,6 +5,7 @@ import {
apiRemoveBackup,
apiUpdateBackup,
} from "@/server/db/schema";
import { removeJob, schedule } from "@/server/utils/backup";
import {
createBackup,
findBackupById,
@@ -20,6 +21,7 @@ import {
findMongoByBackupId,
findMySqlByBackupId,
findPostgresByBackupId,
IS_CLOUD,
} from "@dokploy/builders";
import { TRPCError } from "@trpc/server";
@@ -33,8 +35,16 @@ export const backupRouter = createTRPCRouter({
const backup = await findBackupById(newBackup.backupId);
if (backup.enabled) {
scheduleBackup(backup);
if (IS_CLOUD && backup.enabled) {
await schedule({
cronSchedule: backup.schedule,
backupId: backup.backupId,
type: "backup",
});
} else {
if (backup.enabled) {
scheduleBackup(backup);
}
}
} catch (error) {
throw new TRPCError({
@@ -55,11 +65,19 @@ export const backupRouter = createTRPCRouter({
await updateBackupById(input.backupId, input);
const backup = await findBackupById(input.backupId);
if (backup.enabled) {
removeScheduleBackup(input.backupId);
scheduleBackup(backup);
if (IS_CLOUD && backup.enabled) {
await schedule({
cronSchedule: backup.schedule,
backupId: backup.backupId,
type: "backup",
});
} else {
removeScheduleBackup(input.backupId);
if (backup.enabled) {
removeScheduleBackup(input.backupId);
scheduleBackup(backup);
} else {
removeScheduleBackup(input.backupId);
}
}
} catch (error) {
throw new TRPCError({
@@ -73,7 +91,15 @@ export const backupRouter = createTRPCRouter({
.mutation(async ({ input }) => {
try {
const value = await removeBackupById(input.backupId);
removeScheduleBackup(input.backupId);
if (IS_CLOUD && value) {
removeJob({
backupId: input.backupId,
cronSchedule: value.schedule,
type: "backup",
});
} else {
removeScheduleBackup(input.backupId);
}
return value;
} catch (error) {
throw new TRPCError({
+46
View File
@@ -0,0 +1,46 @@
type QueueJob =
| {
type: "backup";
cronSchedule: string;
backupId: string;
}
| {
type: "server";
cronSchedule: string;
serverId: string;
};
export const schedule = async (job: QueueJob) => {
try {
const result = await fetch(`${process.env.JOBS_URL}/create-backup`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(job),
});
const data = await result.json();
console.log(data);
return data;
} catch (error) {
console.log(error);
throw error;
}
};
export const removeJob = async (job: QueueJob) => {
try {
const result = await fetch(`${process.env.JOBS_URL}/remove-job`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(job),
});
const data = await result.json();
console.log(data);
return data;
} catch (error) {
console.log(error);
throw error;
}
};
@@ -33,8 +33,6 @@ export const setupDockerContainerLogsWebSocketServer = (
const tail = url.searchParams.get("tail");
const serverId = url.searchParams.get("serverId");
const { user, session } = await validateWebSocketRequest(req);
console.log(wssTerm.clients);
console.log(wssTerm.clients.size);
if (!containerId) {
ws.close(4000, "containerId no provided");