Module RescriptCron.CronJob

Module for creating new cron jobs

type t
val make : [ `CronString of string | `JsDate of Js.Date.t ] -> ((unit -> unit) -> unit) -> ?⁠onComplete:(unit -> unit) -> ?⁠start:bool -> ?⁠timezone:string -> ?⁠context:< .. > Js.t -> ?⁠runOnInit:bool -> ?⁠utcOffset:int -> ?⁠unrefTimeout:bool -> unit -> t

make(cronTime, onTickFunction) returns a cron job that when started will run on the schedule specified by the cron time.

parameter cronTime

The syntax of cronTime can be in cron format (e.g. 2 * * * * * *) or as a Js.Date.t.

parameter onTick

The function that will be run for every job execution (specified by the cron time). It will be passed the function given as onComplete parameter as it's parameter.

parameter onComplete

A function that will fire when the job is stopped with stop, and may also be called by onTick at the end of each run (it is passed to onTick).

parameter start

Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call start in order to start the job (assuming job is the variable you set the cronjob to). This does not immediately fire your onTick function, it just gives you more control over the behavior of your jobs.

parameter timezone

Specify the timezone for the execution. This will modify the actual time relative to your timezone. If the timezone is invalid, an error is thrown. You can check all timezones available at Moment Timezone website. Probably don't use both timezone and utcOffset together or weird things may happen.

parameter context

The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call this.stop() (this is for JS, uncertain how this is in ReasonML/OCaml). However, if you change this you'll have access to the functions and values within your context object.

parameter 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.

parameter utcOffset

Set an UTC offset for your timezone instead of using the timezone parameter. This is for a Moment-object, so see Moment-documentation for more info. Do not use utcOffset and timezone parameter together.

parameter 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 timers#timers_timeout_unref from the NodeJS docs.