UNPKG

guanaco

Version:

A cute and cuddly cryptocurrency quant trading engine in TypeScript.

86 lines (85 loc) 3.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const node_cron_1 = require("node-cron"); const models_1 = require("../models"); const utils_1 = require("../utils"); /** * Schedule market actions ahead of time. */ class Scheduler { /** * Run a function on a second-by-second basis. * @param callback The function to call. * @param every The interval, in seconds. */ static secondInterval(callback, every) { return this.schedule(callback, null, null, null, null, null, new models_1.Interval(Scheduler.WILDCARD, every)); } /** * Run a function in a minutely interval. * @param callback The function to call. * @param every The interval, in minutes. */ static minuteInterval(callback, every) { return this.schedule(callback, null, null, null, null, new models_1.Interval(Scheduler.WILDCARD, every)); } /** * Run a function in an hourly interval. * @param callback The function to call. * @param every The interval, in hours. */ static hourInterval(callback, every) { return this.schedule(callback, null, null, null, new models_1.Interval(Scheduler.WILDCARD, every)); } /** * Run a function on a daily basis. * @param callback The function to call. * @param every The interval, in number of days. */ static dailyInterval(callback, every) { return this.schedule(callback, null, null, new models_1.Interval(Scheduler.WILDCARD, every), null, null); } /** * Schedule a function called precisely at a certain time, range, or interval. * @param callback The function to call. * @param dayOfWeek The day of the week to call the function. Leave undefined or null for every day of the week. * @param month The month to call the function. Leave undefined or null for every month of the year. * @param day The day of the month to call the function. Leave undefined or null for every day of the month. * @param hour The hour of the day to call the function. Leave undefined or null for every hour of the day. * @param minute The minute of the hour to call the function. Leave undefined or null for every minute of every hour. * @param second The second of the minute to call the function. Leave undefined or null for every second of every minute. */ static schedule(callback, dayOfWeek = Scheduler.WILDCARD, month = Scheduler.WILDCARD, day = Scheduler.WILDCARD, hour = Scheduler.WILDCARD, minute = Scheduler.WILDCARD, second) { let cronExpression = ''; if (second !== undefined) { cronExpression += `${second} `; } cronExpression += `${utils_1.Condition.null(minute) ? Scheduler.WILDCARD : minute} `; cronExpression += `${utils_1.Condition.null(hour) ? Scheduler.WILDCARD : hour} `; cronExpression += `${utils_1.Condition.null(day) ? Scheduler.WILDCARD : day} `; cronExpression += `${utils_1.Condition.null(month) ? Scheduler.WILDCARD : month} `; cronExpression += `${utils_1.Condition.null(dayOfWeek) ? Scheduler.WILDCARD : dayOfWeek}`; // <-- No extra space here this.validate(cronExpression); return this.perform(callback, cronExpression); } /** * Validate a CRON expression. * @param cronExpression The CRON expression to validate. */ static validate(cronExpression) { const isExpressionValid = node_cron_1.validate(cronExpression); if (!isExpressionValid) { throw new Error(`Invalid CRON expression: '${cronExpression}'`); } } /** * Schedule a valid CRON expression. * @param callback The function to call. * @param cronExpression The CRON expression to perform. */ static perform(callback, cronExpression) { return node_cron_1.schedule(cronExpression, callback); } } Scheduler.WILDCARD = '*'; exports.Scheduler = Scheduler;