Skip to content

Class: MilliCron

A simple Cron Daemon which supports a resolution of up to 10ms.

Constructors

Constructor

ts
new MilliCron(autostart?: boolean): MilliCron;

Creates a new instance of the MilliCron class.

Parameters

ParameterTypeDescription
autostart?booleanWhether to automatically start the cron daemon upon instantiation.

Returns

MilliCron

  • A new instance of the MilliCron class.

Accessors

running

Get Signature

ts
get running(): boolean;

Returns a boolean indicating whether the cron daemon is currently running.

Returns

boolean

  • A boolean indicating whether the cron daemon is currently running.

Methods

$clear()

ts
$clear(event?: string): void;

Removes all listeners for the specified event or all events if no event is specified.

Parameters

ParameterTypeDescription
event?stringThe name of the event to remove all listeners from.

Returns

void

See

events!EventEmitter.removeAllListeners | EventEmitter.removeAllListeners


$off()

ts
$off(event: string, cb: EventCallback): void;

Removes a previously registered callback function for the specified event.

Parameters

ParameterTypeDescription
eventstringThe name of the event to remove the callback function from.
cbEventCallbackThe callback function to remove.

Returns

void

See

events!EventEmitter.off | EventEmitter.off


$on()

ts
$on(event: string, cb: EventCallback): void;

Registers a callback function to be executed every time the specified event is emitted.

Parameters

ParameterTypeDescription
eventstringThe name of the event to listen for.
cbEventCallbackThe callback function to execute when the event is emitted.

Returns

void

See

events!EventEmitter.on | EventEmitter.on


$once()

ts
$once(event: string, cb: EventCallback): void;

Registers a callback function to be executed only once when the specified event is emitted.

Parameters

ParameterTypeDescription
eventstringThe name of the event to listen for.
cbEventCallbackThe callback function to execute when the event is emitted.

Returns

void

See

events!EventEmitter.once | EventEmitter.once


crontabMatchesDateTime()

ts
crontabMatchesDateTime(crontab: string, now: DateTime): boolean;

Determines whether the given crontab expression matches the given date and time.

Parameters

ParameterTypeDescription
crontabstringThe crontab expression to check.
nowDateTimeThe date and time to check against the crontab expression.

Returns

boolean

  • A boolean indicating whether the crontab expression matches the given date and time.

getNextRunTimeForCrontab()

ts
getNextRunTimeForCrontab(
   cronExpression: string,
   now?: DateTime<boolean>,
   max?: DateTime<boolean>): DateTime<boolean> | undefined;

Calculate the next run time for a crontab expression. This method takes a crontab expression and calculates the next time it will match, starting from the current time or a specified now time. If no match is found within the specified time range, it returns undefined.

Parameters

ParameterTypeDescription
cronExpressionstringThe crontab expression to parse.
now?DateTime<boolean>The current date and time to use as a reference when calculating the next match time. If not provided, the current date and time will be used.
max?DateTime<boolean>The maximum date and time to use as a reference when calculating the next match time. If not provided, one year from the current date and time will be used.

Returns

DateTime<boolean> | undefined

  • Returns a DateTime object representing the next time the crontab expression will match, or undefined if no match is found within the specified time range.

getNextRunTimeForParsedCrontab()

ts
getNextRunTimeForParsedCrontab(
   crontab:
  | CronTabObject
  | DateTime<boolean>,
   now?: DateTime<boolean>,
   max?: DateTime<boolean>): DateTime<boolean> | undefined;

Calculate the next run time for a parsed crontab expression. This method takes a parsed crontab expression (either as a DateTime object or a CronTabObject) and calculates the next time it will match, starting from the current time or a specified now time. If no match is found within the specified time range, it returns undefined.

Parameters

ParameterTypeDescription
crontab| CronTabObject | DateTime<boolean>The parsed crontab expression as a DateTime object or a CronTabObject.
now?DateTime<boolean>The current time as a DateTime object. If not provided, the current UTC time will be used.
max?DateTime<boolean>The maximum time as a DateTime object. If not provided, one year from the current UTC time will be used.

Returns

DateTime<boolean> | undefined

  • Returns a DateTime object representing the next time the crontab expression will match, or undefined if no match is found within the specified time range.

getParsedCronExpression()

ts
getParsedCronExpression(cronExpression: string, now?: DateTime<boolean>):
  | CronTabObject
| DateTime<boolean>;

Parses the given cron expression and returns either a DateTime object representing the next time the cron expression will match, or a CronTabObject containing the parsed cron expression.

Parameters

ParameterTypeDescription
cronExpressionstringThe cron expression to parse.
now?DateTime<boolean>The current date and time to use as a reference when calculating the next match time. If not provided, the current date and time will be used.

Returns

| CronTabObject | DateTime<boolean>

  • A DateTime object representing the next time the cron expression will match, or a CronTabObject containing the parsed cron expression.

Throws

  • Throws an error if the cron expression is so invalid that it cannot be recognized, or if the now parameter is not a valid DateTime object.

restart()

ts
restart(): void;

Function

Stops the cron daemon if it is running and then starts it again.

Returns

void


start()

ts
start(): void;

Function

Starts the cron daemon if it is not already running.

Returns

void


stop()

ts
stop(): void;

Function

Stops the cron daemon if it is running.

Returns

void


crontabMatchesDateTime()

ts
static crontabMatchesDateTime(crontab: string, now: DateTime): boolean;

Check if the crontab expression matches the current time

Parameters

ParameterTypeDescription
crontabstringA crontab expression, crontab alias or unix timestamp
nowDateTimeA DateTime object representing the current time

Returns

boolean

If the crontab expression matches the current time


getNextRunTimeForCrontab()

ts
static getNextRunTimeForCrontab(
   cronExpression: string,
   now?: DateTime<boolean>,
   max?: DateTime<boolean>): DateTime<boolean> | undefined;

Calculate the next run time for a crontab expression. This method takes a crontab expression and calculates the next time it will match, starting from the current time or a specified now time. If no match is found within the specified time range, it returns undefined.

Parameters

ParameterTypeDescription
cronExpressionstringThe crontab expression to parse.
now?DateTime<boolean>The current date and time to use as a reference when calculating the next match time. If not provided, the current date and time will be used.
max?DateTime<boolean>The maximum date and time to use as a reference when calculating the next match time. If not provided, one year from the current date and time will be used unless the crontab expression resolves to a specific date/time, in which case it will be ignored.

Returns

DateTime<boolean> | undefined

  • Returns a DateTime object representing the next time the crontab expression will match, or undefined if no match is found within the specified time range.

getNextRunTimeForParsedCrontab()

ts
static getNextRunTimeForParsedCrontab(
   crontab:
  | CronTabObject
  | DateTime<boolean>,
   now?: DateTime<boolean>,
   max?: DateTime<boolean>): DateTime<boolean> | undefined;

Calculate the next run time for a parsed crontab expression. This method takes a parsed crontab expression (either as a DateTime object or a CronTabObject) and calculates the next time it will match, starting from the current time or a specified now time. If no match is found within the specified time range, it returns undefined.

Parameters

ParameterTypeDescription
crontab| CronTabObject | DateTime<boolean>The parsed crontab expression as a DateTime object or a CronTabObject.
now?DateTime<boolean>The current time as a DateTime object. If not provided, the current UTC time will be used.
max?DateTime<boolean>The maximum time as a DateTime object. If not provided, one year from the current UTC time will be used unless the crontab is a DateTime object, in which case it will be ignored.

Returns

DateTime<boolean> | undefined

  • Returns a DateTime object representing the next time the crontab expression will match, or undefined if no match is found within the specified time range.

getParsedCronExpression()

ts
static getParsedCronExpression(cronExpression: string, now?: DateTime<boolean>):
  | CronTabObject
| DateTime<boolean>;

Parses the given cron expression and returns either a DateTime object representing the next time the cron expression will match, or a CronTabObject containing the parsed cron expression.

Parameters

ParameterTypeDescription
cronExpressionstringThe cron expression to parse.
now?DateTime<boolean>The current date and time to use as a reference when calculating the next match time. If not provided, the current date and time will be used.

Returns

| CronTabObject | DateTime<boolean>

  • A DateTime object representing the next time the cron expression will match, or a CronTabObject containing the parsed cron expression.

Throws

  • Throws an error if the cron expression is so invalid that it cannot be recognized, or if the now parameter is not a valid DateTime object.