-
Notifications
You must be signed in to change notification settings - Fork 75
/
schedule.explorer.ts
129 lines (117 loc) · 4.01 KB
/
schedule.explorer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { DiscoveryService, MetadataScanner } from '@nestjs/core';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { SchedulerType } from './enums/scheduler-type.enum';
import { SchedulerMetadataAccessor } from './schedule-metadata.accessor';
import { SchedulerOrchestrator } from './scheduler.orchestrator';
@Injectable()
export class ScheduleExplorer implements OnModuleInit {
private readonly logger = new Logger('Scheduler');
constructor(
private readonly schedulerOrchestrator: SchedulerOrchestrator,
private readonly discoveryService: DiscoveryService,
private readonly metadataAccessor: SchedulerMetadataAccessor,
private readonly metadataScanner: MetadataScanner,
) {}
onModuleInit() {
this.explore();
}
explore() {
const instanceWrappers: InstanceWrapper[] = [
...this.discoveryService.getControllers(),
...this.discoveryService.getProviders(),
];
instanceWrappers.forEach((wrapper: InstanceWrapper) => {
const { instance } = wrapper;
if (!instance || !Object.getPrototypeOf(instance)) {
return;
}
this.metadataScanner.scanFromPrototype(
instance,
Object.getPrototypeOf(instance),
(key: string) =>
wrapper.isDependencyTreeStatic()
? this.lookupSchedulers(instance, key)
: this.warnForNonStaticProviders(wrapper, instance, key),
);
});
}
lookupSchedulers(instance: Record<string, Function>, key: string) {
const methodRef = instance[key];
const metadata = this.metadataAccessor.getSchedulerType(methodRef);
switch (metadata) {
case SchedulerType.CRON: {
const cronMetadata = this.metadataAccessor.getCronMetadata(methodRef);
const cronFn = this.wrapFunctionInTryCatchBlocks(methodRef, instance);
return this.schedulerOrchestrator.addCron(cronFn, cronMetadata!);
}
case SchedulerType.TIMEOUT: {
const timeoutMetadata = this.metadataAccessor.getTimeoutMetadata(
methodRef,
);
const name = this.metadataAccessor.getSchedulerName(methodRef);
const timeoutFn = this.wrapFunctionInTryCatchBlocks(
methodRef,
instance,
);
return this.schedulerOrchestrator.addTimeout(
timeoutFn,
timeoutMetadata!.timeout,
name,
);
}
case SchedulerType.INTERVAL: {
const intervalMetadata = this.metadataAccessor.getIntervalMetadata(
methodRef,
);
const name = this.metadataAccessor.getSchedulerName(methodRef);
const intervalFn = this.wrapFunctionInTryCatchBlocks(
methodRef,
instance,
);
return this.schedulerOrchestrator.addInterval(
intervalFn,
intervalMetadata!.timeout,
name,
);
}
}
}
warnForNonStaticProviders(
wrapper: InstanceWrapper<any>,
instance: Record<string, Function>,
key: string,
) {
const methodRef = instance[key];
const metadata = this.metadataAccessor.getSchedulerType(methodRef);
switch (metadata) {
case SchedulerType.CRON: {
this.logger.warn(
`Cannot register cron job "${wrapper.name}@${key}" because it is defined in a non static provider.`,
);
break;
}
case SchedulerType.TIMEOUT: {
this.logger.warn(
`Cannot register timeout "${wrapper.name}@${key}" because it is defined in a non static provider.`,
);
break;
}
case SchedulerType.INTERVAL: {
this.logger.warn(
`Cannot register interval "${wrapper.name}@${key}" because it is defined in a non static provider.`,
);
break;
}
}
}
private wrapFunctionInTryCatchBlocks(methodRef: Function, instance: object) {
return async (...args: unknown[]) => {
try {
await methodRef.call(instance, ...args);
} catch (error) {
this.logger.error(error);
}
};
}
}