mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-06-19 06:05:25 +02:00
- 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.
89 lines
1.8 KiB
TypeScript
89 lines
1.8 KiB
TypeScript
import {
|
|
type BackupScheduleList,
|
|
IS_CLOUD,
|
|
removeScheduleBackup,
|
|
} from "@dokploy/server/index";
|
|
|
|
type QueueJob =
|
|
| {
|
|
type: "backup";
|
|
cronSchedule: string;
|
|
backupId: string;
|
|
}
|
|
| {
|
|
type: "server";
|
|
cronSchedule: string;
|
|
serverId: string;
|
|
}
|
|
| {
|
|
type: "schedule";
|
|
cronSchedule: string;
|
|
scheduleId: string;
|
|
};
|
|
export const schedule = async (job: QueueJob) => {
|
|
try {
|
|
const result = await fetch(`${process.env.JOBS_URL}/create-backup`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-API-Key": process.env.API_KEY || "NO-DEFINED",
|
|
},
|
|
body: JSON.stringify(job),
|
|
});
|
|
const data = await result.json();
|
|
return data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const removeJob = async (job: QueueJob) => {
|
|
try {
|
|
const result = await fetch(`${process.env.JOBS_URL}/remove-job`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-API-Key": process.env.API_KEY || "NO-DEFINED",
|
|
},
|
|
body: JSON.stringify(job),
|
|
});
|
|
const data = await result.json();
|
|
return data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const updateJob = async (job: QueueJob) => {
|
|
try {
|
|
const result = await fetch(`${process.env.JOBS_URL}/update-backup`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-API-Key": process.env.API_KEY || "NO-DEFINED",
|
|
},
|
|
body: JSON.stringify(job),
|
|
});
|
|
const data = await result.json();
|
|
return data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const cancelJobs = async (backups: BackupScheduleList) => {
|
|
for (const backup of backups) {
|
|
if (backup.enabled) {
|
|
if (IS_CLOUD) {
|
|
await removeJob({
|
|
cronSchedule: backup.schedule,
|
|
backupId: backup.backupId,
|
|
type: "backup",
|
|
});
|
|
} else {
|
|
removeScheduleBackup(backup.backupId);
|
|
}
|
|
}
|
|
}
|
|
};
|