node-cron
Version:
Job scheduling for Node.js with overlap prevention, distributed coordination, and background tasks. Zero dependencies, written in TypeScript.
120 lines (113 loc) • 4.12 kB
TypeScript
interface Logger {
info(message: string): void;
warn(message: string): void;
error(message: string | Error, err?: Error): void;
debug(message: string | Error, err?: Error): void;
}
declare function setLogger(logger: Logger): void;
interface RunCoordinator {
shouldRun(key: string, ttlMs: number): boolean | Promise<boolean>;
onComplete?(key: string): void | Promise<void>;
}
type SkipReason = 'not-elected' | 'coordinator-error';
declare function setRunCoordinator(coordinator: RunCoordinator | undefined): void;
type TaskContext = {
date: Date;
dateLocalIso: string;
task?: ScheduledTask;
execution?: Execution;
triggeredAt: Date;
reason?: SkipReason;
};
type TaskEvent = 'task:started' | 'task:stopped' | 'task:destroyed' | 'execution:started' | 'execution:finished' | 'execution:failed' | 'execution:missed' | 'execution:overlap' | 'execution:maxReached' | 'execution:skipped';
type TaskOptions = {
timezone?: string;
name?: string;
noOverlap?: boolean;
distributed?: boolean;
runCoordinator?: RunCoordinator;
distributedLease?: number;
maxExecutions?: number;
maxRandomDelay?: number;
logger?: Logger;
suppressMissedWarning?: boolean;
missedExecutionTolerance?: number;
executeTimeout?: number;
startTimeout?: number;
};
type Execution = {
id: string;
reason: 'invoked' | 'scheduled';
startedAt?: Date;
finishedAt?: Date;
error?: Error;
result?: any;
};
type LastRun = {
date: Date;
result?: unknown;
error?: Error;
};
type TaskFn = (context: TaskContext) => any | Promise<any>;
interface ScheduledTask {
id: string;
name?: string;
start(): void | Promise<void>;
stop(): void | Promise<void>;
getStatus(): string;
destroy(): void | Promise<void>;
execute(): Promise<any>;
getNextRun(): Date | null;
getNextRuns(count: number): Date[];
match(date: Date): boolean;
msToNext(): number | null;
isBusy(): boolean;
runsLeft(): number | undefined;
getPattern(): string;
lastRun(): LastRun | null;
on(event: TaskEvent, fun: (context: TaskContext) => Promise<void> | void): void;
off(event: TaskEvent, fun: (context: TaskContext) => Promise<void> | void): void;
once(event: TaskEvent, fun: (context: TaskContext) => Promise<void> | void): void;
}
interface CronFieldError {
field: string;
value?: string;
message: string;
}
interface ParsedFields {
second: number[];
minute: number[];
hour: number[];
dayOfMonth: (number | string)[];
month: number[];
dayOfWeek: (number | string)[];
}
interface DetailedValidation {
valid: boolean;
fields?: ParsedFields;
errors: CronFieldError[];
}
declare function validateDetailed$1(pattern: string): DetailedValidation;
declare function parse$1(pattern: string): ParsedFields;
declare function schedule(expression: string, func: TaskFn | string, options?: TaskOptions): ScheduledTask;
declare function createTask(expression: string, func: TaskFn | string, options?: TaskOptions): ScheduledTask;
declare function solvePath(filePath: string): string;
declare function validate(expression: string): boolean;
declare const validateDetailed: typeof validateDetailed$1;
declare const parse: typeof parse$1;
declare const getTasks: () => Map<string, ScheduledTask>;
declare const getTask: (taskId: string) => ScheduledTask | undefined;
interface NodeCron {
schedule: typeof schedule;
createTask: typeof createTask;
validate: typeof validate;
validateDetailed: typeof validateDetailed;
parse: typeof parse;
getTasks: typeof getTasks;
getTask: typeof getTask;
setLogger: typeof setLogger;
setRunCoordinator: typeof setRunCoordinator;
}
declare const nodeCron: NodeCron;
export { createTask, nodeCron as default, getTask, getTasks, nodeCron, parse, schedule, setLogger, setRunCoordinator, solvePath, validate, validateDetailed };
export type { CronFieldError, DetailedValidation, LastRun, Logger, NodeCron, ParsedFields, RunCoordinator, ScheduledTask, SkipReason, TaskContext, TaskFn, TaskOptions };