Skip to content

Commit 081bda5

Browse files
committed
Improve performance and fix comments
1 parent 9d043b7 commit 081bda5

2 files changed

Lines changed: 113 additions & 17 deletions

File tree

app/_utils/system/cron.ts

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,24 @@ export async function getCronJobs(): Promise<CronJob[]> {
5050
const lines = cronContent.split("\n");
5151
const jobs: CronJob[] = [];
5252
let currentComment = "";
53+
let currentUser = "";
5354
let jobIndex = 0;
5455

5556
lines.forEach((line) => {
5657
const trimmedLine = line.trim();
5758

5859
if (!trimmedLine) return;
5960

61+
if (trimmedLine.startsWith("# User: ")) {
62+
currentUser = trimmedLine.substring(8).trim();
63+
return;
64+
}
65+
66+
if (trimmedLine.startsWith("# System Crontab")) {
67+
currentUser = "system";
68+
return;
69+
}
70+
6071
if (trimmedLine.startsWith("#")) {
6172
currentComment = trimmedLine.substring(1).trim();
6273
return;
@@ -93,11 +104,52 @@ export async function addCronJob(
93104
): Promise<boolean> {
94105
try {
95106
const cronContent = await readCronFiles();
96-
const newEntry = comment
97-
? `# ${comment}\n${schedule} ${command}`
98-
: `${schedule} ${command}`;
99-
const newCron = cronContent + "\n" + newEntry;
100-
await writeCronFiles(newCron);
107+
108+
if (isDocker) {
109+
const lines = cronContent.split("\n");
110+
let hasUserSection = false;
111+
let userSectionEnd = -1;
112+
113+
for (let i = 0; i < lines.length; i++) {
114+
const line = lines[i];
115+
if (line.startsWith("# User: ")) {
116+
hasUserSection = true;
117+
userSectionEnd = i;
118+
for (let j = i + 1; j < lines.length; j++) {
119+
if (lines[j].startsWith("# User: ") || lines[j].startsWith("# System Crontab")) {
120+
userSectionEnd = j - 1;
121+
break;
122+
}
123+
userSectionEnd = j;
124+
}
125+
break;
126+
}
127+
}
128+
129+
if (!hasUserSection) {
130+
const newEntry = comment
131+
? `# User: root\n# ${comment}\n${schedule} ${command}`
132+
: `# User: root\n${schedule} ${command}`;
133+
const newCron = cronContent + "\n" + newEntry;
134+
await writeCronFiles(newCron);
135+
} else {
136+
const newEntry = comment
137+
? `# ${comment}\n${schedule} ${command}`
138+
: `${schedule} ${command}`;
139+
140+
const beforeSection = lines.slice(0, userSectionEnd + 1).join("\n");
141+
const afterSection = lines.slice(userSectionEnd + 1).join("\n");
142+
const newCron = beforeSection + "\n" + newEntry + "\n" + afterSection;
143+
await writeCronFiles(newCron);
144+
}
145+
} else {
146+
const newEntry = comment
147+
? `# ${comment}\n${schedule} ${command}`
148+
: `${schedule} ${command}`;
149+
const newCron = cronContent + "\n" + newEntry;
150+
await writeCronFiles(newCron);
151+
}
152+
101153
return true;
102154
} catch (error) {
103155
console.error("Error adding cron job:", error);

app/_utils/system/docker.ts

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ export async function readCronFilesDocker(): Promise<string> {
141141
try {
142142
const filePath = path.join(crontabDir, file);
143143
const content = await fs.readFile(filePath, "utf-8");
144+
allCronContent += `# User: ${file}\n`;
144145
allCronContent += content;
145-
allCronContent += "\n";
146+
allCronContent += "\n\n";
146147
} catch (fileError) {
147148
console.error(`Error reading crontab for user ${file}:`, fileError);
148149
}
@@ -157,18 +158,61 @@ export async function readCronFilesDocker(): Promise<string> {
157158

158159
export async function writeCronFilesDocker(cronContent: string): Promise<boolean> {
159160
try {
160-
const userCrontabPath = `/host/cron/crontabs/root`;
161-
const content = cronContent + "\n";
161+
// Parse the cron content and distribute to appropriate user crontabs
162+
const lines = cronContent.split("\n");
163+
const userCrontabs: { [key: string]: string[] } = {};
164+
let currentUser = "root"; // Default to root user
165+
let currentContent: string[] = [];
162166

163-
try {
164-
await execAsync(`chown root:root ${userCrontabPath}`);
165-
await execAsync(`chmod 666 ${userCrontabPath}`);
166-
await fs.writeFile(userCrontabPath, content);
167-
await execAsync(`chown 1000:105 ${userCrontabPath}`);
168-
await execAsync(`chmod 600 ${userCrontabPath}`);
169-
} catch (error) {
170-
console.error(`Failed to write crontab:`, error);
171-
return false;
167+
for (const line of lines) {
168+
if (line.startsWith("# User:")) {
169+
// Save previous user's content
170+
if (currentUser && currentContent.length > 0) {
171+
userCrontabs[currentUser] = [...currentContent];
172+
}
173+
currentUser = line.substring(8).trim();
174+
currentContent = [];
175+
} else if (line.startsWith("# System Crontab")) {
176+
// Save previous user's content
177+
if (currentUser && currentContent.length > 0) {
178+
userCrontabs[currentUser] = [...currentContent];
179+
}
180+
currentUser = "system";
181+
currentContent = [];
182+
} else if (currentUser && line.trim()) {
183+
currentContent.push(line);
184+
}
185+
}
186+
187+
// Save the last user's content
188+
if (currentUser && currentContent.length > 0) {
189+
userCrontabs[currentUser] = [...currentContent];
190+
}
191+
192+
// Write to appropriate crontab files
193+
for (const [username, cronJobs] of Object.entries(userCrontabs)) {
194+
if (username === "system") {
195+
const systemContent = cronJobs.join("\n") + "\n";
196+
try {
197+
await fs.writeFile("/host/crontab", systemContent);
198+
} catch (error) {
199+
console.error("Failed to write system crontab:", error);
200+
return false;
201+
}
202+
} else {
203+
const userCrontabPath = `/host/cron/crontabs/${username}`;
204+
const userContent = cronJobs.join("\n") + "\n";
205+
try {
206+
await execAsync(`chown root:root ${userCrontabPath}`);
207+
await execAsync(`chmod 666 ${userCrontabPath}`);
208+
await fs.writeFile(userCrontabPath, userContent);
209+
await execAsync(`chown 1000:105 ${userCrontabPath}`);
210+
await execAsync(`chmod 600 ${userCrontabPath}`);
211+
} catch (error) {
212+
console.error(`Failed to write crontab for user ${username}:`, error);
213+
return false;
214+
}
215+
}
172216
}
173217

174218
return true;

0 commit comments

Comments
 (0)