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

@@ -30,6 +30,7 @@ export * from "./services/github";
export * from "./services/gitlab";
export * from "./services/gitea";
export * from "./services/server";
export * from "./services/schedule";
export * from "./services/application";
export * from "./utils/databases/rebuild";
export * from "./setup/config-paths";

View File

@@ -8,10 +8,9 @@ import type {
updateScheduleSchema,
} from "../db/schema/schedule";
import { execAsync, execAsyncRemote } from "../utils/process/execAsync";
import { IS_CLOUD, paths } from "../constants";
import { paths } from "../constants";
import path from "node:path";
import { encodeBase64 } from "../utils/docker/utils";
import { scheduleJob, removeScheduleJob } from "../utils/schedules/utils";
export type ScheduleExtended = Awaited<ReturnType<typeof findScheduleById>>;
@@ -29,10 +28,6 @@ export const createSchedule = async (
await handleScript(newSchedule);
}
if (newSchedule?.enabled) {
scheduleJob(newSchedule);
}
return newSchedule;
};
@@ -94,6 +89,13 @@ export const updateSchedule = async (
.where(eq(schedules.scheduleId, scheduleId))
.returning();
if (!updatedSchedule) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Schedule not found",
});
}
if (
updatedSchedule?.scheduleType === "dokploy-server" ||
updatedSchedule?.scheduleType === "server"
@@ -101,16 +103,6 @@ export const updateSchedule = async (
await handleScript(updatedSchedule);
}
if (IS_CLOUD) {
// scheduleJob(updatedSchedule);
} else {
if (updatedSchedule?.enabled) {
removeScheduleJob(scheduleId);
scheduleJob(updatedSchedule);
} else {
removeScheduleJob(scheduleId);
}
}
return updatedSchedule;
};