mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor:add remote cron jobs
This commit is contained in:
@@ -21,6 +21,7 @@ import {
|
|||||||
execAsync,
|
execAsync,
|
||||||
execAsyncRemote,
|
execAsyncRemote,
|
||||||
findAdmin,
|
findAdmin,
|
||||||
|
findAdminById,
|
||||||
findServerById,
|
findServerById,
|
||||||
getDokployImage,
|
getDokployImage,
|
||||||
initializeTraefik,
|
initializeTraefik,
|
||||||
@@ -64,6 +65,7 @@ import {
|
|||||||
protectedProcedure,
|
protectedProcedure,
|
||||||
publicProcedure,
|
publicProcedure,
|
||||||
} from "../trpc";
|
} from "../trpc";
|
||||||
|
import { removeJob, schedule } from "@/server/utils/backup";
|
||||||
|
|
||||||
export const settingsRouter = createTRPCRouter({
|
export const settingsRouter = createTRPCRouter({
|
||||||
reloadServer: adminProcedure.mutation(async () => {
|
reloadServer: adminProcedure.mutation(async () => {
|
||||||
@@ -205,22 +207,56 @@ export const settingsRouter = createTRPCRouter({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const server = await findServerById(input.serverId);
|
const server = await findServerById(input.serverId);
|
||||||
if (server.enableDockerCleanup) {
|
|
||||||
scheduleJob("docker-cleanup", "0 0 * * *", async () => {
|
if (server.adminId !== ctx.user.adminId) {
|
||||||
console.log(
|
throw new TRPCError({
|
||||||
`Docker Cleanup ${new Date().toLocaleString()}] Running...`,
|
code: "UNAUTHORIZED",
|
||||||
);
|
message: "You are not authorized to access this server",
|
||||||
await cleanUpUnusedImages(server.serverId);
|
|
||||||
await cleanUpDockerBuilder(server.serverId);
|
|
||||||
await cleanUpSystemPrune(server.serverId);
|
|
||||||
await sendDockerCleanupNotifications();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (server.enableDockerCleanup) {
|
||||||
|
if (IS_CLOUD) {
|
||||||
|
await schedule({
|
||||||
|
cronSchedule: "0 0 * * *",
|
||||||
|
serverId: input.serverId,
|
||||||
|
type: "server",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
scheduleJob(server.serverId, "0 0 * * *", async () => {
|
||||||
|
console.log(
|
||||||
|
`Docker Cleanup ${new Date().toLocaleString()}] Running...`,
|
||||||
|
);
|
||||||
|
await cleanUpUnusedImages(server.serverId);
|
||||||
|
await cleanUpDockerBuilder(server.serverId);
|
||||||
|
await cleanUpSystemPrune(server.serverId);
|
||||||
|
await sendDockerCleanupNotifications();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (IS_CLOUD) {
|
||||||
|
await removeJob({
|
||||||
|
cronSchedule: "0 0 * * *",
|
||||||
|
serverId: input.serverId,
|
||||||
|
type: "server",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
const currentJob = scheduledJobs[server.serverId];
|
||||||
|
currentJob?.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (!IS_CLOUD) {
|
} else if (!IS_CLOUD) {
|
||||||
|
const admin = await findAdminById(ctx.user.adminId);
|
||||||
|
|
||||||
|
if (admin.adminId !== ctx.user.adminId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "UNAUTHORIZED",
|
||||||
|
message: "You are not authorized to access this admin",
|
||||||
|
});
|
||||||
|
}
|
||||||
await updateAdmin(ctx.user.authId, {
|
await updateAdmin(ctx.user.authId, {
|
||||||
enableDockerCleanup: input.enableDockerCleanup,
|
enableDockerCleanup: input.enableDockerCleanup,
|
||||||
});
|
});
|
||||||
const admin = await findAdmin();
|
|
||||||
|
|
||||||
if (admin.enableDockerCleanup) {
|
if (admin.enableDockerCleanup) {
|
||||||
scheduleJob("docker-cleanup", "0 0 * * *", async () => {
|
scheduleJob("docker-cleanup", "0 0 * * *", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user