feat: one-time tasks
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { CronType } from "@/enums";
|
||||
import { CronExpressionParser } from "cron-parser";
|
||||
|
||||
const ONE_TIME_VALUE = "@one-time";
|
||||
|
||||
export function parseCron(cron: string | null): {
|
||||
type: CronType;
|
||||
value: number;
|
||||
@@ -11,6 +13,11 @@ export function parseCron(cron: string | null): {
|
||||
type: CronType.MANUAL,
|
||||
value: 0,
|
||||
};
|
||||
} else if (cron === ONE_TIME_VALUE) {
|
||||
return {
|
||||
type: CronType.ONE_TIME,
|
||||
value: 0,
|
||||
};
|
||||
} else if ((value = /^0 (\d+) \* \* \*$/i.exec(cron)) !== null) {
|
||||
return {
|
||||
type: CronType.EVERY_DAY,
|
||||
@@ -45,6 +52,8 @@ export function getCron(
|
||||
fallback: string,
|
||||
): string | null {
|
||||
switch (type) {
|
||||
case CronType.ONE_TIME:
|
||||
return ONE_TIME_VALUE;
|
||||
case CronType.EVERY_DAY:
|
||||
return `0 ${value.toFixed(0)} * * *`;
|
||||
case CronType.EVERY_WEEK:
|
||||
@@ -62,6 +71,10 @@ export function getCron(
|
||||
return null;
|
||||
}
|
||||
|
||||
export function isOneTime(cron: string | null): boolean {
|
||||
return cron === ONE_TIME_VALUE;
|
||||
}
|
||||
|
||||
export function validCron(cron: string): boolean {
|
||||
try {
|
||||
CronExpressionParser.parse(cron);
|
||||
|
||||
Reference in New Issue
Block a user