UNPKG

@opengis/fastify-table

Version:

core-plugins

53 lines (41 loc) 1.35 kB
import config from '../../../../config.js'; import logger from '../../logger/getLogger.js'; import pgClients from '../../pg/pgClients.js'; import cronList from '../cronList.js'; import runCron from './runCron.js'; import interval2ms from './interval2ms.js'; /** * interval: * - 02:54 - every day * - 2:03 - every day * - *1:43 - 2 times a day * - *12:03 - 2 times a day * - **:54 - every hour * - **:*3 - every 10 minutes * - 60 - every minute * - 10 * 60 - every 10 minutes */ export default async function addCron(func, interval, pg = pgClients.client) { const { time = {}, disabled = [] } = config.cron || {}; const name = func.name || func.toString().split('/').at(-1).split('\'')[0]; // if (!config.isServer) return; if (disabled.includes(name)) { logger.file('cron', { name, message: 'cron disabled' }); return; } cronList[name] = func; const userInterval = time[name] || interval; const [waitMs, intervalMs] = interval2ms[typeof interval](userInterval); if (intervalMs < 1000) { logger.file('cron', { name, error: `interval ${interval} too small` }); return; } // setTimeout to w8 for the time to start setTimeout(() => { runCron({ pg, func, name }); // interval setInterval(() => { runCron({ pg, func, name }); }, intervalMs); }, waitMs); }