export type LogObject = { name: string; msg: string; on: Date; data?: unknown; err: Error; }; export type LogFn = (log: LogObject) => void; export type SchedulerOptions = { /** * Custom logger function, will receive an instance of LogObject when an error is thrown */ logger?: LogFn; /** * Name of the pubsub (used in logs as well) */ name?: string; /** * Default timeZone to use */ timeZone?: string | null; /** * Whether or not to run async jobs in parallel or not (defaults to true) */ parallel?: boolean | number; /** * If set to true will automatically run the schedule every 60 seconds (defaults to false) */ auto?: boolean; }; type SchedulerJobFn = ((data: any) => void | Promise); type SchedulerJobFnDefault = (data: unknown) => void | Promise; export type SchedulerJob = { name: string; schedule: string; timeZone?: string | null; fn: T; data?: Parameters[0]; }; /** * The Scheduler class. * * Instantiate this module, add multiple scheduled tasks, * and then call run() to execute those whose cron schedule matches the current time. */ declare class Scheduler { #private; constructor(options?: SchedulerOptions); /** * Getter returning the name of the scheduler. */ get name(): string; /** * Getter returning the scheduler's timeZone setting */ get timeZone(): string | null; /** * Getter returning the scheduler's parallel setting */ get parallel(): number | boolean; /** * Getter returning whether or not the schedule is automatically running every 60 seconds */ get isAutomatic(): boolean; /** * Returns the configured jobs array (bar fn/internals) to allow for introspection and debugging */ get jobs(): { data?: {} | undefined; name: string; schedule: string; timeZone: string | null; }[]; /** * Add a job to the scheduler. * * @param {SchedulerJob} job - Raw job object to be added to the jobs list */ add(job: SchedulerJob): boolean; /** * Remove a job from the schedule by name * * @param {string|string[]} name - Name or array of names of the job(s) to remove */ remove(name: string | string[]): void; /** * Iterate through all added jobs and execute those that should run at the current time. */ run(): Promise; /** * Stops automatic running of the schedule */ stopAutomaticRun(): void; /** * Starts automatic running of the schedule */ startAutomaticRun(): void; /** * MARK: Private */ /** * Helper which converts a cron schedule to a map for easy processing down the line * * @param {string} schedule - Raw cron schedule to process */ private static convertToMap; /** * Returns the time in the provided zone as an object of parts * * @param {Date} date - Date to get parts from * @param {string|null} timeZone - Zone to use */ private static getTimeParts; /** * Checks the processed time against the processed map and returns true/false if matches or not * * @param {CronMap} map - Cron Map * @param {TimeMap} time - Time Map */ private static checkTimeAgainstMap; /** * MARK: Static */ /** * Checks if a string is a valid cron schedule * * @param {string} raw - Value to verify is a valid schedule */ static isCronSchedule(raw: string): boolean; /** * Check if a given cron schedule should run at the current (or specified) time. * * @param schedule A cron string (5 fields: minute, hour, day of month, month, day of week). * @param timeZone Optional IANA time zone string. * @returns true if the schedule matches the current time. */ static cronShouldRun(schedule: string, timeZone?: string | null): boolean; } export { Scheduler, Scheduler as default };