|
| 1 | +import { ScheduleTypeLike } from '../model/ScheduleMetadata'; |
| 2 | +import { AccessLevel, ContextProto, EggProtoImplClass, PrototypeUtil } from '@eggjs/core-decorator'; |
| 3 | +import { ScheduleInfoUtil } from '../util/ScheduleInfoUtil'; |
| 4 | +import { StackUtil } from '@eggjs/tegg-common-util'; |
| 5 | + |
| 6 | +export interface ScheduleParams<T> { |
| 7 | + type: ScheduleTypeLike; |
| 8 | + scheduleData: T; |
| 9 | +} |
| 10 | + |
| 11 | +export interface CronParams { |
| 12 | + cron: string; |
| 13 | + cronOptions?: any; |
| 14 | +} |
| 15 | + |
| 16 | +export interface IntervalParams { |
| 17 | + interval: string | number; |
| 18 | +} |
| 19 | + |
| 20 | +export type CronScheduleParams = ScheduleParams<CronParams>; |
| 21 | +export type IntervalScheduleParams = ScheduleParams<IntervalParams>; |
| 22 | + |
| 23 | +export interface ScheduleOptions { |
| 24 | + // default is false |
| 25 | + immediate?: boolean; |
| 26 | + // default is false |
| 27 | + disable?: boolean; |
| 28 | + // if env has value, only run in this envs |
| 29 | + env?: Array<string>; |
| 30 | +} |
| 31 | + |
| 32 | +export interface ScheduleSubscriber { |
| 33 | + subscribe(data?: any): Promise<any>; |
| 34 | +} |
| 35 | + |
| 36 | +export function Schedule<T>(param: ScheduleParams<T>, options?: ScheduleOptions) { |
| 37 | + return function(clazz: EggProtoImplClass<ScheduleSubscriber>) { |
| 38 | + ScheduleInfoUtil.setIsSchedule(true, clazz); |
| 39 | + ScheduleInfoUtil.setScheduleParams(param, clazz); |
| 40 | + if (options) { |
| 41 | + ScheduleInfoUtil.setScheduleOptions(options, clazz); |
| 42 | + } |
| 43 | + const func = ContextProto({ |
| 44 | + name: clazz.name, |
| 45 | + accessLevel: AccessLevel.PUBLIC, |
| 46 | + }); |
| 47 | + func(clazz); |
| 48 | + |
| 49 | + PrototypeUtil.setFilePath(clazz, StackUtil.getCalleeFromStack(false, 5)); |
| 50 | + }; |
| 51 | +} |
| 52 | + |
0 commit comments