(cronExpression: string, taskFn: TaskFn, options?: TaskOptions)
| 26 | private _lastRun: LastRun | null = null; |
| 27 | |
| 28 | constructor(cronExpression: string, taskFn: TaskFn, options?: TaskOptions){ |
| 29 | this.emitter = new TaskEmitter(); |
| 30 | this.cronExpression = cronExpression; |
| 31 | |
| 32 | this.id = createID(); |
| 33 | this.name = options?.name || this.id; |
| 34 | this.timezone = options?.timezone; |
| 35 | this.logger = options?.logger || logger; |
| 36 | this.suppressMissedWarning = options?.suppressMissedWarning || false; |
| 37 | |
| 38 | this.timeMatcher = new TimeMatcher(cronExpression, options?.timezone) |
| 39 | this.stateMachine = new StateMachine(); |
| 40 | |
| 41 | const runnerOptions: RunnerOptions = { |
| 42 | timezone: options?.timezone, |
| 43 | noOverlap: options?.noOverlap, |
| 44 | maxExecutions: options?.maxExecutions, |
| 45 | maxRandomDelay: options?.maxRandomDelay, |
| 46 | missedExecutionTolerance: options?.missedExecutionTolerance, |
| 47 | logger: this.logger, |
| 48 | beforeRun: (date: Date, execution: Execution) => { |
| 49 | if(execution.reason === 'scheduled'){ |
| 50 | this.changeState('running'); |
| 51 | } |
| 52 | this.emitter.emit('execution:started', this.createContext(date, execution)); |
| 53 | return true; |
| 54 | }, |
| 55 | onFinished: (date: Date, execution: Execution) => { |
| 56 | if(execution.reason === 'scheduled'){ |
| 57 | this.changeState('idle'); |
| 58 | } |
| 59 | this.recordLastRun(execution); |
| 60 | this.emitter.emit('execution:finished', this.createContext(date, execution)); |
| 61 | return true; |
| 62 | }, |
| 63 | onError: (date: Date, error: Error, execution: Execution) => { |
| 64 | this.logger.error(error); |
| 65 | this.recordLastRun(execution); |
| 66 | this.emitter.emit('execution:failed', this.createContext(date, execution)); |
| 67 | this.changeState('idle'); |
| 68 | }, |
| 69 | onOverlap: (date: Date) => { |
| 70 | this.emitter.emit('execution:overlap', this.createContext(date)); |
| 71 | }, |
| 72 | onMissedExecution: (date: Date) => { |
| 73 | // Warn only when the caller is not handling the missed execution |
| 74 | // themselves (no listener) and has not opted out explicitly. |
| 75 | const handled = this.emitter.listenerCount('execution:missed') > 0; |
| 76 | if(!this.suppressMissedWarning && !handled){ |
| 77 | this.logger.warn(`missed execution at ${date}! Possible blocking IO or high CPU user at the same process used by node-cron.`); |
| 78 | } |
| 79 | this.emitter.emit('execution:missed', this.createContext(date)); |
| 80 | }, |
| 81 | onMaxExecutions: (date: Date) => { |
| 82 | this.emitter.emit('execution:maxReached', this.createContext(date)); |
| 83 | this.destroy(); |
| 84 | }, |
| 85 | // Distributed coordination: only wired when this task opted in |
nothing calls this directly
no test coverage detected