Skip to content

Commit 033f4fe

Browse files
committed
feat: add missing cron v4 bindings
Add constructor params added in cron v2/v3/v4: waitForCompletion, errorHandler, name, threshold. Add new method and property bindings: nextDate, isActive, isCallbackRunning, validateCronExpression.
1 parent d09b07d commit 033f4fe

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

src/RescriptCron.res

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ module CronJob = {
1212
Nullable.t<bool>,
1313
Nullable.t<int>,
1414
Nullable.t<bool>,
15+
Nullable.t<bool>,
16+
Nullable.t<JsExn.t => unit>,
17+
Nullable.t<string>,
18+
Nullable.t<float>,
1519
) => t = "CronJob"
1620

1721
let make = (
@@ -22,8 +26,12 @@ module CronJob = {
2226
~timezone: option<string>=?,
2327
~context: option<{..}>=?,
2428
~runOnInit: option<bool>=?,
25-
~utcOffset: option<int>=?, // Could also be string, but that complicates
29+
~utcOffset: option<int>=?,
2630
~unrefTimeout: option<bool>=?,
31+
~waitForCompletion: option<bool>=?,
32+
~errorHandler: option<JsExn.t => unit>=?,
33+
~name: option<string>=?,
34+
~threshold: option<float>=?,
2735
(),
2836
) =>
2937
make(
@@ -36,6 +44,10 @@ module CronJob = {
3644
Nullable.fromOption(runOnInit),
3745
Nullable.fromOption(utcOffset),
3846
Nullable.fromOption(unrefTimeout),
47+
Nullable.fromOption(waitForCompletion),
48+
Nullable.fromOption(errorHandler),
49+
Nullable.fromOption(name),
50+
Nullable.fromOption(threshold),
3951
)
4052
}
4153

@@ -78,3 +90,17 @@ let nextJsDates = (~numberOfDates=1, job: CronJob.t) =>
7890

7991
@send
8092
external addCallback: (CronJob.t, (unit => unit) => unit) => unit = "addCallback"
93+
94+
@send external nextDate: CronJob.t => LuxonDateTime.t = "nextDate"
95+
96+
@get external isActive: CronJob.t => bool = "isActive"
97+
98+
@get external isCallbackRunning: CronJob.t => bool = "isCallbackRunning"
99+
100+
type validateCronResult = {
101+
valid: bool,
102+
error?: JsExn.t,
103+
}
104+
105+
@module("cron") @val
106+
external validateCronExpression: string => validateCronResult = "validateCronExpression"

src/RescriptCron.resi

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ module CronJob: {
1616
@param runOnInit This will immediately fire your [onTick] function as soon as the requisite initialization has happened. This option is set to [false] by default for backwards compatibility.
1717
@param utcOffset Set a UTC offset for your timezone instead of using the [timezone] parameter. Do not use [utcOffset] and [timezone] parameter together.
1818
@param unrefTimeout If you have code that keeps the event loop running and want to stop the node process when that finishes regardless of the state of your cronjob, you can do so making use of this parameter. This is off by default and cron will run as if it needs to control the event loop. For more information take a look at {{: https://nodejs.org/api/timers.html#timers_timeout_unref } timers#timers_timeout_unref } from the NodeJS docs.
19+
@param waitForCompletion If set to [true], the job will wait for the previous execution to complete before starting a new one.
20+
@param errorHandler A function called with the error if [onTick] throws. Defaults to [None].
21+
@param name An optional name for the job, useful for debugging.
22+
@param threshold An optional threshold in milliseconds. If the job is overdue by more than this value it will not fire.
1923
")
2024
let make: (
2125
[#CronString(string) | #JsDate(Date.t)],
@@ -27,6 +31,10 @@ module CronJob: {
2731
~runOnInit: bool=?,
2832
~utcOffset: int=?,
2933
~unrefTimeout: bool=?,
34+
~waitForCompletion: bool=?,
35+
~errorHandler: JsExn.t => unit=?,
36+
~name: string=?,
37+
~threshold: float=?,
3038
unit,
3139
) => t
3240
}
@@ -104,3 +112,32 @@ external fireOnTick: CronJob.t => unit = "fireOnTick"
104112
)
105113
@send
106114
external addCallback: (CronJob.t, (unit => unit) => unit) => unit = "addCallback"
115+
116+
@ocaml.doc(
117+
" [nextDate(cronJob)] returns the next {!type:LuxonDateTime.t} that will trigger an [onTick] "
118+
)
119+
@send
120+
external nextDate: CronJob.t => LuxonDateTime.t = "nextDate"
121+
122+
@ocaml.doc(" [isActive(cronJob)] returns [true] if the job is currently running ")
123+
@get
124+
external isActive: CronJob.t => bool = "isActive"
125+
126+
@ocaml.doc(" [isCallbackRunning(cronJob)] returns [true] if an [onTick] callback is currently executing ")
127+
@get
128+
external isCallbackRunning: CronJob.t => bool = "isCallbackRunning"
129+
130+
@ocaml.doc(
131+
" Result type returned by {!val:validateCronExpression}. [valid] is [true] if the expression is valid, [error] is set if it is not. "
132+
)
133+
type validateCronResult = {
134+
valid: bool,
135+
error?: JsExn.t,
136+
}
137+
138+
@ocaml.doc(
139+
" [validateCronExpression(expr)] validates a cron expression string. Returns a {!type:validateCronResult} with [valid] set to [true] if the expression is valid. "
140+
)
141+
@module("cron")
142+
@val
143+
external validateCronExpression: string => validateCronResult = "validateCronExpression"

0 commit comments

Comments
 (0)