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

@@ -16,27 +16,74 @@ import {
createSchedule,
updateSchedule,
} from "@dokploy/server/services/schedule";
import { IS_CLOUD, scheduleJob } from "@dokploy/server";
import { removeJob, schedule } from "@/server/utils/backup";
import { removeScheduleJob } from "@dokploy/server";
export const scheduleRouter = createTRPCRouter({
create: protectedProcedure
.input(createScheduleSchema)
.mutation(async ({ input }) => {
const schedule = await createSchedule(input);
return schedule;
const newSchedule = await createSchedule(input);
if (newSchedule?.enabled) {
if (IS_CLOUD) {
schedule({
scheduleId: newSchedule.scheduleId,
type: "schedule",
cronSchedule: newSchedule.cronExpression,
});
} else {
scheduleJob(newSchedule);
}
}
return newSchedule;
}),
update: protectedProcedure
.input(updateScheduleSchema)
.mutation(async ({ input }) => {
const schedule = await updateSchedule(input);
return schedule;
const updatedSchedule = await updateSchedule(input);
if (IS_CLOUD) {
if (updatedSchedule?.enabled) {
schedule({
scheduleId: updatedSchedule.scheduleId,
type: "schedule",
cronSchedule: updatedSchedule.cronExpression,
});
} else {
await removeJob({
cronSchedule: updatedSchedule.cronExpression,
scheduleId: updatedSchedule.scheduleId,
type: "schedule",
});
}
} else {
if (updatedSchedule?.enabled) {
removeScheduleJob(updatedSchedule.scheduleId);
scheduleJob(updatedSchedule);
} else {
removeScheduleJob(updatedSchedule.scheduleId);
}
}
return updatedSchedule;
}),
delete: protectedProcedure
.input(z.object({ scheduleId: z.string() }))
.mutation(async ({ input }) => {
const schedule = await findScheduleById(input.scheduleId);
await deleteSchedule(input.scheduleId);
if (IS_CLOUD) {
await removeJob({
cronSchedule: schedule.cronExpression,
scheduleId: schedule.scheduleId,
type: "schedule",
});
} else {
removeScheduleJob(schedule.scheduleId);
}
return true;
}),

View File

@@ -14,6 +14,11 @@ type QueueJob =
type: "server";
cronSchedule: string;
serverId: string;
}
| {
type: "schedule";
cronSchedule: string;
scheduleId: string;
};
export const schedule = async (job: QueueJob) => {
try {