Enhance schedule initialization and management features

- Introduced a new `initSchedules` function to initialize and schedule active schedules from the database, improving the management of scheduled tasks.
- Updated the `createSchedule` and `updateSchedule` functions to handle scheduling jobs based on the enabled status of schedules, ensuring proper job management.
- Refactored the `removeScheduleJob` utility to cancel existing scheduled jobs, enhancing the flexibility of schedule updates.
- Improved the `HandleSchedules` and `ShowSchedules` components by removing unused imports and enhancing the user interface for better clarity and usability.
This commit is contained in:
Mauricio Siu
2025-05-02 23:05:39 -06:00
parent 98d0f1d5bf
commit 3072795232
7 changed files with 71 additions and 16 deletions

View File

@@ -8,8 +8,10 @@ import type {
updateScheduleSchema,
} from "../db/schema/schedule";
import { execAsync, execAsyncRemote } from "../utils/process/execAsync";
import { paths } from "../constants";
import { IS_CLOUD, 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>>;
@@ -26,6 +28,11 @@ export const createSchedule = async (
) {
await handleScript(newSchedule);
}
if (newSchedule?.enabled) {
scheduleJob(newSchedule);
}
return newSchedule;
};
@@ -50,12 +57,16 @@ export const findScheduleById = async (scheduleId: string) => {
export const deleteSchedule = async (scheduleId: string) => {
const schedule = await findScheduleById(scheduleId);
const serverId =
schedule?.serverId ||
schedule?.application?.serverId ||
schedule?.compose?.serverId;
const { SCHEDULES_PATH } = paths(!!serverId);
const { SCHEDULES_PATH } = paths(!!schedule?.serverId);
const fullPath = path.join(SCHEDULES_PATH, schedule?.appName || "");
const command = `rm -rf ${fullPath}`;
if (schedule.serverId) {
await execAsyncRemote(schedule.serverId, command);
if (serverId) {
await execAsyncRemote(serverId, command);
} else {
await execAsync(command);
}
@@ -89,19 +100,32 @@ export const updateSchedule = async (
) {
await handleScript(updatedSchedule);
}
console.log("updatedSchedule", updatedSchedule);
if (IS_CLOUD) {
// scheduleJob(updatedSchedule);
} else {
if (updatedSchedule?.enabled) {
removeScheduleJob(scheduleId);
scheduleJob(updatedSchedule);
} else {
removeScheduleJob(scheduleId);
}
}
return updatedSchedule;
};
const handleScript = async (schedule: Schedule) => {
const { SCHEDULES_PATH } = paths(!!schedule?.serverId);
const fullPath = path.join(SCHEDULES_PATH, schedule?.appName || "");
const encodedContent = encodeBase64(schedule?.script || "");
const script = `
mkdir -p ${fullPath}
rm -f ${fullPath}/script.sh
touch ${fullPath}/script.sh
chmod +x ${fullPath}/script.sh
echo "${schedule?.script}" > ${fullPath}/script.sh
echo "${encodedContent}" | base64 -d > ${fullPath}/script.sh
`;
if (schedule?.scheduleType === "dokploy-server") {