@steveojs/scheduler-prisma
Advanced tools
Comparing version 7.0.0 to 7.1.0
@@ -6,3 +6,11 @@ import TypedEventEmitter from 'typed-emitter'; | ||
export declare const isHealthy: (heartbeat: number, timeout: number) => boolean; | ||
export declare const computeNextRunAt: (interval: string, timezone?: string) => string; | ||
export declare const computeNextRuns: (interval: string, { timezone, startDate, count, }?: { | ||
timezone?: string | undefined; | ||
startDate?: string | undefined; | ||
count?: number | undefined; | ||
}) => string[]; | ||
export declare const computeNextRun: (interval: string, { timezone, startDate, }?: { | ||
timezone?: string | undefined; | ||
startDate?: string | undefined; | ||
}) => string; | ||
/** | ||
@@ -9,0 +17,0 @@ * @description Loops through all the job rows with the name provided and publishes the task |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.timestampHelperFactory = exports.resetJob = exports.taskRunner = exports.computeNextRunAt = exports.isHealthy = void 0; | ||
exports.timestampHelperFactory = exports.resetJob = exports.taskRunner = exports.computeNextRun = exports.computeNextRuns = exports.isHealthy = void 0; | ||
const moment_timezone_1 = __importDefault(require("moment-timezone")); | ||
@@ -14,21 +14,75 @@ const rrule_rust_1 = require("rrule-rust"); | ||
exports.isHealthy = isHealthy; | ||
const getValidRule = (recurrence, timezone) => { | ||
const isICalRule = recurrence.includes('DTSTART'); | ||
if (isICalRule) | ||
return recurrence; | ||
let derivedTimezone = timezone !== null && timezone !== void 0 ? timezone : 'Australia/Sydney'; | ||
const rule = recurrence | ||
.split(';') | ||
.filter(b => { | ||
const [key, value] = b.split('='); | ||
if (key === 'TZID') { | ||
derivedTimezone = value; | ||
} | ||
return key !== 'TZID'; | ||
}) | ||
.join(';'); | ||
const timeISO8601 = (0, moment_timezone_1.default)().tz(derivedTimezone).format('YYYYMMDDTHHmmss'); | ||
return `DTSTART;TZID=${derivedTimezone}:${timeISO8601}\nRRULE:${rule}`; | ||
}; | ||
const computeNextRuns = (interval, { | ||
/** | ||
* @description Timezone to compute the next runs | ||
* @default UTC | ||
*/ | ||
timezone = 'UTC', | ||
/** | ||
* @description Start date to compute the next runs | ||
* @default now() | ||
*/ | ||
startDate = (0, moment_timezone_1.default)().toISOString(), | ||
/** | ||
* @description The number of runs to compute | ||
* @default 1 | ||
* @max 30 | ||
*/ | ||
count = 1, } = {}) => { | ||
if (!interval) { | ||
throw new Error('Need a valid interval to compute next runs'); | ||
} | ||
const rule = getValidRule(interval, timezone); | ||
const rrule = rrule_rust_1.RRuleSet.parse(rule); | ||
const runCount = Math.min(count, 30); | ||
const start = (0, moment_timezone_1.default)(startDate).valueOf(); | ||
const end = (0, moment_timezone_1.default)(start).add(SIX_MONTHS_IN_MS, 'ms').valueOf(); | ||
return rrule | ||
.between(start, end, true) | ||
.slice(0, runCount) | ||
.map(run => new Date(run).toISOString()); | ||
}; | ||
exports.computeNextRuns = computeNextRuns; | ||
// interval should be iCal String. | ||
const computeNextRunAt = (interval, timezone = 'UTC') => { | ||
// This function should be in sync with packages/scheduler-sequelize/src/helpers.ts | ||
const computeNextRun = (interval, { | ||
/** | ||
* @description Timezone to compute the next run | ||
* @default UTC | ||
*/ | ||
timezone = 'UTC', | ||
/** | ||
* @description Start date to compute the next run | ||
* @default now() | ||
*/ | ||
startDate = (0, moment_timezone_1.default)().toISOString(), } = {}) => { | ||
if (!interval) { | ||
throw new Error('Invalid interval argument supplied to computeNextRunAt'); | ||
throw new Error('Need a valid interval to compute next run'); | ||
} | ||
const isValidRule = interval.includes('DTSTART'); | ||
if (!isValidRule) { | ||
const rule = interval | ||
.split(';') | ||
.filter(b => !b.includes('TZID')) | ||
.join(';'); | ||
const timeISO8601 = (0, moment_timezone_1.default)().tz(timezone).format('YYYYMMDDTHHmmss'); | ||
const rrule = rrule_rust_1.RRuleSet.parse(`DTSTART;TZID=${timezone}:${timeISO8601}\nRRULE:${rule}\nEXDATE;TZID=${timezone}:${timeISO8601}`); | ||
return new Date(rrule.all(1)[0]).toISOString(); | ||
} | ||
const rrule = rrule_rust_1.RRuleSet.parse(interval); | ||
return new Date(rrule.between(new Date().getTime(), new Date().getTime() + SIX_MONTHS_IN_MS, true)[0]).toISOString(); | ||
const [nextRun] = (0, exports.computeNextRuns)(interval, { | ||
timezone, | ||
startDate, | ||
count: 1, | ||
}); | ||
return nextRun; | ||
}; | ||
exports.computeNextRunAt = computeNextRunAt; | ||
exports.computeNextRun = computeNextRun; | ||
/** | ||
@@ -49,3 +103,5 @@ * @description Loops through all the job rows with the name provided and publishes the task | ||
const resetJob = async (client, job, events) => { | ||
const nextRunAt = (0, exports.computeNextRunAt)(job.repeatInterval, job.timezone); | ||
const nextRunAt = (0, exports.computeNextRun)(job.repeatInterval, { | ||
timezone: job.timezone, | ||
}); | ||
events.emit('reset', job, nextRunAt); | ||
@@ -144,3 +200,5 @@ return client.job.update({ | ||
} | ||
const nextRunAt = (0, exports.computeNextRunAt)(job.repeatInterval, job.timezone); | ||
const nextRunAt = (0, exports.computeNextRun)(job.repeatInterval, { | ||
timezone: job.timezone, | ||
}); | ||
await client.job.update({ | ||
@@ -147,0 +205,0 @@ where: namespace |
{ | ||
"name": "@steveojs/scheduler-prisma", | ||
"version": "7.0.0", | ||
"version": "7.1.0", | ||
"description": "A helper lib for a Prisma/Postgres backed in-process job queue", | ||
@@ -50,11 +50,11 @@ "main": "lib/index.js", | ||
"@types/bunyan": "1.8.8", | ||
"@types/chai": "4.3.11", | ||
"@types/chai": "4.3.16", | ||
"@types/mocha": "8.2.3", | ||
"@types/moment-timezone": "0.5.30", | ||
"@types/node": "17.0.45", | ||
"@types/qs": "6.9.11", | ||
"@types/qs": "6.9.15", | ||
"@types/sinon": "10.0.20", | ||
"@types/uuid": "8.3.4", | ||
"@types/validator": "13.11.7", | ||
"chai": "4.3.10", | ||
"@types/validator": "13.11.10", | ||
"chai": "4.4.1", | ||
"cross-env": "7.0.3", | ||
@@ -61,0 +61,0 @@ "eslint-config-ordermentum": "1.0.6", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
46048
16
992
1
35