Improve performance and fix comments

This commit is contained in:
fccview
2025-08-18 20:48:03 +01:00
parent 9d043b7111
commit 081bda5247
2 changed files with 113 additions and 17 deletions

View File

@@ -50,6 +50,7 @@ export async function getCronJobs(): Promise<CronJob[]> {
const lines = cronContent.split("\n");
const jobs: CronJob[] = [];
let currentComment = "";
let currentUser = "";
let jobIndex = 0;
lines.forEach((line) => {
@@ -57,6 +58,16 @@ export async function getCronJobs(): Promise<CronJob[]> {
if (!trimmedLine) return;
if (trimmedLine.startsWith("# User: ")) {
currentUser = trimmedLine.substring(8).trim();
return;
}
if (trimmedLine.startsWith("# System Crontab")) {
currentUser = "system";
return;
}
if (trimmedLine.startsWith("#")) {
currentComment = trimmedLine.substring(1).trim();
return;
@@ -93,11 +104,52 @@ export async function addCronJob(
): Promise<boolean> {
try {
const cronContent = await readCronFiles();
const newEntry = comment
? `# ${comment}\n${schedule} ${command}`
: `${schedule} ${command}`;
const newCron = cronContent + "\n" + newEntry;
await writeCronFiles(newCron);
if (isDocker) {
const lines = cronContent.split("\n");
let hasUserSection = false;
let userSectionEnd = -1;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith("# User: ")) {
hasUserSection = true;
userSectionEnd = i;
for (let j = i + 1; j < lines.length; j++) {
if (lines[j].startsWith("# User: ") || lines[j].startsWith("# System Crontab")) {
userSectionEnd = j - 1;
break;
}
userSectionEnd = j;
}
break;
}
}
if (!hasUserSection) {
const newEntry = comment
? `# User: root\n# ${comment}\n${schedule} ${command}`
: `# User: root\n${schedule} ${command}`;
const newCron = cronContent + "\n" + newEntry;
await writeCronFiles(newCron);
} else {
const newEntry = comment
? `# ${comment}\n${schedule} ${command}`
: `${schedule} ${command}`;
const beforeSection = lines.slice(0, userSectionEnd + 1).join("\n");
const afterSection = lines.slice(userSectionEnd + 1).join("\n");
const newCron = beforeSection + "\n" + newEntry + "\n" + afterSection;
await writeCronFiles(newCron);
}
} else {
const newEntry = comment
? `# ${comment}\n${schedule} ${command}`
: `${schedule} ${command}`;
const newCron = cronContent + "\n" + newEntry;
await writeCronFiles(newCron);
}
return true;
} catch (error) {
console.error("Error adding cron job:", error);

View File

@@ -141,8 +141,9 @@ export async function readCronFilesDocker(): Promise<string> {
try {
const filePath = path.join(crontabDir, file);
const content = await fs.readFile(filePath, "utf-8");
allCronContent += `# User: ${file}\n`;
allCronContent += content;
allCronContent += "\n";
allCronContent += "\n\n";
} catch (fileError) {
console.error(`Error reading crontab for user ${file}:`, fileError);
}
@@ -157,18 +158,61 @@ export async function readCronFilesDocker(): Promise<string> {
export async function writeCronFilesDocker(cronContent: string): Promise<boolean> {
try {
const userCrontabPath = `/host/cron/crontabs/root`;
const content = cronContent + "\n";
// 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 currentContent: string[] = [];
try {
await execAsync(`chown root:root ${userCrontabPath}`);
await execAsync(`chmod 666 ${userCrontabPath}`);
await fs.writeFile(userCrontabPath, content);
await execAsync(`chown 1000:105 ${userCrontabPath}`);
await execAsync(`chmod 600 ${userCrontabPath}`);
} catch (error) {
console.error(`Failed to write crontab:`, error);
return false;
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];
}
currentUser = "system";
currentContent = [];
} else if (currentUser && line.trim()) {
currentContent.push(line);
}
}
// 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";
try {
await fs.writeFile("/host/crontab", systemContent);
} catch (error) {
console.error("Failed to write system crontab:", error);
return false;
}
} else {
const userCrontabPath = `/host/cron/crontabs/${username}`;
const userContent = cronJobs.join("\n") + "\n";
try {
await execAsync(`chown root:root ${userCrontabPath}`);
await execAsync(`chmod 666 ${userCrontabPath}`);
await fs.writeFile(userCrontabPath, userContent);
await execAsync(`chown 1000:105 ${userCrontabPath}`);
await execAsync(`chmod 600 ${userCrontabPath}`);
} catch (error) {
console.error(`Failed to write crontab for user ${username}:`, error);
return false;
}
}
}
return true;