UNPKG

@directus/api

Version:

Directus is a real-time API and App dashboard for managing SQL database content

27 lines (26 loc) 852 B
import { CronJob, validateCronExpression } from 'cron'; import { SynchronizedClock } from '../synchronization.js'; export function validateCron(rule) { return validateCronExpression(rule).valid; } export function scheduleSynchronizedJob(id, rule, cb) { const clock = new SynchronizedClock(`${id}:${rule}`); const job = CronJob.from({ cronTime: rule, onTick: async (fireDate) => { // Get next execution time for synchronization const nextDate = job.nextDate(); const nextTimestamp = nextDate.toMillis(); const wasSet = await clock.set(nextTimestamp); if (wasSet) { await cb(fireDate); } }, start: true, }); const stop = async () => { job.stop(); await clock.reset(); }; return { stop }; }