UNPKG

@penkov/tasks_queue

Version:

A lightweight PostgreSQL-backed task queue system with scheduling, retries, backoff strategies, and priority handling. Designed for efficiency and observability in modern Node.js applications.

25 lines (24 loc) 1.01 kB
import { option } from "scats"; import { CronExpressionParser } from "cron-parser"; export class CronExpressionUtils { static DEFAULT_TIMEZONE = "UTC"; static validate(expression, timezone = CronExpressionUtils.DEFAULT_TIMEZONE) { this.nextExecutionDate(expression, new Date(), timezone); } static nextExecutionDate(expression, baseDate, timezone = CronExpressionUtils.DEFAULT_TIMEZONE) { const normalizedExpression = expression.trim(); if (normalizedExpression.length === 0) { throw new Error("Cron expression must not be blank"); } try { const parsed = CronExpressionParser.parse(normalizedExpression, { currentDate: baseDate, tz: option(timezone).filter((s) => s.trim().length > 0).orUndefined, }); return parsed.next().toDate(); } catch (e) { throw new Error(`Invalid cron expression '${normalizedExpression}': ${e.message}`); } } }