Enhance schedule management and job handling features

- Updated the scheduleRouter to manage job scheduling and removal based on the enabled status of schedules, improving job lifecycle management.
- Refactored the scheduleJob and removeJob utilities to support scheduling and removing jobs for both server and schedule types.
- Introduced a new schema for jobQueue to accommodate schedule jobs, enhancing the flexibility of job definitions.
- Improved the runJobs function to execute scheduled jobs based on their enabled status, ensuring proper execution of active schedules.
- Enhanced the initialization process for schedules to automatically schedule active jobs from the database, streamlining the setup process.
This commit is contained in:
Mauricio Siu
2025-05-03 00:54:01 -06:00
parent c87af312ca
commit ccb141339b
8 changed files with 120 additions and 27 deletions

View File

@@ -3,15 +3,17 @@ import {
cleanUpSystemPrune,
cleanUpUnusedImages,
findBackupById,
findScheduleById,
findServerById,
keepLatestNBackups,
runCommand,
runMariadbBackup,
runMongoBackup,
runMySqlBackup,
runPostgresBackup,
} from "@dokploy/server";
import { db } from "@dokploy/server/dist/db";
import { backups, server } from "@dokploy/server/dist/db/schema";
import { backups, schedules, server } from "@dokploy/server/dist/db/schema";
import { eq } from "drizzle-orm";
import { logger } from "./logger.js";
import { scheduleJob } from "./queue.js";
@@ -57,8 +59,7 @@ export const runJobs = async (job: QueueJob) => {
await runMariadbBackup(mariadb, backup);
await keepLatestNBackups(backup, server.serverId);
}
}
if (job.type === "server") {
} else if (job.type === "server") {
const { serverId } = job;
const server = await findServerById(serverId);
if (server.serverStatus === "inactive") {
@@ -68,6 +69,12 @@ export const runJobs = async (job: QueueJob) => {
await cleanUpUnusedImages(serverId);
await cleanUpDockerBuilder(serverId);
await cleanUpSystemPrune(serverId);
} else if (job.type === "schedule") {
const { scheduleId } = job;
const schedule = await findScheduleById(scheduleId);
if (schedule.enabled) {
await runCommand(schedule.scheduleId);
}
}
} catch (error) {
logger.error(error);
@@ -112,4 +119,17 @@ export const initializeJobs = async () => {
});
}
logger.info({ Quantity: backupsResult.length }, "Backups Initialized");
const schedulesResult = await db.query.schedules.findMany({
where: eq(schedules.enabled, true),
});
for (const schedule of schedulesResult) {
scheduleJob({
scheduleId: schedule.scheduleId,
type: "schedule",
cronSchedule: schedule.cronExpression,
});
}
logger.info({ Quantity: schedulesResult.length }, "Schedules Initialized");
};