UNPKG

@dexare/cron

Version:

A Dexare module that manages cron jobs

46 lines (45 loc) 1.65 kB
import { DexareModule, DexareClient, BaseConfig } from 'dexare'; import { CronJob, CronCommand, CronJobParameters } from 'cron'; export interface CronConfig extends BaseConfig { cron?: CronModuleOptions; } export interface CronModuleOptions { loadFolder?: string; } export declare type CronModuleCommand = CronCommand | ((client: DexareClient<any>, job: CronJob) => void); export interface CronModuleJobParams extends Omit<CronJobParameters, 'context' | 'onTick' | 'onComplete'> { /** * The name of this cron job. */ name: string; /** * The function to fire at the specified time. If an ```onComplete``` callback was provided, ```onTick``` will receive it as an argument. ```onTick``` may call ```onComplete``` when it has finished its work. */ onTick: CronModuleCommand; /** * A function that will fire when the job is stopped with ```job.stop()```, and may also be called by ```onTick``` at the end of each run. */ onComplete?: CronModuleCommand; } export default class CronModule<T extends DexareClient<any>> extends DexareModule<T> { crons: Map<string, CronJob>; constructor(client: T); load(): void; unload(): void; /** * Registers cron jobs from a folder. * @param path The path to register from. */ registerFromFolder(path: string): Promise<any>; /** * Registers a cron job. * @param opts The options for the new cron job */ register(opts: CronModuleJobParams): CronJob; /** * Unregisters a cron job. * @param job The job name to unregister */ unregister(job: string): void; private _wrapFn; }