Skip to content

Commit 336bf8c

Browse files
committed
cron: Add 'unique' versions of addRepeatForeverJob / addRepeatUntilJob [#100]
1 parent 1a11ee3 commit 336bf8c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/cron.q

+36
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,24 @@
199199
:.cron.add[func;args;`repeat;startTime;0Wp;interval];
200200
};
201201

202+
/ Schedules a job that repeats forever but only if there isn't an active job with the same function and arguments
203+
/ @param uFunc (Symbol) Symbol reference to the function to execute
204+
/ @param uArgs () Any arguments that are required to execute the function. Pass generic null (::) for no arguments
205+
/ @param startTime (Timestamp) The first time the job will be run
206+
/ @param interval (Timespan) The interval at which repeating jobs should recur. Pass null (0Nn) for one time jobs
207+
/ @returns (Long) The job ID either of the existing job, or the newly scheduled job
208+
/ @see .cron.addRepeatForeverJob
209+
.cron.addUniqueRepeatForeverJob:{[uFunc;uArgs;startTime;interval]
210+
match:exec from .cron.jobs where func = uFunc, args ~\: uArgs, not 0Wp = nextRunTime;
211+
212+
if[not null match`id;
213+
.log.if.info ("Cron job with matching function and arguments is active. Not adding job [ Function: {} ] [ Arguments: {} ]"; uFunc; uArgs);
214+
:match`id;
215+
];
216+
217+
:.cron.addRepeatForeverJob[uFunc; uArgs; startTime; interval];
218+
};
219+
202220
/ Shortcut function to add a job that repeats until a specified time
203221
/ @param func (Symbol) Symbol reference to the function to execute
204222
/ @param args () Any arguments that are required to execute the function. Pass generic null (::) for no arguments
@@ -210,6 +228,24 @@
210228
:.cron.add[func;args;`repeat;startTime;endTime;interval];
211229
};
212230

231+
/ Schedules a job that repeats until the specified time but only if there isn't an active job with the same function and arguments
232+
/ @param uFunc (Symbol) Symbol reference to the function to execute
233+
/ @param uArgs () Any arguments that are required to execute the function. Pass generic null (::) for no arguments
234+
/ @param startTime (Timestamp) The first time the job will be run
235+
/ @param endTime (Timestamp) The time to finish a repeating job executing. Pass null (0Np) to repeat forever or for one time jobs
236+
/ @param interval (Timespan) The interval at which repeating jobs should recur. Pass null (0Nn) for one time jobs
237+
/ @see .cron.addRepeatUntilTimeJob
238+
.cron.addUniqueRepeatUntilTimeJob:{[uFunc;uArgs;startTime;endTime;interval]
239+
match:exec from .cron.jobs where func = uFunc, args ~\: uArgs, not 0Wp = nextRunTime;
240+
241+
if[not null match`id;
242+
.log.if.info ("Cron job with matching function and arguments is active. Not adding job [ Function: {} ] [ Arguments: {} ]"; uFunc; uArgs);
243+
:match`id;
244+
];
245+
246+
:.cron.addRepeatUntilTimeJob[uFunc; uArgs; startTime; endTime; interval];
247+
};
248+
213249
/ Cancels the specified job from running. Run once jobs will never run and repeating jobs will no longer run
214250
/ @param jobId (Long) The ID of the job to cancel
215251
/ @throws InvalidCronJobException If the ID of the job does not exist

0 commit comments

Comments
 (0)