33import {
44 getCronJobs ,
55 addCronJob ,
6- deleteCronJob ,
7- updateCronJob ,
8- pauseCronJob ,
9- resumeCronJob ,
106 cleanupCrontab ,
7+ readUserCrontab ,
8+ writeUserCrontab ,
9+ findJobIndex ,
10+ updateCronJob ,
1111 type CronJob ,
1212} from "@/app/_utils/cronjob-utils" ;
13- import { getAllTargetUsers , getUserInfo } from "@/app/_utils/crontab-utils" ;
13+ import { getAllTargetUsers } from "@/app/_utils/crontab-utils" ;
1414import { revalidatePath } from "next/cache" ;
1515import { getScriptPathForCron } from "@/app/_server/actions/scripts" ;
16- import { exec } from "child_process" ;
17- import { promisify } from "util" ;
1816import { isDocker } from "@/app/_server/actions/global" ;
1917import {
2018 runJobSynchronously ,
2119 runJobInBackground ,
2220} from "@/app/_utils/job-execution-utils" ;
23-
24- const execAsync = promisify ( exec ) ;
21+ import {
22+ pauseJobInLines ,
23+ resumeJobInLines ,
24+ deleteJobInLines ,
25+ } from "@/app/_utils/line-manipulation-utils" ;
26+ import { cleanCrontabContent } from "@/app/_utils/files-manipulation-utils" ;
2527
2628export const fetchCronJobs = async ( ) : Promise < CronJob [ ] > => {
2729 try {
@@ -90,10 +92,22 @@ export const createCronJob = async (
9092} ;
9193
9294export const removeCronJob = async (
93- id : string
95+ jobData : { id : string ; schedule : string ; command : string ; comment ?: string ; user : string }
9496) : Promise < { success : boolean ; message : string ; details ?: string } > => {
9597 try {
96- const success = await deleteCronJob ( id ) ;
98+ const cronContent = await readUserCrontab ( jobData . user ) ;
99+ const lines = cronContent . split ( "\n" ) ;
100+
101+ const jobIndex = findJobIndex ( jobData , lines , jobData . user ) ;
102+
103+ if ( jobIndex === - 1 ) {
104+ return { success : false , message : "Cron job not found in crontab" } ;
105+ }
106+
107+ const newCronEntries = deleteJobInLines ( lines , jobIndex ) ;
108+ const newCron = await cleanCrontabContent ( newCronEntries . join ( "\n" ) ) ;
109+ const success = await writeUserCrontab ( jobData . user , newCron ) ;
110+
97111 if ( success ) {
98112 revalidatePath ( "/" ) ;
99113 return { success : true , message : "Cron job deleted successfully" } ;
@@ -124,8 +138,15 @@ export const editCronJob = async (
124138 return { success : false , message : "Missing required fields" } ;
125139 }
126140
141+ const cronJobs = await getCronJobs ( false ) ;
142+ const job = cronJobs . find ( ( j ) => j . id === id ) ;
143+
144+ if ( ! job ) {
145+ return { success : false , message : "Cron job not found" } ;
146+ }
147+
127148 const success = await updateCronJob (
128- id ,
149+ job ,
129150 schedule ,
130151 command ,
131152 comment ,
@@ -152,7 +173,7 @@ export const cloneCronJob = async (
152173 newComment : string
153174) : Promise < { success : boolean ; message : string ; details ?: string } > => {
154175 try {
155- const cronJobs = await getCronJobs ( ) ;
176+ const cronJobs = await getCronJobs ( false ) ;
156177 const originalJob = cronJobs . find ( ( job ) => job . id === id ) ;
157178
158179 if ( ! originalJob ) {
@@ -183,10 +204,22 @@ export const cloneCronJob = async (
183204} ;
184205
185206export const pauseCronJobAction = async (
186- id : string
207+ jobData : { id : string ; schedule : string ; command : string ; comment ?: string ; user : string }
187208) : Promise < { success : boolean ; message : string ; details ?: string } > => {
188209 try {
189- const success = await pauseCronJob ( id ) ;
210+ const cronContent = await readUserCrontab ( jobData . user ) ;
211+ const lines = cronContent . split ( "\n" ) ;
212+
213+ const jobIndex = findJobIndex ( jobData , lines , jobData . user ) ;
214+
215+ if ( jobIndex === - 1 ) {
216+ return { success : false , message : "Cron job not found in crontab" } ;
217+ }
218+
219+ const newCronEntries = pauseJobInLines ( lines , jobIndex , jobData . id ) ;
220+ const newCron = await cleanCrontabContent ( newCronEntries . join ( "\n" ) ) ;
221+ const success = await writeUserCrontab ( jobData . user , newCron ) ;
222+
190223 if ( success ) {
191224 revalidatePath ( "/" ) ;
192225 return { success : true , message : "Cron job paused successfully" } ;
@@ -204,10 +237,22 @@ export const pauseCronJobAction = async (
204237} ;
205238
206239export const resumeCronJobAction = async (
207- id : string
240+ jobData : { id : string ; schedule : string ; command : string ; comment ?: string ; user : string }
208241) : Promise < { success : boolean ; message : string ; details ?: string } > => {
209242 try {
210- const success = await resumeCronJob ( id ) ;
243+ const cronContent = await readUserCrontab ( jobData . user ) ;
244+ const lines = cronContent . split ( "\n" ) ;
245+
246+ const jobIndex = findJobIndex ( jobData , lines , jobData . user ) ;
247+
248+ if ( jobIndex === - 1 ) {
249+ return { success : false , message : "Cron job not found in crontab" } ;
250+ }
251+
252+ const newCronEntries = resumeJobInLines ( lines , jobIndex , jobData . id ) ;
253+ const newCron = await cleanCrontabContent ( newCronEntries . join ( "\n" ) ) ;
254+ const success = await writeUserCrontab ( jobData . user , newCron ) ;
255+
211256 if ( success ) {
212257 revalidatePath ( "/" ) ;
213258 return { success : true , message : "Cron job resumed successfully" } ;
@@ -257,23 +302,16 @@ export const cleanupCrontabAction = async (): Promise<{
257302} ;
258303
259304export const toggleCronJobLogging = async (
260- id : string
305+ jobData : { id : string ; schedule : string ; command : string ; comment ?: string ; user : string ; logsEnabled ?: boolean }
261306) : Promise < { success : boolean ; message : string ; details ?: string } > => {
262307 try {
263- const cronJobs = await getCronJobs ( ) ;
264- const job = cronJobs . find ( ( j ) => j . id === id ) ;
265-
266- if ( ! job ) {
267- return { success : false , message : "Cron job not found" } ;
268- }
269-
270- const newLogsEnabled = ! job . logsEnabled ;
308+ const newLogsEnabled = ! jobData . logsEnabled ;
271309
272310 const success = await updateCronJob (
273- id ,
274- job . schedule ,
275- job . command ,
276- job . comment || "" ,
311+ jobData ,
312+ jobData . schedule ,
313+ jobData . command ,
314+ jobData . comment || "" ,
277315 newLogsEnabled
278316 ) ;
279317
@@ -309,7 +347,7 @@ export const runCronJob = async (
309347 mode ?: "sync" | "async" ;
310348} > => {
311349 try {
312- const cronJobs = await getCronJobs ( ) ;
350+ const cronJobs = await getCronJobs ( false ) ;
313351 const job = cronJobs . find ( ( j ) => j . id === id ) ;
314352
315353 if ( ! job ) {
@@ -356,7 +394,7 @@ export const executeJob = async (
356394 mode ?: "sync" | "async" ;
357395} > => {
358396 try {
359- const cronJobs = await getCronJobs ( ) ;
397+ const cronJobs = await getCronJobs ( false ) ;
360398 const job = cronJobs . find ( ( j ) => j . id === id ) ;
361399
362400 if ( ! job ) {
@@ -388,13 +426,13 @@ export const executeJob = async (
388426} ;
389427
390428export const backupCronJob = async (
391- id : string
429+ job : CronJob
392430) : Promise < { success : boolean ; message : string ; details ?: string } > => {
393431 try {
394432 const {
395433 backupJobToFile,
396434 } = await import ( "@/app/_utils/backup-utils" ) ;
397- const success = await backupJobToFile ( id ) ;
435+ const success = await backupJobToFile ( job ) ;
398436 if ( success ) {
399437 return { success : true , message : "Cron job backed up successfully" } ;
400438 } else {
0 commit comments