mirror of
https://github.com/fccview/cronmaster.git
synced 2026-05-07 14:22:45 -04:00
Improve performance and fix comments
This commit is contained in:
@@ -166,13 +166,20 @@ export async function deleteCronJob(id: string): Promise<boolean> {
|
||||
let jobIndex = 0;
|
||||
let targetJobIndex = parseInt(id.replace("unix-", ""));
|
||||
|
||||
lines.forEach((line) => {
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
const trimmedLine = line.trim();
|
||||
|
||||
if (!trimmedLine) return;
|
||||
if (!trimmedLine) continue;
|
||||
|
||||
if (trimmedLine.startsWith("#")) {
|
||||
currentComment = trimmedLine;
|
||||
if (trimmedLine.startsWith("# User:") || trimmedLine.startsWith("# System Crontab")) {
|
||||
cronEntries.push(trimmedLine);
|
||||
} else if (trimmedLine.startsWith("#")) {
|
||||
if (i + 1 < lines.length && !lines[i + 1].trim().startsWith("#") && lines[i + 1].trim()) {
|
||||
currentComment = trimmedLine;
|
||||
} else {
|
||||
cronEntries.push(trimmedLine);
|
||||
}
|
||||
} else {
|
||||
if (jobIndex !== targetJobIndex) {
|
||||
const entryWithComment = currentComment
|
||||
@@ -183,7 +190,7 @@ export async function deleteCronJob(id: string): Promise<boolean> {
|
||||
jobIndex++;
|
||||
currentComment = "";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const newCron = cronEntries.join("\n") + "\n";
|
||||
await writeCronFiles(newCron);
|
||||
|
||||
@@ -158,22 +158,19 @@ export async function readCronFilesDocker(): Promise<string> {
|
||||
|
||||
export async function writeCronFilesDocker(cronContent: string): Promise<boolean> {
|
||||
try {
|
||||
// Parse the cron content and distribute to appropriate user crontabs
|
||||
const lines = cronContent.split("\n");
|
||||
const userCrontabs: { [key: string]: string[] } = {};
|
||||
let currentUser = "root"; // Default to root user
|
||||
let currentUser = "root";
|
||||
let currentContent: string[] = [];
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("# User:")) {
|
||||
// Save previous user's content
|
||||
if (currentUser && currentContent.length > 0) {
|
||||
userCrontabs[currentUser] = [...currentContent];
|
||||
}
|
||||
currentUser = line.substring(8).trim();
|
||||
currentContent = [];
|
||||
} else if (line.startsWith("# System Crontab")) {
|
||||
// Save previous user's content
|
||||
if (currentUser && currentContent.length > 0) {
|
||||
userCrontabs[currentUser] = [...currentContent];
|
||||
}
|
||||
@@ -184,12 +181,10 @@ export async function writeCronFilesDocker(cronContent: string): Promise<boolean
|
||||
}
|
||||
}
|
||||
|
||||
// Save the last user's content
|
||||
if (currentUser && currentContent.length > 0) {
|
||||
userCrontabs[currentUser] = [...currentContent];
|
||||
}
|
||||
|
||||
// Write to appropriate crontab files
|
||||
for (const [username, cronJobs] of Object.entries(userCrontabs)) {
|
||||
if (username === "system") {
|
||||
const systemContent = cronJobs.join("\n") + "\n";
|
||||
|
||||
Reference in New Issue
Block a user