| 11 | class TaskEmitter extends EventEmitter{} |
| 12 | |
| 13 | export class InlineScheduledTask implements ScheduledTask { |
| 14 | emitter: TaskEmitter; |
| 15 | cronExpression: string; |
| 16 | timeMatcher: TimeMatcher; |
| 17 | runner: Runner; |
| 18 | id: string; |
| 19 | name: string; |
| 20 | stateMachine: StateMachine; |
| 21 | timezone?: string; |
| 22 | logger: Logger; |
| 23 | suppressMissedWarning: boolean; |
| 24 | // The last actual execution. Recorded when a run really finishes or fails, |
| 25 | // keyed off the execution's own timestamps rather than the tick that armed it. |
| 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)); |
nothing calls this directly
no outgoing calls
no test coverage detected