This repository was archived by the owner on Oct 8, 2023. It is now read-only.
forked from node-cron/node-cron
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode-cron.mjs
58 lines (49 loc) · 1.54 KB
/
node-cron.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import ScheduledTask from './scheduled-task.mjs'
import validation from './pattern-validation.mjs'
import storage from './storage.mjs'
/**
* @typedef {Object} CronScheduleOptions
* @prop {boolean} [scheduled] if a scheduled task is ready and running to be
* performed when the time matches the cron expression.
* @prop {string} [timezone] the timezone to execute the task in.
*/
/**
* Creates a new task to execute the given function when the cron
* expression ticks.
*
* @param {string} expression The cron expression.
* @param {Function} func The task to be executed.
* @param {CronScheduleOptions} [options] A set of options for the scheduled task.
* @returns {ScheduledTask} The scheduled task.
*/
export const schedule = function (expression, func, options) {
const task = createTask(expression, func, options);
storage.save(task);
return task;
}
function createTask(expression, func, options) {
if (typeof func !== 'function') throw 'callback function must be a function';
return new ScheduledTask(expression, func, options);
}
/**
* Check if a cron expression is valid.
*
* @param {string} expression The cron expression.
* @returns {boolean} Whether the expression is valid or not.
*/
export const validate = function validate(expression) {
try {
validation(expression);
return true;
} catch (_) {
return false;
}
}
/**
* Gets the scheduled tasks.
*
* @returns {ScheduledTask[]} The scheduled tasks.
*/
export const getTasks = function getTasks() {
return storage.getTasks();
}